FileInputStream和FileOutputStream复制文件
public class streamCopyFile {
public static void main(String[] args) {
streamCopyFile("E:/file/1.jpg","E:/file/4.jpg");
}
private static void streamCopyFile(String srcFileName, String descFileName) {
FileInputStream fi = null;
FileOutputStream fo = null;
try{
// 使用字节流进行文件复制
fi = new FileInputStream(srcFileName);
fo = new FileOutputStream(descFileName);
// Integer by = 0;
// //一次读取一个字节
// while((by = fi.read()) != -1) {
// fo.write(by);
// }
//创建搬运工具
byte buffer[] = new byte[1024];
//创建长度
int len = 0;
//循环读取数据
while((len = fi.read(buffer))!=-1){
fo.write(buffer,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(fi!=null){
try {
fi.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
if(fo!=null){
try {
fo.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
}
}
}
BufferedInputStream和BufferedOutputStream复制文件
public class BufferedStreamCopyFile {
public static void main(String[] args) {
streamCopyFile("E:/file/1.jpg","E:/file/5.jpg");
}
private static void streamCopyFile(String srcFileName, String descFileName) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
//缓冲区字节输入流
bis = new BufferedInputStream(new FileInputStream(srcFileName));
//缓冲区字节输出流
bos = new BufferedOutputStream(new FileOutputStream(descFileName));
// int len = 0;
// while ((len = bis.read()) != -1) {//读取
// //写入另一个文件
// bos.write(len);
// }
byte[] buffer = new byte[1024];//构造一个长度为1024的字节数组
int len = 0;
while ((len = bis.read(buffer)) != -1) {//读取
//写入另一个文件
bos.write(buffer);
}
bos.flush();
}catch (IOException e){
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
if(bos!=null){
try {
bos.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
}
}
}
FileWriter和FileReader复制文件
public class WriterReaderCopyFile {
public static void copyd() {
FileWriter fw = null;
FileReader fd = null;
try
{
fw = new FileWriter("/Users/dongbo/Downloads/a_copy.txt");
fd = new FileReader("/Users/dongbo/Downloads/a.txt");
char [] ch = new char[1024];
int num = 0;
while((num=fd.read(ch))!=-1) {
fw.write(ch,0,num);
}
} catch(IOException e) {
System.out.println( e.toString());
} finally {
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fd!=null)
fd.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
BufferedReader和BufferedWriter复制文件
public class BufferedWriterReaderCopy {
public static void main(String[] args) {
copyFile("E:/file/3.36.txt","E:/file/3.37.txt");
}
public static void copyFile(String srcFileName, String descFileName) {
BufferedReader bf = null;
BufferedWriter bw = null;
try {
//缓冲区字符输入流
bf = new BufferedReader(new FileReader(srcFileName));
//缓冲区字符输出流
bw = new BufferedWriter(new FileWriter(descFileName));
// int len = 0;
// char[] buffer = new char[1024];
// while ((len = bf.read(buffer)) != -1) {//直接按字节读取
// //写入另一个文件
// bw.write(buffer);
// }
String str;
while ((str = bf.readLine()) != null) {//直接按字节读取
//写入另一个文件
bw.write(str);
bw.newLine();
bw.flush();
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(bw!=null){
try {
bw.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
if(bf!=null){
try {
bf.close();
}catch (IOException e){
throw new RuntimeException("关闭失败");
}
}
}
}
}
- 字符流的均会出现中文字符乱码的情况
InputStreamReader isr = new InputStreamReader(new FileInputStream(srcFileName),"GBK");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcFileName),"GBK"));
网友评论