字节流,缓冲流,字符流复制程序案例
来源:互联网 发布:帮开淘宝店铺 编辑:程序博客网 时间:2024/05/17 03:17
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();
}
}
通过字符流进行复制文件的操作,适合于一般的文本文件
- 字节流,缓冲流,字符流复制程序案例
- IO流_字符缓冲流复制文本文件案例1
- IO流_字符缓冲流复制文本文件案例2
- 字节流、字符流、字节缓冲流、字符缓冲流
- 字符流,字节流,缓冲
- 字符流,字节流,缓冲
- Java IO流 文件的复制的几种方法(字节流,字符流,缓冲流)
- IO流 字符流 字节流 缓冲流 文件的复制
- 字节缓冲流和字符缓冲流的对比
- IO学习(九)字节缓冲流,字符缓冲流
- 文件输入/出字节流、文件缓冲输入/出字节流、文件输入/出字符流、文件缓冲输入/出字符流复制文件
- java缓冲字节流复制文件,逐个字节读取、写入
- java字符、字节流复制
- 字节和字符流复制
- 缓冲字符流读取案例快速读取
- java中演示缓冲字符流案例
- 基于缓冲字符输入输出流复制文本文件
- 字符缓冲流实现文本文件复制功能
- QT学习——没有找到mingwm10.dll的解决办法
- Android apk反编译
- JAVA基础(三)网络编程简介
- 自定义HttpMessageConverter
- Dockerfile中CMD和ENTRYPOINT的组合用法
- 字节流,缓冲流,字符流复制程序案例
- CSS 元素
- 【MyBatis学习01】宏观上把握MyBatis框架
- 机器学习基础-Knn分类器
- Dockerfile中COPY的用法
- JavaScript
- 【MyBatis学习02】走进MyBatis的世界
- CSS 布局模型
- 机器学习方法篇(4)------决策树剪枝