Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
    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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

file navigation and I\O ...

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Code:

import java.io.*;

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
"fileWrite2.txt");
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 = fr.read(in); // read the whole file!
System.out.print(size + " "); // how many bytes read
for(char c : in) // print the array
System.out.print(c);
fr.close(); // again, always close }
} catch(IOException e) { }
}
}

which produces the output:

12 howdy
folks

//
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....
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic