Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-11-2008, 10:33 AM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
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 10:38 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-11-2008, 03:46 PM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 807
Fubarable is on a distinguished road
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, 04:58 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
Does your code close the file after writing to it?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-11-2008, 05:50 PM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
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, 06:37 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
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 06:39 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-12-2008, 07:34 AM
Member
 
Join Date: Oct 2008
Posts: 24
hunterbdb is on a distinguished road
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, 03:42 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 710
Nicholas Jordan is on a distinguished road
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, 04:50 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Gone to Costa Rica
Posts: 2,223
Norm is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 06:52 PM
Writing to a file (at the end) Java Tip Java Tips 0 02-08-2008 11:22 AM
Writing UTF to file using writeUTF Java Tip Java Tips 0 01-22-2008 10:19 PM
writing to a file bugger New To Java 1 11-11-2007 04:49 AM
Help with File reading and writing baltimore New To Java 1 07-31-2007 08:47 PM


All times are GMT +3. The time now is 04:47 PM.


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