inputting and outputing from txt file

bigbri74
bigbri74 used Ask the Experts™
on
can somone post an simple program that uses a text file to imput data and then have the program output data to seperate text files? thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this, I compiled it and it works fine..
you can specify your conditions to write to diff files... I specified a simple scenario..
 **************************************

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

public class Sample
{
     private String inFile, outFile1, outFile2;
     private Vector inputVec;

     public static void main(String args[])
     {
          Sample sample = new Sample();
          sample.readFile();
          sample.writeFile();
     }

     public Sample()
     {
          inputVec = new Vector();
          inFile = "test.in";
          outFile1 = "test1.out";
          outFile2 = "test2.out";
     }

     // this method reads the file and calls a method to parse the line read from file
     public void readFile()
     {
          String line, data;
          BufferedReader buffReader;

          try
          {
               buffReader = new BufferedReader( new FileReader( inFile ) );
               buffReader.ready(); //this tells whether the stream is ready or not
               while( (line = buffReader.readLine()) != null )
              {

                         data = parseString( line );    //read it and save it here
                         System.out.println(data);
                         inputVec.add(data);
              }     // end of while
          buffReader.close();
          }
          catch ( FileNotFoundException e )
          {
               System.out.println(" This file, " + inFile + " ,does not exists " );

          }
          catch ( Exception e )
          {
               e.printStackTrace();
          }
         
     }

     public void writeFile()
     {
          String data;
          PrintWriter printWriter1, printWriter2;

          try
          {
               printWriter1   = new PrintWriter(new BufferedWriter(new FileWriter(outFile1)));
               printWriter1.flush();
               printWriter2   = new PrintWriter(new BufferedWriter(new FileWriter(outFile2)));
               printWriter2.flush();
               System.out.println("out..");
               for(int i = 0; i < inputVec.size(); i++)
               {
                    if(i%2 == 0)
                    {
                    printWriter1.println((String)inputVec.get(i));
                    }
                    else
                    {
                    printWriter2.println((String)inputVec.get(i));
                    }
                   
               }

               printWriter1.close();
               printWriter2.close();

          }
          catch(Exception e)
          {
               e.printStackTrace();
          }

       }

     public String parseString( String line )
     {
          // do any parsing here if you want to...
          return line;
     }

}     // end of class

Commented:
Dear vemul


I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.

See:        http://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002

By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.


EXPERTS: I will return in seven days to close this question. Please leave your thoughts and recommendations here


Thanks !

Jgould

Community Support Moderator
Experts Exchange

Commented:
Hi Jgould,
 It was my first try in answering questions at EE and I didn't know that I wasn't supposed to propose an answer and lock it. I never it do anymore :-)

Thanks,
vemul

Commented:
vemul, no worry since you posted such a complete comment i think the answer is yours anyway next week when i will close them all that are being opened up now, it took the asker almost 2 months to evaluate your answer

Jgould-EE Moderator

Commented:
Question has been closed as force accepted

JGould-EE Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial