Narayanan
Narayanan

Reputation: 43

Java - File manipulation

I have some content in an input file a.txt as

Line 1 : "abcdefghijk001mnopqr hellohello"
Line 2 : "qwertyuiop002asdfgh welcometologic"
Line 3 : "iamworkingherefromnowhere002yes somethingsomething"
Line 4 : "thiswillbesolved001here ithink"

I have to read the a.txt file and write it to two separate files. ie., lines having 001 should be written to output1.txt and lines having 002 should be written to output2.txt

Can someone help me on this with a logic in Java programming.

Thanks, Naren

Upvotes: 1

Views: 713

Answers (3)

Andy Ecca
Andy Ecca

Reputation: 1969

Code complete.

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jfile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;

/**
 *
 * @author andy
 */
public class JFile {

    /**
     * @param args the command line arguments
     */

    static File master = null,
                m1     = null, // file output with "001"
                m2     = null; // file output with "002"

    static FileWriter fw1,
                      fw2;

    static FileReader fr = null;
    static BufferedReader br = null;

    public static void main(String[] args) throws FileNotFoundException, IOException
    {

         String root = System.getProperty("user.dir") + "/src/files/";

         master = new File ( root + "master.txt" );
         m1     = new File ( root + "m1.txt");
         m2     = new File ( root + "m2.txt");

         fw1     = new FileWriter(m1, true);
         fw2     = new FileWriter(m2, true);


         fr = new FileReader (master);
         br = new BufferedReader(fr);

         String line;
         while((line = br.readLine())!=null)
         {
             if(line.contains("001")) 
             {               
                 fw1.write(line + "\n");

             } else if (line.contains("002"))
             {
                 fw2.write(line + "\n");

             }
         }

         fw1.close();
         fw2.close();
         br.close();
    }

}

Project Netbeans : http://www.mediafire.com/?yjdtxj2gh785cyd

Upvotes: 0

vels4j
vels4j

Reputation: 11298

Here is a good example for Reading and writing text files using Java and checking conditions do the following

while ((line = reader.readLine()) != null) {
    //process each line in some way
    if(line.contains("001") {
     fileWriter1.write(line);  
    } else if (line.contains("002") ) {
     fileWriter2.write(line);    
    }
  } 

Upvotes: 0

Aubin
Aubin

Reputation: 14853

BufferedReader br = new BufferedReader( new FileReader( "a.txt" ));
String line;
while(( line = br.readLine()) != null ) {
    if( line.contains( "001" )) sendToFile001( line );
    if( line.contains( "002" )) sendToFile002( line );
}
br.close();

The method sendToFile001() and sendToFile002() write the parameter line as follow:

ps001.println( line );

with ps001 and ps002 of type PrintStream, opened before (in a constructor?)

Upvotes: 2

Related Questions