Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to write to a .txt file  RSS feed

 
gaurav chhabra
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir i have the following code -:::

FileOutputStream out = new FileOutputStream("ram.txt");

with this code i m able to open the file and now i like to store NUMBER and STRING in file , so how to write thatas i m using the methord -- ::

out.write("jai shri ram");
then it is giving the error
please help me out.
thank you
gaurav
 
Joe Ess
Bartender
Posts: 9425
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help us help you. What error does it give you?
 
Mikie Williams
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
out.write is looking for an array of type byte.
Try using out.write("abc".getBytes());

Or you could use:
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
out.write("abc");
 
Vijay Vaddem
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this..... tutorial...

http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
Ramaswamy Narayanan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FileOutputStream object has methods to write a character or byte array.
It has no method to write string.
Instead you can use DataOutputStream.
The DataOutputStream object has a method writeChars(string).
The sample code is

DataOutputStream out=new DataOutputStream(new FileOutputStream("ram.txt"));
out.writeChars("Jai Shri Ram");
out.writeChars("Jai Anchaneya");

pls try this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!