import java.io.*;
import java.text.*;
import java.util.*;
public class NumbersTodat {
File numbers = new File("numbers.dat");
private static void write(int start) {
try {
FileOutputStream fout = new FileOutputStream("numbers.dat");
for(int i = start; i == 99 || i == 100; i = i + 2) {
new PrintStream(fout).println(i);
}
fout.close();
}
catch(IOException e) {
System.err.println("Unable to write to file");
System.exit(-1);
}
}
private static void output() {
try {
FileInputStream fin = new FileInputStream("numbers.dat");
while(fin.available() != -1) {
System.out.println(fin.read());
}
fin.close();
}
catch (IOException e) {
System.err.println("Unable to read from file");
System.exit(-1);
}
}
public static void main(String args[]) {
write(2);
output();
write(1);
output();
}
}
You're Browsing As A Guest! Register Now... |
||
|
Become a Java Expert!
Join 414,938 Java Programmers for FREE! Get instant access to thousands
of Java experts, tutorials, code snippets, and more! There are 2,691 people online right now.Registration is fast and FREE... Join Now!
|
||
Page 1 of 1
Empty output file
#1
Empty output file
Posted 29 November 2008 - 11:13 AM
I'm trying to output numbers to a file, then load them back to the console. I'm ending up with an empty file and an output of an infinite loop of -1. I can't seem to find my errors.
#2
Re: Empty output file
Posted 29 November 2008 - 11:40 AM
private static void write(int start) {
try {
FileOutputStream fout = new FileOutputStream("numbers.dat");
for(int i = start; i == 99 || i == 100; i = i + 2) {
new PrintStream(fout).println(i);
}
fout.close();
}
catch(IOException e) {
System.err.println("Unable to write to file");
System.exit(-1);
}
}
Let's say I called write(4)... in you're for loop's first statement i == 4 but in the conditional statement i has to equal 99 or 100 so the for loop would quit without iterating. Unless the number is either 99 or 100 you'll have an empty file. Try changin the for loop to something like.
for(int i = 0;i<start;i++)
{
new PrintStream(fout).println(i);
}
This post has been edited by itpro4470: 29 November 2008 - 11:44 AM
#3
Re: Empty output file
Posted 29 November 2008 - 11:57 AM
Few issues here. Typically you don't use fileinputstream and fileoutputstream directly. These objects are the base classes for more user friendly and more powerful classes. Such objects are the BufferedReader and BufferedWriter classes as well as PrintWriter etc. These classes use readers (which are types of file input and output streams) to handle writing and reading to files.
So using these and a few other changes to your code, you can get this up and working. Below is a working example...
So read through the code and the in-code comments to see where I have made changes to get this up and running for you. Hope you get the idea now and can make additional changes to make the program all your own. Enjoy!
"At DIC we be file buffering and writing code ninjas... In knight online I am a magical paladin which they too call a 'buffer'"
So using these and a few other changes to your code, you can get this up and working. Below is a working example...
import java.io.*;
import java.text.*;
import java.util.*;
public class NumbersTodat {
private static void write(int start) {
try {
// Use a bufferedwriter to wrap around a filewriter and use this for writing. Output and input streams are basic underlying classes
// used by these more user friendly and bigger classes.
PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat")));
// If you pass in 2, you weren't in your condition so the for loop was never executing. Here we check if it is less than 100 and
// since 2 is, it will go into the for loop and start the writing.
for(int i = start; i <= 100; i = i + 2) {
fout.println(i);
}
fout.close();
}
catch(IOException e) {
System.err.println("Unable to write to file");
System.exit(-1);
}
}
private static void output() {
try {
// Here we use a bufferedReader that wraps around a FileReader
BufferedReader fin = new BufferedReader(new FileReader("numbers.dat"));
String outputline = "";
// Read a line into our string and check if it is not null. Then write it.
// Before you were just checking if the file was available and since your file had nothing, the read was returning -1
// which is why it was in an infinite loop as well as printing -1.
while((outputline = fin.readLine()) != null) {
System.out.println(outputline);
}
fin.close();
}
catch (IOException e) {
System.err.println("Unable to read from file");
System.exit(-1);
}
}
public static void main(String args[]) {
write(2);
output();
write(1);
output();
}
}
So read through the code and the in-code comments to see where I have made changes to get this up and running for you. Hope you get the idea now and can make additional changes to make the program all your own. Enjoy!
"At DIC we be file buffering and writing code ninjas... In knight online I am a magical paladin which they too call a 'buffer'"
#5
Re: Empty output file
Posted 29 November 2008 - 02:59 PM
In my example if you add a second parameter to the FileWriter constructor of "true" it will append to the end of the file when you write instead of writing over the top.
Example...
That should cause the writing to append right to the end of the file on each write.
Example...
// Notice the "true" in this Line.
PrintWriter fout = new PrintWriter(new BufferedWriter(new FileWriter("numbers.dat", true)));
That should cause the writing to append right to the end of the file on each write.
Page 1 of 1


Ask A New Question
Reply





MultiQuote






|