-
DataInputStream
The following code takes the contents of one file and copies it to another file.
How can I read the Ints from one file, add 10 to each int and put the results into another file as text?
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
FileInputStream inputFile = new FileInputStream("theints.txt");
DataInputStream in = new DataInputStream(inputFile);
FileOutputStream outputFile = new FileOutputStream("outagain.txt");
DataOutputStream out = new DataOutputStream(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
-
Use BufferedReader/BufferedWriter.
Use BufferedReader/BufferedWriter instead of DataInputStream for this.
The readInt() method of DataInputStream reads four bytes from the file
and interprets that as a four byte int, I don't think that is a format suited
for your .txt file.
Code:
BufferedReader br=new BufferedReader(new FileReader("c:\\tmp\\theints.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\tmp\\outagain.txt"));
String line=null;
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++) {
int n = Integer.parseInt(intStr[i]);
outLine += (n + 10) + " ";
}
bw.write(outLine);
bw.newLine();
}
br.close();
bw.close();
eschew obfuscation
-
DataInputStream
Thanks for the response. Good point on using the BufferReader. The program comples fine, but I get a runtime error(numberformatexception)... on line....
int n = Integer.parseInt(intStr[i]);?
Seems like it does not want to parse the elements in the array to ints?
Thanks....
-
Integer.parseInt(String s) throws numberFormatException when the contents of
s is not a stringrepresentation of an int. I have tested the code on an input file like this:
3 1 2
1 1 1 -1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
0
Remember, if the string has leading or trailing blanks. the parse fails, so a safe way is: Integer.parseInt(s.trim())
Last edited by sjalle; 02-24-2005 at 09:54 AM.
eschew obfuscation
-
DataInputStream
I'm still getting that format error?
Here is what I have...and used the data from your example.
BufferedReader br=new BufferedReader(new FileReader("intString.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("outagain.txt"));
String line=null;
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++) {
String s=intStr[i];
int n = Integer.parseInt(s.trim()); <<points to this line?
outLine += (n + 10) + " ";
bw.write(outLine);
bw.newLine();
...........................
not sure if I have the trim set up right, or what I'm not processing?
Thanks again...
Last edited by marks20101; 02-24-2005 at 05:11 PM.
Reason: Already posted thread
-
Try catching it
I strongly suspect that the stringsplit yields other stuff than just integer
strings. The trim() is not required in this case, since a split(" ") cannot yield
strings w. leading or trailing blanks as the blank is the split delimiter.
You should enclose the process in a try-catch to see what is happening:
Code:
BufferedReader br=new BufferedReader(new FileReader("intString.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("outagain.txt"));
String line=null;
while ( (line = br.readLine()) != null) {
String[] intStr = line.split(" "); // if ints are separated by space
String outLine = "";
int n;
for (int i = 0; i < intStr.length; i++) {
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;
}
outLine += (n + 10) + " ";
bw.write(outLine);
bw.newLine();
}
}
Also remember that parseInt(String s) fails for float and double values
(values w. a decimal point).
eschew obfuscation
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