【问题标题】:Create multiple files from one text file in java在java中从一个文本文件创建多个文件
【发布时间】:2017-09-14 07:01:05
【问题描述】:

我有一个input.txt 文件,假设有520 行。 我必须在 java 中编写一个这样的代码。

从前 200 行创建第一个名为 file-001.txt 的文件。然后从 201-400 行创建另一个 file-002。然后 file-003.txt 从剩余的行中。

我已经编写了代码,它只写了前 200 行。为了将其工作更新到上述场景,我需要进行哪些更改。

    public class DataMaker {
 public static void main(String args[]) throws IOException{
     DataMaker dm=new DataMaker();
     String file= "D:\\input.txt";
     int roll=1;
     String rollnum ="file-00"+roll;
     String outputfilename="D:\\output\\"+rollnum+".txt";
     String urduwords;
     String path;
     ArrayList<String> where = new ArrayList<String>();
     int temp=0;
     try(BufferedReader br = new BufferedReader(new FileReader(file))) {
            for(String line; (line = br.readLine()) != null; ) {
                ++temp;
                if(temp<201){ //may be i need some changes here
                 dm.filewriter(line+" "+temp+")",outputfilename);
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
 }
 void filewriter(String linetoline,String filename) throws IOException{
     BufferedWriter fbw =null;
     try{

         OutputStreamWriter writer = new OutputStreamWriter(
               new FileOutputStream(filename, true), "UTF-8");
          fbw = new BufferedWriter(writer);
         fbw.write(linetoline);
         fbw.newLine();

     }catch (Exception e) {
         System.out.println("Error: " + e.getMessage());
     }
     finally {
         fbw.close();
        }
 }

}

一种方法可以使用if else,但我不能只使用它,因为我的实际文件是 6000+ 行。

我希望此代码能够像运行代码一样工作,并为我提供 30 多个输出文件。

【问题讨论】:

  • 这里可能在正确的轨道上。而不是 modulus arithmatic。

标签: java filestream file-handling


【解决方案1】:

您可以更改以下位:

if(temp<201){ //may be i need some changes here
    dm.filewriter(line+" "+temp+")",outputfilename);
}

到这里:

dm.filewriter(line, "D:\\output\\file-00" + ((temp/200)+1) + ".txt");

这将确保前 200 行转到第一个文件,接下来的 200 行转到下一个文件,依此类推。

此外,您可能希望将 200 行批处理并一次性写入,而不是每次都创建 writer 并写入文件。

【讨论】:

  • 您刚刚更改了要写入文件的数据。它与输出文件名无关。请再看看。
  • 让我告诉你这是如何工作的。我给了 1083 行 input.txt 文件。和代码创建了 200 个文本文件,每个文件包含 5 行。所以。 200x5= 1000 和 83 行,它们刚刚消失了
  • 好的,把%改成/,现在试试?
【解决方案2】:

您可能有一个方法可以创建Writer 到当前File,最多读取limit 行数,关闭Writer 到当前File,如果它返回true有足够的阅读量,false 如果它无法读取限制的行数(即中止下一次调用,不要尝试读取更多行或写入下一个文件)。

然后你会在循环中调用它,传递Reader、新文件名和限制号。

这是一个例子:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class DataMaker {
    public static void main(final String args[]) throws IOException {
        DataMaker dm = new DataMaker();
        String file = "D:\\input.txt";
        int roll = 1;
        String rollnum = null;
        String outputfilename = null;

        boolean shouldContinue = false;

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {

            do {

                rollnum = "file-00" + roll;
                outputfilename = "D:\\output\\" + rollnum + ".txt";
                shouldContinue = dm.fillFile(outputfilename, br, 200);
                roll++;
            } while (shouldContinue);

        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private boolean fillFile(final String outputfilename, final BufferedReader reader, final int limit)
            throws IOException {

        boolean result = false;
        String line = null;
        BufferedWriter fbw = null;
        int temp = 0;
        try {
            OutputStreamWriter writer = new OutputStreamWriter(
                    new FileOutputStream(outputfilename, true), "UTF-8");
            fbw = new BufferedWriter(writer);

            while (temp < limit && ((line = reader.readLine()) != null)) {

                temp++;
                fbw.write(line);
                fbw.newLine();

            }

            // abort if we didn't manage to read the "limit" number of lines
            result = (temp == limit);

        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        } finally {
            fbw.close();
        }

        return result;

    }

}

【讨论】:

    猜你喜欢
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2020-06-18
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多