首页 > 代码库 > java基础 之 TXT文件处理(感觉比c++好用一些)
java基础 之 TXT文件处理(感觉比c++好用一些)
一:c/c++ 处理文件的用法,详见博客 c++文件操作
二:java与c++的方便之处:
(1) java在读取文件时,可以对字符流进行处理,重新进行编码,如 InputStreamReader reader = new InputStreamReader(new FileInputStream(file),encoding);
(2) BufferReader bufferReader = new BufferReader(file) 进行buffer处理
(3)java的非常好用的一点就是,伸手就来,如 int 转 string ( 如Integer.toString(intNumber) ),字符串分隔(stringStr.split(exp); ) ,字符串重新合成,直接加即可,非常好用;
(4)再借助于集成开发环境 IDE 缺少的依赖类,自动引入功能,更是如虎添翼。
三:处理TXT问文件的示例
package edu.tju.cs; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; public class OriginalDevideByTime { String times[] = {"6:00:00","6:30:00","7:00:00","7:30:00","8:00:00","8:30:00","9:00:00","9:30:00","10:00:00","10:30:00", "11:00:00","11:30:00","12:00:00","12:30:00","13:00:00","13:30:00","14:00:00","14:30:00","15:00:00","15:30:00", "16:00:00","16:30:00","17:00:00","17:30:00","18:00:00","18:30:00","19:00:00","19:30:00","20:00:00","20:30:00", "21:00:00","21:30:00","22:00:00","22:30:00","23:00:00","23:30:00","24:00:00"}; // 合成输出文件名 public String getFileName(int cp){ String fileName = "D:\\tjdata_metro\\results\\"; fileName = fileName + Integer.toString(cp) + ".csv"; return fileName; } // 读取文件,并分隔数据 public void readTxtFile(String filePath){ try { String encoding="GBK"; File file=new File(filePath); int cp = 1; if(file.isFile() && file.exists()){ //判断文件是否存在 InputStreamReader read = new InputStreamReader( new FileInputStream(file),encoding);//考虑到编码格式 BufferedReader bufferedReader = new BufferedReader(read); String originalLine = null; String newLine = null; while((originalLine = bufferedReader.readLine()) != null){ bufferedReader.mark((int)file.length()+1);// 设置文件标志位 //写入文件名处理 if(cp >= times.length) return; String fileName = getFileName(cp); BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileName))); while((originalLine = bufferedReader.readLine()) != null){ // 字符串分隔 String tmp[] = originalLine.split(","); // 如果符合重新合成 if(tmp.length>3) if(tmp[3].compareTo(times[cp-1])>=0&&tmp[3].compareTo(times[cp])<0){ newLine = tmp[0] + "," + tmp[1] + "," + tmp[2] + "," + tmp[3] + "," + tmp[4] + "," + tmp[5] + "," + tmp[6] + "," + tmp[7]; System.out.println(newLine); writer.write(newLine + "\n"); } else System.out.println("Not between times"); } // 关闭写文件 writer.close(); // 重置为文件开始位置,cp自加 bufferedReader.reset(); cp ++; } bufferedReader.close(); read.close(); } else { System.out.println("OriginalDevideByTime……找不到指定的文件"); } } catch (Exception e) { System.out.println("OriginalDevideByTime……读取文件内容出错"); e.printStackTrace(); } System.out.println("OriginalDevideByTime……Devide is over!!!"); } // main 函数入口 public static void main(String argv[]){ String filePath = "D:\\tjdata_metro\\TOKEN_ENEX_201404_20W.csv"; OriginalDevideByTime devide = new OriginalDevideByTime(); devide.readTxtFile(filePath); } }
四:学习心得:
(1)JAVA中文件指针复位到文件开头
File file = new File("temp.txt");
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(file));
reader.mark((int)file.length()+1);
// to do something
reader.reset();
注意:如果文件串的最后一个字符是中文,使用mark()中的长度设为file.length()
如果文件的最后一个字符串是英文或数字,则java.io.IOException: Mark invalid,使用mark()中的长度设为file.length()+1
mark用于标记当前位置;在读取一定数量的数据(小于readlimit的数据)后使用reset可以回到mark标记的位置;FileInputStream不支持mark/reset操作;BufferedInputStream支持此操作;
mark(readlimit)的含义是在当前位置作一个标记,制定可以重新读取的最大字节数,也就是说你如果标记后读取的字节数大于readlimit,你就再也回不到回来的位置了;通常InputStream的read()返回-1后,说明到达文件尾,不能再读取。除非使用了mark/reset
(2)JAVA中int转String类型有三种方法
1》String.valueOf(i)
2》 Integer.toString(i)
3》 i+""
valueOf()和toString()方法可以明白,但为什么3中加上一个""引号也可以?谁有资料可以看到?
(3)stringStr.compareTo(otherstring) ,相等返回 0 ,若是stringStr大于otherstring返回正数,若是stringStr小于otherstring返回负数
java基础 之 TXT文件处理(感觉比c++好用一些)