Turn your Mobile Apps into m-commerce apps – Learn More!

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!



Empty output file Rate Topic: -----

#1 thepeon  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08


Dream Kudos: 0

Share |

Empty output file

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

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();
	}
}



Was This Post Helpful? 0
  • +
  • -


#2 itpro4470  Icon User is offline

  • D.I.C Head
  • Icon

Reputation: 2
  • View blog
  • Posts: 138
  • Joined: 17-June 07


Dream Kudos: 50

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

Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

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

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'" :snap:
Was This Post Helpful? 0
  • +
  • -

#4 thepeon  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08


Dream Kudos: 0

Re: Empty output file

Posted 29 November 2008 - 02:55 PM

Thanks for the help guys. One more question. The second time I call the write method, I'm trying to write to the end of the file, however, it's writing over the file instead.
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

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

// 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. :)
Was This Post Helpful? 0
  • +
  • -

#6 thepeon  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 04-February 08


Dream Kudos: 0

Re: Empty output file

Posted 29 November 2008 - 03:33 PM

Great!! I saw that at the Sun documentation site, but it didn't click that amend would tack it on to the end of the file.

Thanks again for all of your help.
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users