下载TXT小说来看,每次看完,下次要很费力才找到,上次看到那里.所以想将一个.txt文件,分为多个txt文件.
运行之后,有些txt文件,有内容;有些文件,没有内容.不知那出错了.请教各位大侠.
import java.io.*;
public class TestIO {

public static void main(String[] args) throws IOException {
int j=0;//行数
String s1;
int l=1;//txt文件数
File f2 ;
BufferedWriter bw;
     File f3=new File(".\\新建文件夹");
     f3.mkdir();//在当前目录下创建一个新文件夹
    File f=new File("."); 
    if(f.isDirectory()){
     String[] s=f.list();
    for(int i=0;i<s.length;i++){
     File f1=new File("."+ "/" + s[i]);
     //判断是否是文件
     if(f1.isFile()){
     //判断是否是txt文件
     if(f1.getName().endsWith(".txt")){
     String s2=f1.getName();
     BufferedReader br=new BufferedReader(new FileReader("."+ "/"+s2));
    
     f2=new File(".\\新建文件夹" +"/" + l +".txt");
     f2.createNewFile();
     bw=new BufferedWriter(new FileWriter(".\\新建文件夹" +"/" + l +".txt"));
     s1=br.readLine();
     while(s1!=null){
     if(j%200==0){ //如果超过200行,创建一个新的文件
     l++;
      f2=new File(".\\新建文件夹" +"/" + l +".txt");
          f2.createNewFile();
          bw=new BufferedWriter(new FileWriter(".\\新建文件夹" +"/" + l +".txt"));
          
     }
     //写入文件
     bw.write(s1);
     bw.newLine();
     s1=br.readLine();
     j++;
     }
    
     }
     }
    }
   
    }
    
    
}}