Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
    Bookmark Topic Watch Topic
  • New Topic

file navigation and I\O ...

sonia jaswal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Report post to moderator


class Writer2 {
public static void main(String [] args) {
char[] in = new char[50]; // to store input
int size = 0;
try {
File file = new File( // just an object
FileWriter fw =
new FileWriter(file); // create an actual file
// & a FileWriter obj
fw.write("howdy\nfolks\n"); // write characters to
// the file
fw.flush(); // flush before closing
fw.close(); // close file when done

FileReader fr = =
new FileReader(file); // create a FileReader
// object
size =; // read the whole file!
System.out.print(size + " "); // how many bytes read
for(char c : in) // print the array
fr.close(); // again, always close }
} catch(IOException e) { }

which produces the output:

12 howdy

here, the following para is a limitation for the previous code.... which i did not understand... can someboidy please explain it to me...
"When we were reading data back in, we put it into a character array. It being an array and all, we had to declare its size beforehand, so we'd have been in trouble if we hadn't made it big enough! We could have read the data in one character at a time, looking for the end of file after each read(), but that's pretty painful too."

Apart from this... please tell me what does reading the file's contents back into memory mean....

thank you....
Ulf Dittmer
Posts: 42968
  • Mark post as helpful
  • send pies
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
    Bookmark Topic Watch Topic
  • New Topic