Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By Games2Design

Thread: Writing to File using FileWriter issue

  1. #1
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Writing to File using FileWriter issue

    SOLVED:
    Lesson of day: Remember that when you write to a file - the file created will be located at where your classes are at.









    I've used a school package to write to file, and also a method in that package to format stuff. So i am not familiar with FileWriter. The first problem i have is that even though i compiled and ran the program, the text file "myfile.txt" is still blank. There is nothing written onto it.

    The second problem which is formatting can wait for now.

    Java Code:
    import java.util.*;
    import java.io.*;
    
    class PrintToFile {
        public static void main(String[] args) {
    	Scanner in=new Scanner(System.in);
    
    	try{
    	    FileWriter file= new FileWriter("myfile.txt");
    	    //Formatter fm= new Formatter(fil);
    	    file.write('A');
    	    file.write("Canis familiaris");
    	    file.write(15);
    	    //fm.format("%1.2f",3.1415);
    
    
    file.close();
    	}catch(Exception e){
    
    
    
    	}
        }
    }
    Last edited by Games2Design; 10-01-2012 at 04:30 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,856
    Rep Power
    19

    Default Re: Writing to File using FileWriter issue

    Never have empty catch blocks.
    AT the very least do a 'e.printStackTrace()' in there, otherwise you will have no idea whether an exception was thrown.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by Tolls View Post
    Never have empty catch blocks.
    AT the very least do a 'e.printStackTrace()' in there, otherwise you will have no idea whether an exception was thrown.
    Ok, i entered a System.out.println setence inside the catch blocks - so it's no longer empty.
    Don't know what e.printStackTrace() does.

    Anyhow the problem i have has not been solved.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by Games2Design View Post
    Don't know what e.printStackTrace() does.
    It does what the API says it does.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by DarrylBurke View Post
    It does what the API says it does.

    db
    I unsderstand the words individually, but together i don't. Especially as i have never used e.printStackTrace().
    But me understanding that is really not what i need help with :S

    EDIT:
    I am pretty pissed. I used FileWriter AND PrintWriter, and it seems i have done everything correct, yet nothing is written onto the file :/

    file name given in code same as the actual name of the file = check

    closed file = check.

    e.printStackTrace(); = check



    So what am i doing wrong -.- If i use the school package to write to file, i would be done by now. But i want to (and need to) learn to use java's own packages.
    Last edited by Games2Design; 10-01-2012 at 03:13 PM.

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Writing to File using FileWriter issue

    Does the myfile.txt even exist?

  7. #7
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by PhQ View Post
    Does the myfile.txt even exist?
    Yes, it's located at my documents.

    Writing to File using FileWriter issue-myfile.png

  8. #8
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by Games2Design View Post
    Yes, it's located at my documents.

    Click image for larger version. 

Name:	myfile.png 
Views:	7 
Size:	4.9 KB 
ID:	4126
    Nevermind. I figured out that it creates the file.
    Your program works perfectly for me.
    I get this:
    ACanis familiaris
    in the text file.

  9. #9
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by PhQ View Post
    Nevermind. I figured out that it creates the file.
    Your program works perfectly for me.
    I get this: in the text file.
    Exactly...why is it not working for me? myfile.txt is located at "my documents". I checked it after running the program - file empty.
    Thank you for trying the program on your side. Now i hope i can figure out why it's not working for me...

  10. #10
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by Games2Design View Post
    Exactly...why is it not working for me? myfile.txt is located at "my documents". I checked it after running the program - file empty.
    Thank you for trying the program on your side. Now i hope i can figure out why it's not working for me...
    Delete the file and execute your program again.
    I have a feeling that you are looking at the wrong directory.
    Do:
    Java Code:
    File f = new File("myfile.txt");
    System.out.println(f.getAbsolutePath());
    And look at the path. Is it in the My Documents folder?

  11. #11
    Games2Design is offline Member
    Join Date
    Sep 2012
    Posts
    44
    Rep Power
    0

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by PhQ View Post
    Delete the file and execute it again.
    I have a feeling that you are looking at the wrong directory.
    hahhahah i knew it! (a thought just hit me) The file created was saved somewhere else.

    But what was weird is that when i searched for file name - it didn't appear.

    Example i made a new txt file, then searched for it's name (and nothing appeared as result). (i must have made a mistake when searching)

    And when you told me to delete and execute again, i thought "My java classes are found at x directory - perhaps the files are saved there?".

    I checked the directory where my classes are - and booom the txt files were there, and the files had text written into them.



    Thank you for your help!
    PhQ likes this.

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Writing to File using FileWriter issue

    Quote Originally Posted by Games2Design View Post
    hahhahah i knew it! (a thought just hit me) The file created was saved somewhere else.

    But what was weird is that when i searched for file name - it didn't appear.

    Example i made a new txt file, then searched for it's name (and nothing appeared as result). (i must have made a mistake when searching)

    And when you told me to delete and execute again, i thought "My java classes are found at x directory - perhaps the files are saved there?".

    I checked the directory where my classes are - and booom the txt files were there, and the files had text written into them.



    Thank you for your help!
    I'm glad I could help you :)

Similar Threads

  1. Writing conditional tab separated values using FileWriter
    By ravi.joshi53 in forum Advanced Java
    Replies: 10
    Last Post: 09-09-2012, 11:33 AM
  2. FileWriter issue
    By victorgb in forum New To Java
    Replies: 4
    Last Post: 04-18-2012, 09:31 PM
  3. File Writing Issue - Only Writes First Line
    By DragonGhola in forum Advanced Java
    Replies: 4
    Last Post: 07-22-2011, 04:41 PM
  4. write to file with filereader/filewriter
    By LennyKosmos in forum New To Java
    Replies: 3
    Last Post: 10-27-2010, 08:12 PM
  5. issue with FileReader and FileWriter
    By Uma R in forum Advanced Java
    Replies: 6
    Last Post: 07-16-2010, 01:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •