文件字节流:
代码
package ht;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class F {
public static void main(String[] args) {
new F().copy("C:/Users/ACER/Desktop/计算机组成原理 指令缩写.png", "C:/Users/ACER/Desktop/NEW计算机组成原理 指令缩写.png");
}
public static void copy(String inPath,String outPath){
try {
FileInputStream in = new FileInputStream(inPath);
FileOutputStream out = new FileOutputStream(outPath);
byte b[] = new byte[10]; //字节数组
int len=0;
//用while不用担心字节数组的大小问题,都能存进去
//没有读到值了默认是-1,才结束
while((len=in.read(b)) != -1){
out.write(b,0,len); //从b里写:0开始,长度为len
}
out.flush(); //清空缓冲区
out.close(); //关流
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件字符流(适合复制内容为字符的文件)
代码
package ht;
import java.io.FileReader;
import java.io.FileWriter;
public class F {
public static void main(String[] args) {
new F().copy("C:/Users/ACER/Desktop/文本.txt", "C:/Users/ACER/Desktop/NEW文本.txt");
}
public static void copy(String inPath,String outPath){
try {
FileReader fr = new FileReader(inPath);
FileWriter fw = new FileWriter(outPath);
char [] c= new char[10]; //大小不重要,因为while读
//一直读 , 直到为空-1
int len=0;
while((len=fr.read(c)) != -1){
fw.write(c,0,len); //写从c的0开始,长度为len
}
fw.flush(); //清空缓冲区
fw.close(); //关流
fr.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
如果copy了图片,可能就是这样黑shai的
缓冲字节流:
缓冲流速度比上面快很多
代码:
package ht;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class F {
public static void main(String[] args) {
try {
new F().copy("C:\\Users\\ACER\\Desktop\\计算机组成原理 指令缩写.png","C:\\Users\\ACER\\Desktop\\NEW计算机组成原理 指令缩写.png");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(String inPath,String outPath) throws Exception{
//将字节输入流对象放入缓冲字节输入流对象中
FileInputStream in = new FileInputStream(inPath);
BufferedInputStream BIS = new BufferedInputStream(in);
//将字节输出流对象放入缓冲字节输出流对象中
FileOutputStream out = new FileOutputStream(outPath);
BufferedOutputStream BOS = new BufferedOutputStream(out);
byte b[] = new byte[10]; //大小不重要,因为while读
int len=0;
while((len=BIS.read(b)) != -1){
BOS.write(b,0,len); //从b里写:0开始,长度为len
}
BOS.flush(); //清空缓存区
BOS.close(); //关流顺序(迟到早退,后开先关)
out.close();
BIS.close();
in.close();
}
}
缓冲字符流:
代码:
package ht;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class F {
public static void main(String[] args) {
try {
new F().copy("C:\\Users\\ACER\\Desktop\\文本.txt","C:\\Users\\ACER\\Desktop\\NEW文本.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copy(String inPath,String outPath) throws Exception{
//将字符输入流对象放入缓冲字符输入流对象中
FileReader fr = new FileReader(inPath);
BufferedReader BR = new BufferedReader(fr);
//将字符输出流对象放入缓冲字符输出流对象中
FileWriter fw = new FileWriter(outPath);
BufferedWriter BW = new BufferedWriter(fw);
char c[] = new char[10]; //大小不重要,因为while读
int len=0;
while((len=BR.read(c)) != -1){
BW.write(c,0,len);//从c里写:0开始,长度为len
}
BW.flush(); //清空缓存区
BW.close(); //关流顺序(迟到早退,后开先关)
fw.close();
BR.close();
fr.close();
}
}