• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

write a int to a file

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you write a int to a file.
int i = ***;
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
out.write(i);
out.close();
} catch (IOException e1) {
}
It does not work...
Thanks
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test{
static int r;
public static void main(String[] avi){
Random generator = new Random();
r = generator.nextInt();
File file = new File("Status.log");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file,true));
out.write(r);
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}


}
}

This is my code...r is a random integer...
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Please use code-tags)
Quoting java-docs:


public void write(int c)
throws IOException

Write a single character.

Overrides:
write in class Writer

Parameters:
c - int specifying a character to be written.



specifying a character to be written means: an ascii-code.

int 9 = '\t'
int 56= '8'
int 65= 'A'
...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's try to be a little more helpful, please, shall we guys?

If you call write() and pass an int, than it's the corresponding character code, not a human-readable version, of the String that's going to go into the file.

The class "PrintWriter" is designed to render human-readable text, and it has lots of overloaded "print" methods that do what you want. So in your original code, change "BufferedWriter" to "PrintWriter", and change "write()" to "print()", and your program will work as you expected!
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks....
I got it now...
 
reply
    Bookmark Topic Watch Topic
  • New Topic