Author |
Message |
MysticVegeta
|
Posted: Sun Nov 27, 2005 3:11 pm Post subject: IO problems... |
|
|
I am using Ready and the Program is not writing to the file. It read integers 1, 2, 3 from a text file. Also, Why cant i read them as ints?
code: | import java.io.*;
//import java.lang.String;
public class ioExceptions
{
public static void main (String[] Args)
{
String nums;
try
{
BufferedReader inputStream = new BufferedReader (new FileReader ("Database.txt"));
BufferedWriter out = new BufferedWriter (new FileWriter ("Out.txt"));
for (int x = 1 ; x < 4 ; x++)
{
nums = inputStream.readLine ();
System.out.println (nums);
out.write (nums + "2");
}
}
catch (IOException asdf)
{
System.out.println ("FILE NOT FOUND");
}
}
}
|
Thanks a lot. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Hikaru79
|
Posted: Sun Nov 27, 2005 4:58 pm Post subject: (No subject) |
|
|
The thing with BufferedWriters is that if you don't fill the buffer, it doesn't get written to the file and just stays on the stream.
Basically, at the end of the program, write these two lines: Java: | out.flush();
out.close();
|
And it will work just fine
NOTE: Be sure you don't put those lines in the loop, or else it will close the stream before you're done writing. |
|
|
|
|
|
JackTruong
|
Posted: Sun Nov 27, 2005 7:11 pm Post subject: (No subject) |
|
|
Java: | int num = Integer. parseInt(inputStream. readLine()); |
|
|
|
|
|
|
wtd
|
Posted: Sun Nov 27, 2005 7:25 pm Post subject: (No subject) |
|
|
Or if you're using Java 1.5/5.0/numberOfTheWeek:
Java: | import java.util.Scanner;
...
Scanner inputStreamScanner = new Scanner(inputStream);
...
int num = inputStreamScanner.nextInt(); |
|
|
|
|
|
|
1of42
|
Posted: Sun Nov 27, 2005 7:36 pm Post subject: (No subject) |
|
|
Hikaru79 wrote: The thing with BufferedWriters is that if you don't fill the buffer, it doesn't get written to the file and just stays on the stream.
Basically, at the end of the program, write these two lines: Java: | out.flush();
out.close();
|
And it will work just fine
NOTE: Be sure you don't put those lines in the loop, or else it will close the stream before you're done writing.
The flush is unnecessary, close() flushes the stream. |
|
|
|
|
|
MysticVegeta
|
Posted: Sun Nov 27, 2005 8:05 pm Post subject: (No subject) |
|
|
JackTruong wrote: Java: | int num = Integer. parseInt(inputStream. readLine()); |
Why need to parse when it already exists in Integer format in the text file :S |
|
|
|
|
|
wtd
|
Posted: Sun Nov 27, 2005 8:10 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: JackTruong wrote: Java: | int num = Integer. parseInt(inputStream. readLine()); |
Why need to parse when it already exists in Integer format in the text file :S
Does the text file know that it's an int? |
|
|
|
|
|
MysticVegeta
|
Posted: Mon Nov 28, 2005 12:07 am Post subject: (No subject) |
|
|
Apparently turing does. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Mon Nov 28, 2005 1:38 am Post subject: (No subject) |
|
|
No, it doesn't. It assumes that if you ask it to read an integer, the next thing will be an integer.
What if it's not?
Well, then you've read a bunch of data from that stream for nothing. Can you get that data back and treat it differently? Yes, but not easily.
If I read in a string, then parse it, if there's an error, I still have the string, and I can still do something else with it. |
|
|
|
|
|
MysticVegeta
|
Posted: Mon Nov 28, 2005 2:08 pm Post subject: (No subject) |
|
|
I guess you are right. ok after reading in the string "1 2 3" I will use the stringsplit to store it into an array of ints I dont suppose there is a shorter way to do that. Do you think? |
|
|
|
|
|
wtd
|
Posted: Mon Nov 28, 2005 2:34 pm Post subject: (No subject) |
|
|
Again, if we're talking about Java 1.5.0...
Java: | String line = inputStream. readLine();
Scanner lineScanner = new Scanner (line );
ArrayList<Integer> numbers = new ArrayList<Integer> ();
for (int i = 0; i < 3; ++i )
{
numbers. add(lineScanner. nextInt());
} |
|
|
|
|
|
|
wtd
|
Posted: Mon Nov 28, 2005 8:07 pm Post subject: (No subject) |
|
|
Or, if you're not sure how many numbers there are in your line:
Java: | String line = inputStream. readLine();
Scanner lineScanner = new Scanner (line );
ArrayList<Integer> numbers = new ArrayList<Integer> ();
while (lineScanner. hasNextInt())
{
numbers. add(lineScanner. nextInt());
} |
|
|
|
|
|
|
|