4 Replies - 895 Views - Last Post: 14 March 2011 - 07:39 AM Rate Topic: -----

#1 J_BIZZLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-November 09

parsing records from a csv file and writing them to a text file

Posted 14 March 2011 - 06:29 AM

I am trying to parse records from a csv file and write them to a text file. At the minute my program reads all the records and prints them all to screen but I can only get the program to write the last record found to the text file.

Any ideas on how to print the entire contents to the text file??


 
 try {
            //locastion of file
            FileInputStream inputLocation = new FileInputStream(callLogInputLocation);
            // Get the data from the file
            DataInputStream dataFromFile = new DataInputStream(inputLocation);
            BufferedReader lineReader = new BufferedReader(new InputStreamReader(dataFromFile));
            String textOnLine;
            String[] textFromLine = new String[10];
            //Read File Line By Line and while the line is not blank
            while ((textOnLine = lineReader.readLine()) != null) {
                try {
                    // Create file to output data to
                    FileWriter writeToFile = new FileWriter(fileOutputLocation);
                    BufferedWriter outputText = new BufferedWriter(writeToFile);

                    outputText.write(textOnLine);
                    
                   //Close the output stream
                    outputText.close();
                } catch (Exception e) {//Catch exception if any
                    errorCount++;
                }                
                // Print the content on the console
                System.out.println(textOnLine);
            }
            //Close the input stream
            dataFromFile.close();
        } catch (Exception e) {//Catch exception if any
            JOptionPane.showMessageDialog(this, "Please select an input file",
                    "Input Error", JOptionPane.ERROR_MESSAGE);
        }
        if (errorCount > 0) {
            JOptionPane.showMessageDialog(this, "Please select an output location",
                    "Input Error", JOptionPane.ERROR_MESSAGE);
        }




Thanks in advance!

J

Is This A Good Question/Topic? 0
  • +

Replies To: parsing records from a csv file and writing them to a text file

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: parsing records from a csv file and writing them to a text file

Posted 14 March 2011 - 06:53 AM

You open the file for every iteration of the loop.
create the file outside the while loop (before reading it).
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: parsing records from a csv file and writing them to a text file

Posted 14 March 2011 - 06:54 AM

That's because for every text line you read from the file you are creating a new output file. So only the last line read is preserved.

Move the output file creation outside the while() loop.
Was This Post Helpful? 0
  • +
  • -

#4 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: parsing records from a csv file and writing them to a text file

Posted 14 March 2011 - 07:19 AM

Psst, a CSV file is a text file. ;-)

This post has been edited by masijade: 14 March 2011 - 07:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 J_BIZZLE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-November 09

Re: parsing records from a csv file and writing them to a text file

Posted 14 March 2011 - 07:39 AM

Thanks for the help guys. I've got it working now.
J
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1