Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you read and write to text files?

Status
Not open for further replies.

stevepuri

Programmer
Mar 30, 2001
24
0
0
US
Hey,

Does anyone know how to read
and write to text files in Java?



In C++, to read a file, you would do:
(stores the contents into array "data")

Code:
ifstream f("myFile.txt")
char ch;
char* data = " ";
while (f.get(ch))
{
    data << ch;
}
f.close();



In C++, to write to a file, you would do:
(writes &quot;Hello, world!&quot; to myFile.txt)

Code:
ofstream g(&quot;myFile.txt&quot;);
g << &quot;Hello, world!&quot; << &quot;\n&quot;;
g.close();



Anyone have the Java equivalent?

Thanks,
Steve.
 
Well, I have only just begun doing this - so it is probably inelegant but I do somrthing like:

Reading
1. Create FileReader
2. Read from file to charArray (buffer) using FileReader
3. Call function to read char array and print to wherever.
This is the function I used to read, I don't think it is
the most eficient, but I was in hurry ;)

private String readFromABuffer(char[] charBuffer){
String output = new String();
// fill the string with characters
for(int j=0;j<charBuffer.length;j++){
Character newChar = new Character(charBuffer[j]);
String fromBuffer = newChar.toString();
output = output.concat(fromBuffer);
}
return output;
}

This returns a string which can be written to a textArea. I had to use this function because the filereader's read method returns a character array.

Saving is more straight forward:
/** This function is responsible for:
1. Getting an output stream -
2. Writing the data.
3. Saving.
*/
private void save(){

// get text to save
String data = this.txt.getText();

try{
canWrite = theFile.canWrite();
out = new FileWriter(theFile);
System.out.println(&quot;FileWriter created&quot;);
}
catch(SecurityException s){
System.err.println(&quot;Cannot write to this file&quot;);
}
catch(NullPointerException n){
System.err.println(&quot;File not there.&quot;);
}
catch(IOException io){
System.err.println(&quot;IO error - no file or something...&quot;);
}
// write data to file
try{
if(canWrite){
out.write(data);
}
else{
System.out.println(&quot;Cannot overwrite file..&quot;);
}
out.close()
System.out.println(&quot;String.written&quot;);

}
catch(IOException io){
System.err.println(&quot;Error writing to file.&quot;);
}
}


There are some global variables here, sorry:
boolean canWrite;
FileReader in;
FileWriter out;
File theFile;
TextArea txt;

check out you javadocs for more details of these methods.

b[sup]2[/sup] - benbiddington@surf4nix.com
 
There's a good example in Java for Students.

However, applets can't read and write to the local drive; only applications. You can read from a file on the internet the same way, but you have to use ftp to upload them again.

Rose
 
Hi

Just in case you wanted to know, Per Brinch Hansen has some read/write classes he has written for tutorial purposes, but they are actually pretty useful if you are just reading a byte stream.

You can download them at ftp.cis.syr.edu from within pbh/textprogram directory. The file is called Text.java.

pipk



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top