The program first writes a string to the specified file and then reads the contents of the file from another file to the screen.
The program code is as follows:
Import java.io.*;
public class ReadWrite {public
void WriteFile () {
String str = ' This was a program ';//What to write
try {
F Ileoutputstream out = new FileOutputStream ("d:/program/readwriteshuchu.txt"); Output file path
out.write (Str.getbytes ());
Out.close ();
} catch (Exception e) {
e.printstacktrace ();
}
}
public void ReadFile () {
try {
FileInputStream in = new FileInputStream ("d:/program/ceshi.txt");//Read file path
byte bs[] = new byte[in.available ()];
In.read (BS);
System.out.println ("File content=\n" + new String (BS));
In.close ();
} catch (Exception e) {
e.printstacktrace ();
}
}
public static void Main (string[] args) {
ReadWrite t = new ReadWrite ();
T.readfile ();
T.writefile ();
}
After the program runs, T.writefile () outputs the contents of the Ceshi.txt under the program folder of D disk to the screen, T.writefile () creates a new file named ReadWriteshuchu.txt under the program folder in D, and will A program is written inside the file.