读入:------------------------------------------------------InputStream
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStream_ {
public static void main(String[] args) {
File src = new File("D:/javac/1.txt");
FileInputStream is = null ;
try {
is = new FileInputStream(src);
byte[] car = new byte[10];
int len = 0;
while(-1!=(len=is.read(car))) {
String s = new String(car, 0, len);
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
写出:------------------------------------------------------OutputStream
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class OutputStream_ {
public static void main(String[] args) {
File destination = new File( "d:/javac/1.txt" );
OutputStream os = null ;
try {
os = new FileOutputStream(destination, true );
String s = "hehehhehehe\t\n" ;
byte [] data = s.getBytes();
os.write(data, 0, data. length );
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os!=null ) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}