CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2011
    Posts
    8

    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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2011
    Posts
    8

    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.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Oct 2011
    Posts
    8

    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
  •  





Click Here to Expand Forum to Full Width

Featured