Results 1 to 8 of 8
  1. #1
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default Writing to DAT or TXT file

    //------------------------------------------------------------------
    ...
    String Username = "hunterbdb";
    byte[] bite2 = new byte[100];

    bite2 = username.getBytes();

    FileOutputStream fos = new FileOutputStream
    ("C:\\Documents andSettings\\Administrator\\Desktop\\java projects\\notepads\\messenger_users.text", true);

    fos.write(bite2,0,bite2.length);
    ...
    //------------------------------------------------------------------

    Hi, this is the code I'm using to write to a .txt file.


    username is the String that I converted to bytes so that I can send through the FileoutputStream.

    what i'm trying to do is append//add to the notepad file's text, however it is not working. Nothing is written to the file.

    Am I doing something wrong?

    Golden Brownies to the genius that helps me!
    Last edited by hunterbdb; 10-11-2008 at 08:38 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Perhaps you wish to use a FileWriter which was built for writing text to text files.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Does your code close the file after writing to it?

  4. #4
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default yea

    yea, it closes the input output streams afterward


    does Filewriter support abilities like "\n" to go to the next line and "\t" to tab?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    I copied your code, added a close() and it works for me. The file is created the first time and is added to after that.
    it is not working. Nothing is written to the file.
    What does this mean?
    What happens if you delete the file before the test? Is it created?
    Is there anything in it?

    What does the API doc say for FileWriter? It will write characters. \n and \t can be characters.
    Last edited by Norm; 10-11-2008 at 04:39 PM.

  6. #6
    hunterbdb is offline Member
    Join Date
    Oct 2008
    Posts
    48
    Rep Power
    0

    Default

    I used a filewriter like said above^

    it works.

    thanks! I didnt even know that filewriter existed.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    In general, try to stay with Writers for String data, but be aware that some file classes will not flush on close unless one places a close(); in the code as it is possible for the JVM to exit without writing all the data.

    Best approach is to look through all the classes in java.io .... then do testing.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Using/relying on default actions will cause errors. Assuming that the system will take care of you is poor coding technique. Sometimes it does and some times not.

Similar Threads

  1. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  2. Writing to a file (at the end)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:22 AM
  3. Writing UTF to file using writeUTF
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 08:19 PM
  4. writing to a file
    By bugger in forum New To Java
    Replies: 1
    Last Post: 11-11-2007, 02:49 AM
  5. Help with File reading and writing
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:47 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
  •