我有一个ww.txt 文件,文件内容如下:02,10,11,18,27,30
06,09,11,14,20,31
01,03,04,16,18,22
07,14,22,27,28,32
04,05,09,15,21,30
12,13,17,20,22,23
02,06,10,15,21,27
13,16,20,21,27,31
05,06,14,21,29,32
03,10,12,28,29,32
01,11,15,16,32,33
07,08,15,17,23,30
04,12,15,16,19,28
05,13,15,29,30,32
02,04,11,16,17,28
03,12,17,23,28,31
02,06,16,24,30,31
03,05,10,13,18,19
08,13,15,16,24,25
01,03,21,27,30,32
11,15,19,20,21,23
11,14,15,19,24,31
03,14,15,21,22,27
01,03,15,16,22,29
03,06,11,16,19,22
16,22,23,24,27,30
01,08,09,13,20,32
11,21,23,25,26,30
01,08,11,13,23,32
01,03,08,13,23,32
01,03,11,13,18,32
06,09,17,18,25,33
11,13,15,19,24,26
03,12,16,21,27,32
01,04,09,15,24,25
03,06,11,14,19,20
03,06,10,20,28,32
06,07,10,13,18,26
09,15,16,17,22,29
02,05,06,13,15,27
09,18,25,29,31,33
.................
.................我想每五行生成一个文件,如 生成 1.txt 内容(就是前5行)
02,10,11,18,27,30
06,09,11,14,20,31
01,03,04,16,18,22
07,14,22,27,28,32
04,05,09,15,21,302.txt 内容就是(6-10行), 3.txt 内容就是(11-15),依次类推....小弟初学, 线等

解决方案 »

  1.   

    用字符流读取文件每读5行保存一个文件。readline()方法,你懂的。俺来写段代码看看
      

  2.   

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    public class FileSplit
    {
        public static void main(String[] args)
        {
    //目标文件
    File srcFile = new File("d:/test.txt");

    //按行读取的字符串
    String readLine = null;

    //创建文件的编号
    int n = 1;
    try
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
        BufferedWriter bw = null;
        for(;;)
        {
    //如果读到文件末尾则退出
    if((readLine = br.readLine()) == null)
    {
        System.out.println(readLine);
        break;
    }

    //每5行创建一个文件
    bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/"+n+".txt")));
    for(int i=0; i<4; i++)
    {
        if(i==0)
        {
    bw.write(readLine);
    bw.newLine();
        }
        
        //如果读到文件末尾则退出
        if((readLine = br.readLine()) != null)
        {
    bw.write(readLine);
        }
        bw.newLine();
    }
    bw.flush();
    bw.close();
    n++;
        }
        br.close();
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        }
    }
      

  3.   

    public static void main(String[] args) throws Exception {
    int line = 1;
    int flag = 1; BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("c:/test.txt")));
    BufferedWriter writer;
    String temp;
    StringBuffer sb = new StringBuffer();
    while (true) {
    temp = reader.readLine();// 逐行讀取 if (temp != null) {
    sb.append(temp + "\r\n");
    } if (line++ == 5 || temp == null) {
    writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:/t/" + (flag++) + ".txt")));
    writer.write(sb.toString());// 寫出文件
    writer.flush();
    writer.close();
    sb.delete(0, sb.length());// 清空保存的上一個文件的內容
    line = 1;// 計數器復位
    if (temp == null) {
    break;
    }
    }
    }
    reader.close();
    }
      

  4.   

    import java.io.*;class MyFile {
        private static int count = 1;// 1号文件,
        private final int id = count++;// 然后依次递增
        private String fileName = id + ".txt";    public MyFile(StringBuffer text) throws Exception {// 将截取的文本传入,就可以产生新文件
    PrintWriter pw = new PrintWriter(new File(fileName));
    pw.print(text);
    pw.flush();
    pw.close();
        }
    }public class TestFileSplit { // 将一个文件分开为多个文件
        BufferedReader br = null;    public void execute() throws Exception {
    br = new BufferedReader(new FileReader(new File("test.txt")));
    String line = null;
    StringBuffer sb = new StringBuffer();
    int i = 0;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\r\n");
        i++;
        if (i % 5 == 0) {
    new MyFile(sb);
    sb.delete(0, sb.length());
        }
    }
    if (sb.length() != 0)
        new MyFile(sb);// 如果不足5行或最后凑不够5行
    br.close();
        }    public static void main(String[] args) throws Exception {
    new TestFileSplit().execute();
        }
    }
      

  5.   

    都是将整篇文章读入到缓冲区啊,要是这个txt过G的话,内存吃不消啊