Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-11-2008, 08:33 AM
Member
 
Join Date: Oct 2008
Posts: 40
Rep Power: 0
hunterbdb is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-11-2008, 01:46 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 3,195
Rep Power: 5
Fubarable is on a distinguished road
Default
Perhaps you wish to use a FileWriter which was built for writing text to text files.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-11-2008, 02:58 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
Default
Does your code close the file after writing to it?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-11-2008, 03:50 PM
Member
 
Join Date: Oct 2008
Posts: 40
Rep Power: 0
hunterbdb is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-11-2008, 04:37 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
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.
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-12-2008, 05:34 AM
Member
 
Join Date: Oct 2008
Posts: 40
Rep Power: 0
hunterbdb is on a distinguished road
Default
I used a filewriter like said above^

it works.

thanks! I didnt even know that filewriter existed.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-12-2008, 01:42 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
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.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-12-2008, 02:50 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
swapping the contents of the file and writing to another file Ms.Ranjan New To Java 9 07-10-2008 04:52 PM
Writing to a file (at the end) Java Tip Java Tips 0 02-08-2008 09:22 AM
Writing UTF to file using writeUTF Java Tip Java Tips 0 01-22-2008 08:19 PM
writing to a file bugger New To Java 1 11-11-2007 02:49 AM
Help with File reading and writing baltimore New To Java 1 07-31-2007 06:47 PM


All times are GMT +2. The time now is 03:28 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org