Generating multiple files from one text file in java

I have one file input.txt

which is let say 520 lines. I have to make a code in java that will act like this.

Create the first file named file-001.txt

with the first 200 lines. then create another one file-002

of 201-400 lines. then file-003.txt

from the remaining lines.

I coded this, it just wrote the first 200 lines. What changes do I need to make to update it to work on the above scenario.

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

}

      

One way can be used if else

, but I cannot use it because my actual file is 6000+ lines.

I want this code to work as I run the code and give me 30+ output files.

+3


source to share


2 answers


You can change the following bit:

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

      

This will cause the first 200 lines to jump to the first file, the next 200 lines to jump to the next file, and so on.

Also, you can concatenate 200 lines and write them in one go, rather than create writer

each time and write to a file.

+1


source


You can have a method that creates Writer

for the current one File

, reads up to the limit

number of lines, closes Writer

up to the current one File

, and then returns true

if it was enough to read false

, if it could not read the limit on the number of lines (i.e. abort the next call, don't try to read more lines or write the next file).

Then you call this in a loop, passing Reader

in the new filename and limit number.



Here's an example:

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;

    }

}

      

0


source







All Articles