yellavon
yellavon

Reputation: 2881

Java read in single file and write out multiple files

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

Upvotes: 1

Views: 10837

Answers (5)

Paul Vargas
Paul Vargas

Reputation: 42010

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

Upvotes: 1

ant
ant

Reputation: 22948

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

Upvotes: 0

CoolBeans
CoolBeans

Reputation: 20800

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

Upvotes: 1

Piyush Mattoo
Piyush Mattoo

Reputation: 16095

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.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168795

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.

Upvotes: 1

Related Questions