字节流,缓冲流,字符流复制程序案例

来源:互联网 发布:帮开淘宝店铺 编辑:程序博客网 时间:2024/05/17 03:17
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileStreamCopy {
public static void main(String[] args) throws IOException{
int size;
FileInputStream f=new FileInputStream("D:/workspace/I_O/src/p3/FileStreamCopy.java");
FileOutputStream fout=new FileOutputStream("D:/workspace/I_O/src/p3/copy-of-file.txt");
System.out.println("Total Available Bytes:"+(size=f.available()));
int n=size/30;
System.out.println("First "+n+"bytes of the file one read() at a time");
for(int i=0;i<n;i++){
fout.write(f.read());
}
System.out.println("Still Available:"+f.available());
System.out.println("Reading the next "+n+" with one read(b[])");
byte b[]=new byte[n];
if(f.read(b)!=n){
System.out.println("couldn't read"+n+"bytes.");
}
fout.write(b);
System.out.println("Still Available:"+f.available());
System.out.println("Reading the rest bytes with read(b[],offset,len)");
int count=0;
while((count=f.read(b,0,n))!=-1)
fout.write(b,0,count);
System.out.println("Still Available:"+f.available());
f.close();
fout.flush();
fout.close();
}

}


通过字节流将文件进行复制操作

-----------------------------------------------------------------------------------------------------------------------------------------------------


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedStreamCopy {
public static void main(String[] args) throws IOException{
int size;
FileInputStream f=new FileInputStream("D:/workspace/I_O/src/p4/BufferedStreamCopy.java");
FileOutputStream fout=new FileOutputStream("D:/workspace/I_O/src/p4/copy.txt");
BufferedInputStream bis=new BufferedInputStream(f);
BufferedOutputStream bos=new BufferedOutputStream(fout);
System.out.println("Total Available Bytes: "+(size=bis.available()));
int n=size/30;
System.out.println("First "+n+" bytes of the file one read() at a time");
for(int i=0;i<n;i++){
bos.write(bis.read());
}
System.out.println("Still Available: "+bis.available());
System.out.println("Reading the next "+n+" with one read(b[])");
byte b[]=new byte[n];
if(bis.read(b)!=n){
System.err.println("couldn't read "+n+"bytes.");
}
bos.write(b);
System.out.println("Still Available: "+bis.available());
System.out.println("Reading the rest bytes with read(b[],offst,len)");
int count=0;
while((count=bis.read(b, 0, n))!=-1){
bos.write(b,0,count);
}
System.out.println("Still Available: "+bis.available());
bis.close();
bos.flush();
bos.close();
f.close();
fout.flush();
fout.close();
}
}


通过缓冲流进行复制文件操作,能够优化字节流的复制速度,使得程序更高效,更有效率

-----------------------------------------------------------------------------------------------------------------------------------------------------


import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileReaderWriterDemo {
public static void main(String[] args) throws IOException{
File file=new File("D:/workspace/I_O/src/p5/FileReaderWriterDemo.java");
FileReader f=new FileReader(file);
FileWriter fout=new FileWriter("D:/workspace/I_O/src/p5/copy.txt");
System.out.println("Current charset is: "+f.getEncoding());
int n=(int) (file.length()/30);
System.out.println("First "+n+" char of the file one read() at a time");
for(int i=0;i<n;i++){
fout.write(f.read());
//System.out.print(f.read());
}
System.out.println("Reading the next "+n+" with one read(b[])");
char b[]=new char[n];
if(f.read(b)!=n){
System.err.println("couldn't read "+n+" with one read(b[])");
}
fout.write(b);
System.out.println("Reading the rest chars with read(b[],offset,len)");
int count=0;
while((count=f.read(b, 0, n))!=-1)
{ fout.write(b, 0, count);
// System.out.print((char)b[count]);
}

f.close();
fout.flush();
fout.close();


}


}


通过字符流进行复制文件的操作,适合于一般的文本文件

原创粉丝点击