Read and Write files:
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class Dxshuju {
public static void Main (string[] args) {
String str = "Chinayang, Ajax keeps letting go, and I can't wait to see me."
+ "The right to be spoiled everyone big space hack phone card words ah ah ah ah ah ah!"
+ "The right to spoiled everybody big space hack phone card words ah ah ah ah ah ah oh \ n"
+ "The right to spoiled everyone big space hack phone card words ah ah ah ah ah ah";
Specific path to the connected file
try {
FileOutputStream fs = new FileOutputStream ("Hello.java"); Creating an Instance Object
Write a file
try {
byte [] b=new byte [5000];
B=str.getbytes ();
int off=0;
int len=b.length;
System.out.println (len);
Fs.write (b, off, Len);
System.out.println ("FS OK");
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Fs.close (); Turn off the output stream
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
FileInputStream FS1 =new fileinputstream ("Hello.java"); Create an Object association file
File f =new file ("Hello.java"); Get file properties
Long an = F.length ();
Byte[] B = new byte[(int) an];
Read the file
try {
int rEnd = Fs1.read (b); Read the whole block.
System.out.println (New String (b));
for (int i=0;i<an;i++) {
int xie = Fs1.read ();
if ( -1!=xie) {
System.out.print ((char) Xie);
}
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
try {
Fs1.close (); Turn off the output stream
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Java file read/write