最近要做一个文本分割,要求把一个txt文件分割成十个txt文件,我想按照每个文本文件50行来分,下面是我写的四段代码,由于小弟java不太成熟,大神不要见笑,现在问题是生成的每个新文件里面只有一行文本(应该有50行才对的)public class inputFile {
/**
 * @param the start of pre_process
 * @throws IOException 
 */
public static void main(String[] args)  throws IOException {
fileCut in=new fileCut("E:/reviewsNew.txt");//创建一个fileCut对象 in
in.inputfile();
}
}
public class fileCut implements RandomAccess{
private String filetrack;
private String copyline;
private int count=1;

public fileCut(String string) {
// TODO 传入文件路径
this.filetrack=string;
} void inputfile() throws IOException{
// TODO 创建RandomAccessFile类型对象, 并读取文件
File track=new File(filetrack);
RandomAccessFile sourcefile=new RandomAccessFile(track, "r");
System.out.println("I have read it");

setName newname=new setName();
int t=newname.setName();//得到一个int型的变量,用于命名新文件

for(int i=0;i<10;i++){  //十次循环,每次新建一个文件
count=1;
writeLine write=new writeLine();// 声明一个writeLine对象write
int countfile = t++;
write.writeNewfile(countfile);
while(count<=50){           // 循环将58行写入一个文件
copyline = sourcefile.readLine();
write.writeLine(copyline);
count++;
                  }
}
}
}
public class writeLine {
public String newinline;
public int a;
public writeLine(){
}

public DataOutputStream writefile(int a) throws FileNotFoundException{
DataOutputStream d = new DataOutputStream(new FileOutputStream("E:/files/"+a+".txt"));
return d;
}

public void writeNewfile(int t) throws FileNotFoundException {
a=t;
writefile(a);
         } public void writeLine(String s) throws IOException{
this.newinline=s;
writefile(a).writeBytes(newinline);
System.out.println("now I am writing into the file :"+a);
}

}
接下来这个函数写的有点累赘
public class setName {
   int txtnum=0;
   
   int setName(){
   return txtnum;
   }
}我自己感觉是红色字体部分的循环有问题,可是就是找不到解决方法

解决方案 »

  1.   

    个人认为:
    public class writeLine {
    public String newinline;
    public int a;
    private DataOutputStream d;
    public writeLine(){
    }public void writefile(int a) throws FileNotFoundException{
    this.d = new DataOutputStream(new FileOutputStream("E:/files/"+a+".txt"));
    }public void writeNewfile(int t) throws FileNotFoundException {
    a=t;
    writefile(a);
      }public void writeLine(String s) throws IOException{
    this.d.writeByte((s + "\r\n").getBytes());
    }}应该可以。最好能加上close()方法。作用就是关闭DataOutputStream.在使用每个文件使用完毕使用。
      

  2.   


    public class InputFile {
    /**
    * @param the start of pre_process
    * @throws IOException  
    */
    public static void main(String[] args) throws IOException {
    FileCut in=new FileCut("E:/temp/test.txt");//创建一个fileCut对象 in
    in.inputfile();
    }
    }public class FileCut implements RandomAccess {
    private String filetrack;
    private String copyline; public FileCut(String string) {
    // TODO 传入文件路径
    this.filetrack = string;
    } void inputfile() throws IOException {
    // TODO 创建RandomAccessFile类型对象, 并读取文件
    File track = new File(filetrack);
    RandomAccessFile sourcefile = new RandomAccessFile(track, "r");
    System.out.println("I have read it"); int t = 1; for (int i = 0; i < 10; i++) { // 十次循环,每次新建一个文件
    int count = 0;
    DataOutputStream d = new DataOutputStream(new FileOutputStream(
    "E:/" + (t++) + ".txt"));
    while (count < 50) { // 循环将58行写入一个文件
    copyline = sourcefile.readLine();
    d.writeBytes(copyline);
    count++;
    }
    d.close();
    }
    }
    }
    不知道你写这么多类要干嘛...不晕吗..?
      

  3.   

    补充一下,要分行的话在
    d.writeBytes(copyline);//后面加一句 d.writeChars("\n");
      

  4.   

    这是以前学习时候自己写的文件切割和文件合并的例子,希望对你有用:
    package com.xinzhanedu1;import java.io.*;public class Testdevide {
    public static void main(String[] args) {
    File file = new File("E:/初恋这件小事.rmvb");
    File file1 = new File("E:/1.rmvb");
    File file2 = new File("E:/2.rmvb"); int i ,j = 0;
    try {
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos1 = new FileOutputStream(file1);
    BufferedOutputStream bos1 = new BufferedOutputStream(fos1); FileOutputStream fos2 = new FileOutputStream(file2);
    BufferedOutputStream bos2 = new BufferedOutputStream(fos2); while((i = bis.read()) != -1) {
    j++;
    if (j < 100000000) {
    bos1.write(i);
    } else {
    bos2.write(i);
    }

    }
    fos1.flush();
    fos2.flush();
    fos1.close();
    fos2.close();
    fis.close();
    } catch (FileNotFoundException e) {
    System.out.println("文件没有找到!");
    } catch (IOException e) {
    System.out.println("输入输出异常!");
    }
    System.out.println("文件切割完成");
    }
    }
    //**************************************
    package com.xinzhanedu2;import java.io.*;public class TestCombine {
    public static void main(String[] args) {
    File file1 = new File("E:/1.rmvb");
    File file2 = new File("E:/2.rmvb");
    File file = new File("E:/combine.rmvb");

    int i=0;

    try {
    FileInputStream fis1 = new FileInputStream(file1);
    BufferedInputStream bis1 = new BufferedInputStream(fis1);

    FileInputStream fis2 = new FileInputStream(file2);
    BufferedInputStream bis2 = new BufferedInputStream(fis2);

    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    while((i = bis1.read())!=-1){
    bos.write(i);
    }
    while((i = bis2.read())!=-1){
    bos.write(i);
    }

    bos.flush();
    bis1.close();
    bis2.close();
    bos.close();

    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("文件合并完成!");
    }}