|
-
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....
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