1

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

5 Answers 5

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();
Sign up to request clarification or add additional context in comments.

2 Comments

This was very helpful. Thanks! I don't really understand the for loop though. I'm used to the for(initialization; termination; increment). Why is it not String line = null; and there is not increment?
Are optional. You can have only for(;;)
1

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.

Comments

1

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())

2 Comments

Your two conditions are not equivalent. isEmpty() only checks for length, it does not trim previously
@GuillaumePolet - That was an oversight on my part. Added the null check as well!
0
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.

Comments

0

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();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.