1.缓冲字节流:
基于硬盘io文件操作速度太慢,产生了基于内存的缓冲IO流。
import java. io. BufferedInputStream;
import java. io. BufferedOutputStream;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
public class Test {
public static void main ( String[ ] args) {
try {
Test. copyFile ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testBufferedInputStream ( ) throws Exception {
FileInputStream in = new FileInputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt.txt" ) ;
BufferedInputStream br = new BufferedInputStream ( in) ;
byte [ ] b = new byte [ 10 ] ;
int len = 0 ;
while ( ( len = br. read ( b) ) != - 1 ) {
System. out. println ( new String ( b, 0 , len) ) ;
}
br. close ( ) ;
in. close ( ) ;
}
public static void testBufferedOutputStream ( ) throws Exception {
FileOutputStream out = new FileOutputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt2.txt" ) ;
BufferedOutputStream bo = new BufferedOutputStream ( out) ;
String s = "hello world" ;
bo. write ( s. getBytes ( ) ) ;
bo. flush ( ) ;
bo. close ( ) ;
out. close ( ) ;
}
public static void copyFile ( ) throws Exception {
BufferedInputStream br = new BufferedInputStream ( new FileInputStream ( "C:\\\\Users\\\\Daniel\\\\eclipse-workspace\\\\hello\\\\src\\\\day13\\\\tt.txt" ) ) ;
BufferedOutputStream bo = new BufferedOutputStream ( new FileOutputStream ( "C:\\\\\\\\Users\\\\\\\\Daniel\\\\\\\\eclipse-workspace\\\\\\\\hello\\\\\\\\src\\\\\\\\day13\\\\\\\\tt2.txt" ) ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = br. read ( b) ) != - 1 ) {
bo. write ( b, 0 , len) ;
}
bo. flush ( ) ;
bo. close ( ) ;
br. close ( ) ;
}
}
2.缓冲字符流:
import java. io. BufferedInputStream;
import java. io. BufferedOutputStream;
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
public class Test {
public static void main ( String[ ] args) {
try {
Test. copyFile ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testBufferedInputStream ( ) throws Exception {
FileInputStream in = new FileInputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt.txt" ) ;
BufferedInputStream br = new BufferedInputStream ( in) ;
byte [ ] b = new byte [ 10 ] ;
int len = 0 ;
while ( ( len = br. read ( b) ) != - 1 ) {
System. out. println ( new String ( b, 0 , len) ) ;
}
br. close ( ) ;
in. close ( ) ;
}
public static void testBufferedOutputStream ( ) throws Exception {
FileOutputStream out = new FileOutputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt2.txt" ) ;
BufferedOutputStream bo = new BufferedOutputStream ( out) ;
String s = "hello world" ;
bo. write ( s. getBytes ( ) ) ;
bo. flush ( ) ;
bo. close ( ) ;
out. close ( ) ;
}
public static void copyFile ( ) throws Exception {
BufferedInputStream br = new BufferedInputStream ( new FileInputStream ( "C:\\\\Users\\\\Daniel\\\\eclipse-workspace\\\\hello\\\\src\\\\day13\\\\tt.txt" ) ) ;
BufferedOutputStream bo = new BufferedOutputStream ( new FileOutputStream ( "C:\\\\\\\\Users\\\\\\\\Daniel\\\\\\\\eclipse-workspace\\\\\\\\hello\\\\\\\\src\\\\\\\\day13\\\\\\\\tt2.txt" ) ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = br. read ( b) ) != - 1 ) {
bo. write ( b, 0 , len) ;
}
bo. flush ( ) ;
bo. close ( ) ;
br. close ( ) ;
}
}
3.转换流:
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. InputStreamReader;
public class Test2 {
public static void main ( String[ ] args) {
try {
Test2. testInputStreamReader ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testInputStreamReader ( ) throws Exception {
FileInputStream fs = new FileInputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt5.txt" ) ;
InputStreamReader in = new InputStreamReader ( fs, "GBK" ) ;
char [ ] c = new char [ 100 ] ;
int len = 0 ;
while ( ( len = in. read ( ) ) != - 1 ) {
System. out. println ( new String ( c, 0 , len) ) ;
}
in. close ( ) ;
fs. close ( ) ;
}
}
4.标准输入输出流:
import java. io. BufferedReader;
import java. io. BufferedWriter; import java. io. FileWriter;
import java. io. IOException;
import java. io. InputStreamReader;
public class Test3 {
public static void main ( String[ ] args) {
try {
Test3. write2TXT ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testSystemIn ( ) throws Exception {
InputStreamReader is = new InputStreamReader ( System. in) ;
BufferedReader br = new BufferedReader ( is) ;
String str = "" ;
while ( ( str= br. readLine ( ) ) != null) {
System. out. println ( str) ;
}
br. close ( ) ;
is. close ( ) ;
}
public static void write2TXT ( ) throws Exception {
InputStreamReader is = new InputStreamReader ( System. in) ;
BufferedReader br = new BufferedReader ( is) ;
BufferedWriter out = new BufferedWriter ( new FileWriter ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt7.txt" ) ) ;
String str = "" ;
while ( ( str= br. readLine ( ) ) != null) {
if ( str. equals ( "over" ) ) {
break ;
}
out. write ( str) ;
}
out. flush ( ) ;
out. close ( ) ;
br. close ( ) ;
is. close ( ) ;
}
}
5*.打印流和数据流:
6.对象流:
import java. io. Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1 L;
String name;
int age;
}
import java. io. FileInputStream;
import java. io. FileNotFoundException;
import java. io. FileOutputStream;
import java. io. IOException;
import java. io. ObjectInputStream;
import java. io. ObjectOutputStream;
import java. io. OutputStream;
public class Test5 {
public static void main ( String[ ] args) {
try {
Test5. testSerialize ( ) ;
Test5. testDeserialize ( ) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testSerialize ( ) throws FileNotFoundException, Exception {
ObjectOutputStream out = new ObjectOutputStream ( new FileOutputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt8.txt" ) ) ;
Person p = new Person ( ) ;
p. name = "zhangsan" ;
p. age = 11 ;
out. writeObject ( p) ;
out. flush ( ) ;
out. close ( ) ;
}
public static void testDeserialize ( ) throws FileNotFoundException, Exception {
ObjectInputStream in = new ObjectInputStream ( new FileInputStream ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt8.txt" ) ) ;
Object obj = in. readObject ( ) ;
Person p = ( Person) obj;
System. out. println ( p. name) ;
System. out. println ( p. age) ;
in. close ( ) ;
}
}
7.随机读取流:
import java. io. FileNotFoundException;
import java. io. RandomAccessFile;
public class Test6 {
public static void main ( String[ ] args) {
try {
Test6. testRandomAccessFileRead ( ) ;
Test6. testRandomAccessFileWrite ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static void testRandomAccessFileRead ( ) throws Exception {
RandomAccessFile ra = new RandomAccessFile ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt10.txt" , "r" ) ;
ra. seek ( 0 ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = ra. read ( b) ) != - 1 ) {
System. out. println ( new String ( b, 0 , len) ) ;
}
ra. close ( ) ;
}
public static void testRandomAccessFileWrite ( ) throws Exception {
RandomAccessFile ra = new RandomAccessFile ( "C:\\Users\\Daniel\\eclipse-workspace\\hello\\src\\day13\\tt10.txt" , "rw" ) ;
ra. seek ( 0 ) ;
ra. write ( "你好" . getBytes ( ) ) ;
ra. close ( ) ;
}
}