-
October 7th, 2011, 09:40 PM
#1
BufferedWriter Issue
Hello,
I am trying to write to a textfile using the BufferedWriter class. My program is below:
import java.io.BufferedWriter;
import java.io.FileWriter;
public class File_Write {
public static void main(String[] args) throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter("file.txt"));
writer.write("First Line Of My File");
writer.close();
}
}
The file.txt is in the same folder as the .java and .class files in the BlueJ project.
I see nothing in the file after the program runs.
Any comments on what I am doing wrong?
Thank you!
jetski100
-
October 8th, 2011, 08:22 AM
#2
Re: BufferedWriter Issue
It works for me.
Make sure you are looking at the right text file. I suggest you delete the one in your src folder, run the program and see if it gets recreated. If so see what's in it. If not check the project's root directory as that is where I would expect to see it.
-
October 8th, 2011, 12:34 PM
#3
Re: BufferedWriter Issue
Hello keang,
That worked very well! Thank you!
If you don't mind, one more question:
I tried opening an 2010 Excel file by writing:
BufferedWriter writer = new BufferedWriter(new FileWriter("file.xls"));
It ends up opening a Microsoft Works spreadsheet instead of an Office 2010 Excel file. When I try the extension, ".xlsx", it creates a 2010 excel file. The problem is I get an error when I try to open the file saying that it is the wrong file extension.
I would be grateful for any help!
Regards,
jetski100
Last edited by jetski100; October 8th, 2011 at 12:56 PM.
-
October 8th, 2011, 03:17 PM
#4
Re: BufferedWriter Issue
It ends up opening a Microsoft Works spreadsheet instead of an Office 2010 Excel file.
That's down to the file associations on your computer.
The problem is I get an error when I try to open the file saying that it is the wrong file extension.
An Excel .xlsx file is a pretty complex file. It's actually a zip of several XML files. You can't just write some plain text to a file give it a .xlsx extension and expect Excel to be able to open it.
-
October 8th, 2011, 06:33 PM
#5
Re: BufferedWriter Issue
Hello Keang,
Makes sense. Thank you again!
Regards,
jetski100
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|