• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a text file.  RSS feed

 
JayaSiji Gopal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have the following lines of code to write to a text file.



However, when I open xmlFile, I get only a character (?) wht is wrong with this code?
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take it that your variable "year" is an integer. Take a look at the java documentation for java.io.BufferedWriter and you'll see:

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.
Throws:
IOException - If an I/O error occurs


With a variable name like "year" I don't think you want to write a single character. If you want to write a value like a year (i.e. 2005) to a file so that you can read it, either convert it to a String before using the write() method which takes a String as an argument or use a PrintWriter, which has methods to write primitive values in a text format.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!