Question

How to read one line at a time from a file and write to different files?

Would like to know how to read one line at a time and write to different files. Can't find good example when googling:

For instance, read a text file with following content: sent1 sent2 sent3 ....

write sent1 to 1.txt, sent2 to 2.txt, sent3 to 3.txt

It seems to be a simple operation, There is something wrong with my coding and not sure how to fix it.

import java.util.*;
import java.io.*;

public class MyFunction {
public static void main(String args[])
throws IOException, ClassNotFoundException
{


   //variable initialization

  String strLine;
  int i = 0;
 //Read in a file and process one line at a time

   FileReader singleFile = new FileReader("C:\\data);
   BufferedReader reader = new BufferedReader(singleFile);
    for (int j=0; j < 12; j++) {
   FileWriter outFile = new FileWriter("C:\\doc" + i + ".txt");
   BufferedWriter writer = new BufferedWriter(outFile);     

    //Read one line at a time from the input file
   while ((strLine = reader.readLine()) !=null) {
   //process a string which is not empty

   if (strLine.length() > 0) {

   System.out.println("This is strLine: " + strLine);
   writer.write (strLine);

   i=i+1;
   System.out.println("This is i: " + i);

   } //end of if
   }


   singleFile.close();
   writer.close();

   } }}
Was it helpful?

Solution 3

It works with the following codes. Since I know there are only 12 lines in the doc - would like to know how to find out the total line number in the input file.

   import java.util.*;
   import java.io.*;



  public class MyFunction {
 public static void main(String args[])
 throws IOException, ClassNotFoundException
 {


 //variable initialization

String strLine;

 //Read in a file and process one line at a time

   FileReader singleFile = new FileReader("C:\\data\\data.txt");  
   BufferedReader reader = new BufferedReader(singleFile);

    for (int i=0; i<12; i++) {
    FileWriter outFile = new FileWriter("C:\\sample\\doc" + i + ".txt");
    BufferedWriter writer = new BufferedWriter(outFile);     

    //Read one line at a time from the input file
    if ((strLine = reader.readLine()) !=null) 
    //process a string which is not empty

    if (strLine.length() > 0) {

    System.out.println("This is strLine: " + strLine);
    writer.write (strLine);

    System.out.println("This is i: " + i);

    } //end of if


  singleFile.close();
  writer.close();

    } }}

OTHER TIPS

The easiest approach is probably to open n FileWriters, one on each output file, in an array (I'll call the array "writers"). Set a counter i = 0. As you iterate over the input lines, write the current line to writers[i], then increment i and set i = i%n. This will loop i over the range 0..n-1, which means you'll write to the files in the sequence 0, 1, ... n-1, 0, 1... which I think is what you want.

You are stuck in your while look until the file is done. Take the while loop out and replace it with an if that breaks out of the for loop. That will get you the first 12 lines only. After that change your for loop to a while loop and manually increment j, if j >= 12, set j to 0.

The code below loops over 12 files, is that what you wanted?

import java.util.*;
import java.io.*;

public class MyFunction {
public static void main(String args[])
throws IOException, ClassNotFoundException
{


   //variable initialization

  String strLine;
  int i = 0;
   //Read in a file and process one line at a time

   FileReader singleFile = new FileReader("C:\\data);
   BufferedReader reader = new BufferedReader(singleFile);
   j = 0;
   while (true) { //loop forever
   FileWriter outFile = new FileWriter("C:\\doc" + j + ".txt");
   BufferedWriter writer = new BufferedWriter(outFile);     

    //Read one line at a time from the input file
   if ((strLine = reader.readLine()) !=null) { break; } // exit loop
   //process a string which is not empty

   if (strLine.length() > 0) {

   System.out.println("This is strLine: " + strLine);
   writer.write (strLine);

   i=i+1;
   j=j+1;
   if (j >= 12) {j=0};
   System.out.println("This is i: " + i);

   } //end of if


   singleFile.close();
   writer.close();

   } }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top