-2

Suppose I have 105 records and I want write all records in text files where in each file there will be 10 records.

so total file will generate here 11.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
Avijit Das
  • 52
  • 4
  • 2
    Do you have some code? Just use a for-loop. For example `int maxCnt = 10; for(int i=0;i – killexe Aug 07 '15 at 05:19
  • What have you tried so far? In which way was the result of your attempts different from the requirement? – Hulk Aug 07 '15 at 05:28

3 Answers3

0
Try this

    int count = 0; String filename = "text1";
    for (int i = 0; i <= 105; i++) // loop to 105 times
    {
       count++;
       if(count <= 10) //write 10 records to the file
       {
            if(count == 1)
                filename = "text" + (i/10); 

            try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)))) {
            out.println("the text");

            }catch (IOException e) {}
            if(count == 10)
                count = 0;
       }
    }
Avijit Das
  • 52
  • 4
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • ...but please don't hardcode the limits. In any "real" code (no matter whether production or homework), this should be in a method, the input (some kind of collection, probably a List) and the number of entries per file should be parameters (probably the filename-pattern too). – Hulk Aug 07 '15 at 05:38
  • @Harshit , in file writing position i am written below code but it's creates only one file. File file = new File(filename+".txt"); // creates the file file.createNewFile(); // creates a FileWriter Object FileWriter writer = new FileWriter(file); writer.write("avijit"); // Writes the content to the file writer.flush(); writer.close(); – Avijit Das Aug 07 '15 at 06:12
  • @Harshit, Now it's creating 11 file but in each file there is only one record. – Avijit Das Aug 07 '15 at 06:20
  • Actually I dont have java installed on the system, So cannot run it. So just guessing the output. Are you really appending the file or just overwriting the previous file ? – Harshit Aug 07 '15 at 06:25
  • @Harshit, Every time i am creating new file.and then write into this file. – Avijit Das Aug 07 '15 at 06:30
0
public static void writeToMultipleFiles(int numberOfLines, String prefix, List<String> records){
    lineCounter = 1;
    counter = 1;
    String filename = prefix + counter; 
    for (String line : records){
        if(lineCounter = numberOfLines){
            lineCounter = 1;
            counter++;
            filename = prefix + counter;
        }
        try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)))) {
            out.println(line);
            lineCounter++;
        }catch (IOException e) {}

    }
}
Astrogat
  • 1,617
  • 12
  • 24
0
public static void writeToMultipleFilesExp() {
        int lineNumber = 0;
        int rowLimit = 10;
        String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
        String filename = null;
        PrintWriter out = null;
        for (int i = 0; i <= 105; i++) {
            lineNumber++;
            try {
                if (lineNumber <= rowLimit) {
                    if (lineNumber == 1) {
                        filename = "C:\\temp\\sep\\file_" + timeStamp + "_" + + (i / rowLimit) + ".txt";
                        out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
                    } 
                    out.println("the text");                    
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (lineNumber == rowLimit || i==105) { // i==105 is needed or else last page will not be rendered.
                    lineNumber = 0;
                    out.close();
                }               
            }           
        }
    }
vivek4348
  • 435
  • 1
  • 5
  • 15