Results 1 to 12 of 12
- 10-01-2012, 01:36 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
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.
- 10-01-2012, 02:04 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 10-01-2012, 02:20 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
- 10-01-2012, 02:31 PM #4
- 10-01-2012, 02:41 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Writing to File using FileWriter issue
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.
- 10-01-2012, 04:08 PM #6
Re: Writing to File using FileWriter issue
Does the myfile.txt even exist?
- 10-01-2012, 04:11 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
- 10-01-2012, 04:14 PM #8
- 10-01-2012, 04:17 PM #9
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Writing to File using FileWriter issue
- 10-01-2012, 04:23 PM #10
Re: Writing to File using FileWriter issue
- 10-01-2012, 04:27 PM #11
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Writing to File using FileWriter issue
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!
- 10-01-2012, 04:31 PM #12
Similar Threads
-
Writing conditional tab separated values using FileWriter
By ravi.joshi53 in forum Advanced JavaReplies: 10Last Post: 09-09-2012, 11:33 AM -
FileWriter issue
By victorgb in forum New To JavaReplies: 4Last Post: 04-18-2012, 09:31 PM -
File Writing Issue - Only Writes First Line
By DragonGhola in forum Advanced JavaReplies: 4Last Post: 07-22-2011, 04:41 PM -
write to file with filereader/filewriter
By LennyKosmos in forum New To JavaReplies: 3Last Post: 10-27-2010, 08:12 PM -
issue with FileReader and FileWriter
By Uma R in forum Advanced JavaReplies: 6Last Post: 07-16-2010, 01:16 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks