|
|
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 |
|
Subject:
Re: java.io
Answered By: answerguru-ga on 15 Mar 2006 08:04 PST Rated: |
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 | |
| |
|
hook80-ga
rated this answer:
Thanks alot - really helped me out! Hook |
|
There are no comments at this time. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |