对于输入流,则没有这个概念,只有OutputStream/Writer/Console实现了Flushable接口。
对于文本文件,我们可以通过FileReader或者FileWriter进行字符的读写,但是对于一些图片或者视频这类文件,显然字符是无意义的,需要使用FileInputStream与FileOutputStream等字节流进行操作。
Copy @ Test
public void testFileWriter() {
try ( FileWriter fw = new FileWriter( "demo.txt" ) ;) {
fw . write ( "abcde" );
fw . flush ();
} catch ( IOException e) {
e . printStackTrace ();
}
}
@ Test
public void testFileReader() {
try ( FileReader fileReader = new FileReader( "demo.txt" ) ;) {
int ch = 0 ;
while ((ch = fileReader . read ()) != - 1 ) {
System . out . println ( "ch=" + ( char ) ch);
}
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
}
@ Test
public void testFileReader2() {
try ( FileReader fileReader = new FileReader( "demo.txt" ) ;) {
char [] buf = new char [ 1024 ];
int num = 0 ;
while ((num = fileReader . read (buf)) != - 1 ) {
System . out . println ( new String(buf , 0 , num) );
}
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
}
Copy @ Test
public void testFileStream() {
try ( OutputStream outputStream = new FileOutputStream( "/Users/yue/Desktop/copy.png" ) ;
InputStream inputStream = new FileInputStream( "/Users/yue/Desktop/test.png" ) ;
) {
byte [] buffer = new byte [ 1024 * 1024 ];
while ( inputStream . read (buffer) != - 1 ) {
outputStream . write (buffer);
}
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
}
Copy @ Test
public void testBufferedStream() {
long start = System . currentTimeMillis ();
try (
InputStream inputStream = new FileInputStream( "/Users/yue/Desktop/test.ev4" ) ;
InputStream bis = new BufferedInputStream(inputStream , 1024 * 1024 ) ;
OutputStream outputStream = new FileOutputStream( "/Users/yue/Desktop/copy.ev4" ) ;
OutputStream bos = new BufferedOutputStream(outputStream , 1024 * 1024 ) ;
) {
byte [] buffer = new byte [ 1024 * 1024 ];
while (( bis . read (buffer)) != - 1 ) {
bos . write (buffer);
}
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
long end = System . currentTimeMillis ();
System . out . println ( "使用缓冲,耗时:" + (end - start));
}
Copy @ Test
public void testDataStream() {
try (
DataOutputStream dos = new DataOutputStream( new FileOutputStream( "test.txt" )) ;
DataInputStream dis = new DataInputStream( new BufferedInputStream( new FileInputStream( "test.txt" ))) ;
) {
dos . writeChars ( "abc" );
dos . writeBoolean ( Boolean . TRUE );
dos . writeInt ( 123 );
System . out . println ( dis . readChar ());
System . out . println ( dis . readChar ());
System . out . println ( dis . readChar ());
System . out . println ( dis . readBoolean ());
System . out . println ( dis . readInt ());
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
}
//输出如下a
b
c
true
123
Copy public class Person implements Serializable {
private String name;
private int age;
public Person ( String name , int age){
this . name = name;
this . age = age;
}
//省略setter/getter
@ Override
public String toString () {
return String . format ( "%s: %d" , this . name , this . age );
}
}
@ Test
public void testObjectStream() {
//写入
try (
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("object.ar")));
) {
Person person1 = new Person( "xiaohong" , 12 ) ;
Person person2 = new Person( "xiaoming" , 12 ) ;
oos . writeObject (person1);
oos . writeObject (person2);
oos . flush ();
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
}
//读取
try (
ObjectInputStream ois = new ObjectInputStream( new BufferedInputStream( new FileInputStream( "object.ar" ))) ;
) {
Person xiaohong = (Person) ois . readObject ();
Person xiaoming = (Person) ois . readObject ();
System . out . println (xiaohong);
System . out . println (xiaoming);
} catch ( FileNotFoundException e) {
e . printStackTrace ();
} catch ( IOException e) {
e . printStackTrace ();
} catch ( ClassNotFoundException e) {
e . printStackTrace ();
}
}