Google Answers Logo
View Question
 
Q: java.io ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: java.io
Category: Computers > Programming
Asked by: hook80-ga
List Price: $13.32
Posted: 15 Mar 2006 05:47 PST
Expires: 14 Apr 2006 06:47 PDT
Question ID: 707535
Hi,
   Basically, below is a simple java script to copy a complete file
and produce an output file containing an exact copy of the original:

import java.io.*;

public class Copy {
    public static void main(String[] args) throws IOException {
File inputFile = new File("clust_2.0_Votes_Candidates_-1.scr");
File outputFile = new File("outagain.txt");

       FileReader in = new FileReader(inputFile);
       FileWriter out = new FileWriter(outputFile);
       int c;

       while ((c = in.read()) != -1)
          out.write(c);

       in.close();
       out.close();
    }
}


But my problem is that I need to 'selectively' copy only certain info
from the file.

E.g,


117 0.920765 (117 118 119 120 121)
109 0.640138 (109 110 111 112 113)
34 0.640138 (34 36)
35 0.640138 (35)
37 0.640138 (37)
97 0.615852 (97)
94 0.615852 (94 95 96)
52 0.615852 (52)
31 0.613872 (31)
76 0.592357 (76 77)
38 0.592357 (38 39)

I only want to copy the numbers on the immediate left. 117, 109, 34,
35, 37 etc and have them reproduced in an output file like this:

117
109
34
35
37
97
94
52
31
76
38

Please help, 
Thanks,
Hook
Answer  
Subject: Re: java.io
Answered By: answerguru-ga on 15 Mar 2006 08:04 PST
Rated:4 out of 5 stars
 
Hello Hook,

If you want to be able to parse the data in your input file, I suggest
using a BufferedReader. This will allow you to retrieve one line at a
time (rather than one character which you are currently doing). We can
then check for the first space on that line, grab everything before
it, and appending a new line character. Here is the modified code:

import java.io.*;

public class Copy {
    public static void main(String[] args) throws IOException {
File inputFile = new File("clust_2.0_Votes_Candidates_-1.scr");
File outputFile = new File("outagain.txt");

       BufferedReader in = new BufferedReader(new FileReader(inputFile));
       FileWriter out = new FileWriter(outputFile);
       String inData;
       String segment;

       while ((inData = in.readLine()) != null)
       {
          segment = inData.substring(0, inData.indexOf(" ")) + "\n";
          out.write(segment);
       }

       in.close();
       out.close();
    }
}

If you have any problems with the code above please post a
clarification and I will respond promptly.

Cheers,
answerguru-ga

Request for Answer Clarification by hook80-ga on 15 Mar 2006 08:27 PST
Hey guru,
          I'm a complete java novice so forgive my ignorance. Is what
you've suggested( "We can then check for the first space on that line,
grab everything before it, and appending a new line character.." )
what you've done? If so it looks good.
Just one thing, i understand up as far as:
FileWriter out = new FileWriter(outputFile);

Could you just explain what each of remaining the lines is doing?

Hook

Clarification of Answer by answerguru-ga on 15 Mar 2006 09:10 PST
Yes - what I described is what the code is doing.

The while loop will iterate through the input file until it reaches
the end. At each line, it will find the the part before the first
space and append a newline character to it. This string is then
written to the output file.

Hope that helps :)

answerguru-ga
hook80-ga rated this answer:4 out of 5 stars
Thanks alot - really helped me out!

Hook

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy