• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how to write to a .txt file

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help us help you. What error does it give you?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this..... tutorial...

http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What I don't understand is how they changed the earth's orbit to fit the metric calendar. Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic