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: File IO Compiles, but wont work?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File IO Compiles, but wont work?

    I wrote this code and compiled it in eclipse I have a text file hello.txt in the same src folder as the .class it does compile properly but wont append the Dirk Benson to the file I have no idea what is wrong.


    import java.io.*;
     
    public class ioappend 
    {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws IOException
     
    	//TODO make try/catch for in-thread error catching
     
    	{
    		String filename;
    		BufferedReader input = new BufferedReader
    			( new InputStreamReader(System.in));//TODO testing only 
     
    		System.out.println("Enter a file name: ");//TODO testing only 
     
    		filename = input.readLine();//TODO testing only 
     
    		FileWriter inject = new FileWriter( filename, true);
     
    		inject.write("Dirk Benson");
     
    		inject.close();
     
     
    	}
     
    }
    Last edited by copeg; February 1st, 2011 at 09:56 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: File IO Compiles, but wont work?

    Are any exceptions thrown when you run it? Where is the file you want to append, and what do you enter?

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File IO Compiles, but wont work?

    the file I want to append to is in the same package as the .class file, hello.txt. I want to simply append some string of text I put dirk benson in there for right now. Thanks

    I did a try catch block no errors shown
    Last edited by StarKannon; February 1st, 2011 at 10:17 PM.

  4. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: File IO Compiles, but wont work?

    I compiled your code its running absolutely fine.

    appending the content.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: File IO Compiles, but wont work?

    Hello StarKannon.

    I have tested this code and it works fine.

    Try putting the hello.txt file in the package folder. The same location as the src and bin folders.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #6
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: File IO Compiles, but wont work?

    Try putting the hello.txt file in the package folder. The same location as the src and bin folders.
    if your file hello.txt is in another location , specify the absolute path to that file.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 02:58 PM
  2. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 04:40 PM
  3. making a .bat file that compiles your java files
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2010, 12:55 AM
  4. Why wont the Button wrap?
    By Taylorleemusic in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 1st, 2010, 12:03 AM
  5. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM