-
Writing to file
This module takes an int from a file, adds 10 to the amount in the txt file, then puts the results into another file.
I can get the results to the console but not to another file?
>>>bw.write(outLine);<<<
How can I get this to another file. Not sure why it is not processing?
farrago.txt == (1 3 4 5 7)
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("farrago.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("outagain.txt"));
String line=null;
int n=0;
while ( (line = br.readLine()) != null) {
String[] intStr = line.split(" "); // if ints are separated by space
String outLine = "";
for (int i = 0; i < intStr.length; i++) {
intStr[i] = intStr[i].trim();
String s=intStr[i];
try {
n = Integer.parseInt(s);
} catch (NumberFormatException nfe) {
System.out.println("the line: ["+line+"] failed integer conversion");
System.out.println("Failure for :["+intStr[i]+"]");
System.out.println(nfe.getMessage());
System.out.println("Element was skipped");
continue;
}
n=n+10;
outLine += (n) + " ";
}
System.out.println(outLine);
bw.write(outLine);
bw.newLine();
}
}
}
Thanks....
-
I can only see one thing missing....
I think you should close the files before the program terminates. So when
the read loop has finished you do:
bw.close();
br.close();
You should always do so when you are processing files, or you can (if you are
really in bad luck) get them pretty messed up.
PS:
This looks like a piece of code that I have been involved in, so if I forgot to
include the close statements I apologise (blush).
Last edited by sjalle; 02-27-2005 at 12:02 PM.
eschew obfuscation
-
Writing to file
sjalle, no need to apologize, I should have detected it as well, thanks for the help and advice. The close methods is what it needed.
bw.close();
br.close();
Thanks....!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks