原本一个很简单的程序,脑袋真笨了一会...唉...写了2个多小时...最后冷静了一下用集合存放,才迎刃而解....发表一下做个纪念吧..
/**
* 将一个txt文档,分为几个文档。
*
* @author XiongXing
*
*/
public class ReadAndWriteTxt {
public static void main(String[] args) {
try {
readTxt();
} catch (IOException e) {
System.out.println("异常");
}
System.out.println("完毕");
}
public static void readTxt() throws IOException {
int count =1;
FileInputStream inputFile = new FileInputStream("F:/20110930/20110930.txt");
String line = ""; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(inputFile));
ArrayList<String> list=new ArrayList<String>();
line = reader.readLine(); // 读取第一行
while (line != null) {
list.add(line); // 将读到的内容添加到 集合当中。
if(count%1000==0){ //每一千行创建一个txt文件
//FileOutputStream fos = new FileOutputStream("F:/20111001/"+count+".txt");
PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("F:/20111001/"+count+".txt")));
for(Iterator<String> it=list.iterator();it.hasNext();){
pw.println(it.next().toString());
}
pw.flush();
pw.close();
list.clear();
}
line = reader.readLine(); // 读取下一行
count++;
}
//将最后多的几行写完
PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("F:/20111001/end.txt")));
for(Iterator<String> it=list.iterator();it.hasNext();){
pw.println(it.next().toString());
}
pw.flush();
pw.close();
list.clear();
reader.close();
}
}