Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Output problem (newbie)

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Output problem (newbie Q)

    Hello. I am learning java and tried to make a simple program, which reads the int number and writes it into a file. The problem is - the txt file remains empty. Take a look at the code:

    import java.io.*;
    import java.util.*;
     
    class Job {
    	int x;
     
    	public int inputTxt() {
    		try {
    			x = System.in.read();
    		} catch (Exception e) {
    			System.out.println("input error");	
    		}
    		return x;
    	}
     
    	public void outputF() {
    		try {
    			OutputStream f = new FileOutputStream("test.txt");
    			f.write(x);
    		} catch (Exception e) {
    			System.out.println("output error");	
    		}
    	}
     
    	public void closeF() {
    		try {
    			OutputStream f = new FileOutputStream("test.txt");
    			f.close();
    		} catch (Exception e) {
    			System.out.println("close error");	
    		}
    	}
    }
     
    class Test {
    	public static void main(String args [ ]) {
    		Job j = new Job();
    		j.inputTxt();
    		j.outputF();
    		j.closeF();
    	}
    }
    Last edited by Asido; June 8th, 2010 at 11:56 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Output problem (newbie)

    The f object in the close method is NOT the same as the f object in the write method.
    Close the output file after writing to it or have a reference to the object that was written to when closing it.
    txt file remains empty
    What is the size of the output file? Is it 0? What does the write() method output to the file?
    Look at the file with a hex editor.
    Last edited by Norm; June 8th, 2010 at 11:57 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Asido (June 8th, 2010)

  4. #3
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Output problem (newbie)

    Quote Originally Posted by Norm View Post
    The f object in the close method is NOT the same as the f object in the write method.
    Close the output file after writing to it or have a reference to the object that was written to when closing it.

    What is the size of the output file? Is it 0? What does the write() method output to the file?
    Look at the file with a hex editor.
    Ok, I go it, thanks a lot!
    Yes, the txt file had 0 b size.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Output problem (newbie)

    I got output (1 byte) when I ran your code (except I moved the close to the outputF() method).

  6. #5
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Output problem (newbie)

    Quote Originally Posted by Norm View Post
    I got output (1 byte) when I ran your code (except I moved the close to the outputF() method).
    I did the same and it solved the problem.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Output problem (newbie)

    I guess what happened is you created a file and wrote to it. Then in the closeF() method you created a new file with the same name and wrote nothing to it destroying the first version.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Asido (June 8th, 2010)

Similar Threads

  1. xml output problem
    By tsili in forum Java Theory & Questions
    Replies: 4
    Last Post: May 25th, 2010, 04:56 AM
  2. newbie GUI/ event handling problem
    By zyspt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 10th, 2010, 11:36 AM
  3. Newbie Programming problem
    By Pingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:10 AM
  4. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  5. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM