3

I have one input.txt file which consist on let suppose 520 lines. I have to make a code in java which will act like this.

Create first file named file-001.txt from first 200 lines. then create another file-002 from 201-400 lines. then file-003.txt from remaining lines.

I have coded this, it just write first 200 lines. What changes I need to make in order to update its working to 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 use of if else but I cant just use it because my actual file is 6000+ lines.

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

ALI
  • 339
  • 3
  • 11
Adnan Ali
  • 2,851
  • 5
  • 22
  • 39
  • Probably on the right track here. Instead of < 201, look at [modulus arithmatic](http://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java). – Wonko the Sane Apr 21 '17 at 13:59

2 Answers2

1

You can change the following bit:

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

to this:

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

This will make sure first 200 lines go to first file, next 200 lines go to next file and so on.

Also, you might want to batch 200 lines together and write them in one go rather than creating a writer everytime and write to file.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • you just changed the data which is going to be written in file. it have nothing to do with output file name. kindly take a look again. – Adnan Ali Apr 18 '17 at 12:35
  • Let me tell you how this is working. I gave 1083 lines input.txt file. and code created 200 text files each containing 5 lines. so. 200x5= 1000 and 83 lines they just vanished – Adnan Ali Apr 18 '17 at 13:00
  • Okay, changed `%` to `/`, try now? – Darshan Mehta Apr 18 '17 at 13:03
0

You may have a method that creates the Writer to the current File, reads up to limit number of lines, closes the Writer to the current File, then returns true if it had enough to read , false if it couldn't read the limit number of lines (i.e, abort next call, don't attempt to read more lines or write next file).

Then you would call this in a loop , passing the Reader, the new file name, and the limit number.

Here is 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;

    }

}
Arnaud
  • 17,229
  • 3
  • 31
  • 44