一、IO流的体系
二、代码应用
import java. io. * ;
public class Demo05 {
public static void main ( String [ ] args) throws IOException {
copy1 ( ) ;
copy2 ( ) ;
copy3 ( ) ;
copy4 ( ) ;
WriteGBK ( ) ;
ReadByGBK ( ) ;
copy6 ( ) ;
copy7 ( ) ;
}
public static void copy1 ( ) throws IOException {
long start = System . currentTimeMillis ( ) ;
FileInputStream fis = new FileInputStream ( "要读取的文件路径" ) ;
FileOutputStream fos = new FileOutputStream ( "要写入的文件路径" ) ;
int by= 0 ;
while ( ( by= fis. read ( ) ) != - 1 ) {
fos. write ( by) ;
}
fis. close ( ) ;
fos. close ( ) ;
long end = System . currentTimeMillis ( ) ;
System . out. println ( "1 使用原始的字节流按照一个一个字节的形式复制文件 = " + ( end- start) ) ;
}
public static void copy2 ( ) throws IOException {
long start = System . currentTimeMillis ( ) ;
FileInputStream fis = new FileInputStream ( "要读取的文件路径" ) ;
FileOutputStream fos = new FileOutputStream ( "要写入的文件路径" ) ;
int len= 0 ;
byte [ ] bytes= new byte [ 1024 ] ;
while ( ( len= fis. read ( bytes) ) != - 1 ) {
fos. write ( bytes, 0 , len) ;
}
fis. close ( ) ;
fos. close ( ) ;
long end = System . currentTimeMillis ( ) ;
System . out. println ( "2 使用原始的字节流按照字节数组的形式复制文件 = " + ( end- start) ) ;
}
public static void copy3 ( ) throws IOException {
long start = System . currentTimeMillis ( ) ;
BufferedInputStream bis = new BufferedInputStream ( new FileInputStream ( "要读取的文件路径" ) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( "要写入的文件路径" ) ) ;
int by= 0 ;
while ( ( by= bis. read ( ) ) != - 1 ) {
bos. write ( by) ;
}
bis. close ( ) ;
bos. close ( ) ;
long end = System . currentTimeMillis ( ) ;
System . out. println ( "3 使用高效的缓冲字节流按照一个一个字节的形式复制文件 = " + ( end- start) ) ;
}
public static void copy4 ( ) throws IOException {
long start = System . currentTimeMillis ( ) ;
BufferedInputStream bis = new BufferedInputStream ( new FileInputStream ( "要读取的文件路径" ) ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( "要写入的文件路径" ) ) ;
int len= 0 ;
byte [ ] bytes= new byte [ 1024 ] ;
while ( ( len= bis. read ( bytes) ) != - 1 ) {
bos. write ( bytes, 0 , len) ;
}
bis. close ( ) ;
bos. close ( ) ;
long end = System . currentTimeMillis ( ) ;
System . out. println ( "4 使用高效的缓冲字节流按照字节数组的形式复制文件 = " + ( end- start) ) ;
}
private static void WriteGBK ( ) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter ( new FileOutputStream ( "要写入的文件路径" ) , "gbk" ) ;
osw. write ( "我是GBK编码的文本" ) ;
osw. close ( ) ;
}
private static void ReadByGBK ( ) throws IOException {
InputStreamReader inputStreamReader = new InputStreamReader ( new FileInputStream ( "要读取的文件路径" ) , "gbk" ) ;
char [ ] chars = new char [ 1024 ] ;
int len;
while ( ( len= inputStreamReader. read ( chars) ) != - 1 ) {
System . out. println ( new String ( chars, 0 , len) ) ;
}
inputStreamReader. close ( ) ;
}
private static void copy6 ( ) {
String sourceFilePath = "要读取的文件路径" ;
String destinationFilePath = "要写入的文件路径" ;
try ( FileReader fr = new FileReader ( sourceFilePath) ;
FileWriter fw = new FileWriter ( destinationFilePath) ) {
int data;
while ( ( data = fr. read ( ) ) != - 1 ) {
fw. write ( data) ;
}
System . out. println ( "文件转存成功!" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
private static void copy7 ( ) {
String sourceFilePath = "要读取的文件路径" ;
String destinationFilePath = "要写入的文件路径" ;
try ( BufferedReader reader = new BufferedReader ( new FileReader ( sourceFilePath) ) ;
BufferedWriter writer = new BufferedWriter ( new FileWriter ( destinationFilePath) ) ) {
String line;
while ( ( line = reader. readLine ( ) ) != null ) {
writer. write ( line) ;
writer. newLine ( ) ;
}
System . out. println ( "文件转存成功!" ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}