Java read in single file and write out multiple files

寵の児 提交于 2020-01-15 12:44:27

问题


I want to write a simple java program to read in a text file and then write out a new file whenever a blank line is detected. I have seen examples for reading in files but I don't know how to detect the blank line and output multiple text files.

fileIn.txt:

line1
line2

line3

fileOut1.txt:

line1
line2

fileOut2.txt:

line3

回答1:


Just in case your file has special characters, maybe you should specify the encoding.

FileInputStream inputStream = new FileInputStream(new File("fileIn.txt"));
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader reader = new BufferedReader(streamReader);
int n = 0;
PrintWriter out = new PrintWriter("fileOut" + ++n + ".txt", "UTF-8");
for (String line;(line = reader.readLine()) != null;) {
    if (line.trim().isEmpty()) {
        out.flush();
        out.close();
        out = new PrintWriter("file" + ++n + ".txt", "UTF-8");
    } else {
        out.println(line);
    }
}
out.flush();
out.close();
reader.close();
streamReader.close();
inputStream.close();



回答2:


I don't know how to detect the blank line..

if (line.trim().length==0) { // perform 'new File' behavior

.. and output multiple text files.

Do what is done for a single file, in a loop.




回答3:


You can detect an empty string to find out if a line is blank or not. For example:

if(str!=null && str.trim().length()==0)

Or you can do (if using JDK 1.6 or later)

if(str!=null && str.isEmpty())



回答4:


BufferedReader br = new BufferedReader(new FileReader("test.txt"));
String line;
int empty = 0;
while ((line = br.readLine()) != null) {
if (line.trim().isEmpty()) {
 // Line is empty
 }
}

The above code snippet can be used to detect if the line is empty and at that point you can create FileWriter to write to new file.




回答5:


Something like this should do :

public static void main(String[] args) throws Exception {
        writeToMultipleFiles("src/main/resources/fileIn.txt", "src/main/resources/fileOut.txt");
    }

    private static void writeToMultipleFiles(String fileIn, String fileOut) throws Exception {      

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileIn))));
        String line;
        int counter = 0;
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileOut))));

        while((line=br.readLine())!=null){

            if(line.trim().length()!=0){
                wr.write(line);
                wr.write("\n");
            }else{
                wr.close();
                wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOut + counter)));
                wr.write(line);
                wr.write("\n");
            }
            counter++;
        }

        wr.close();
    }


来源:https://stackoverflow.com/questions/10543053/java-read-in-single-file-and-write-out-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!