二进制文件的拷贝
图片、音频、视频等,只能用字节流
拷贝二进制文件有4种方式:
1、字节缓冲流一次读写一个字节数组(效果最佳)
public static void main(String[] args) throws IOException {
String srcString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy\\a.txt";
String destString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\b.txt";
File srcFile = new File(srcString);
File destFile = new File(destString);
method(srcFile, destFile);
}
public static void method(File srcFile, File destFile) throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bs = new byte[1024];
int length = 0;
while ((length = bufferedInputStream.read(bs)) != -1) {
bufferedOutputStream.write(bs, 0, length);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
2、字节缓冲流一次读写一个字节
public static void main(String[] args) {
String srcStr = "D:\\1.txt";
String tagStr = "D:\\IOTest\\2.txt";
File f1 = new File(srcStr);
File f2 = new File(tagStr);
copy(f1, f2);
}
public static void copy(File f1,File f2) {
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis = new BufferedInputStream(new FileInputStream(f1));
bos = new BufferedOutputStream(new FileOutputStream(f2));
int length=0;
while ((length=bis.read())!=-1) {
bos.write(length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3、基本字节流一次读写一个字节数组
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr, tagStr);
}
public static void copy(String srcStr,String tagStr) {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis = new FileInputStream(srcStr);
fos = new FileOutputStream(tagStr);
byte[] bs=new byte[1024];
int length=0;
while ((length=fis.read(bs))!=-1) {
fos.write(bs,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4、基本字节流一次读写一个字节
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr,tagStr);
}
public static void copy(String srcStr, String tagStr) {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis = new FileInputStream(srcStr);
fos = new FileOutputStream(tagStr);
int length=0;
while ((length=fis.read())!=-1) {
fos.write(length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis!=null||fos!=null) {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文本文件(*.txt文件)的拷贝
用字节流和字符流都可以,但是推荐用字符流
拷贝文本文件,有5种方式:
1、字符缓冲流一次读写一个字符串(效果最佳)
public static void main(String[] args) throws IOException{
String srcString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy\\a.txt";
String destString = "D:\\QiuBo\\Class\\LessonPlan\\IOStudy22222\\b.txt";
method1(srcString,destString);
}
public static void method1(String srcString,String destString) throws IOException{
BufferedReader bufferedReader = new BufferedReader(new FileReader(srcString));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destString));
String line = null;
while ((line = bufferedReader.readLine())!= null) {
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
}
2、字符缓冲流一次读写一个字符数组
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr,tagStr);
}
public static void copy(String srcStr, String tagStr) {
BufferedReader br=null;
BufferedWriter bw=null;
try {
br = new BufferedReader(new FileReader(srcStr));
bw = new BufferedWriter(new FileWriter(tagStr));
char[] cs=new char[1024];
int length=0;
while ((length=br.read(cs))!=-1) {
bw.write(new String(cs, 0, length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br!=null||bw!=null) {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、字符缓冲流一次读写一个字符
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr,tagStr);
}
public static void copy(String srcStr, String tagStr) {
BufferedReader br=null;
BufferedWriter bw=null;
try {
br = new BufferedReader(new FileReader(srcStr));
bw = new BufferedWriter(new FileWriter(tagStr));
int length=0;
while ((length=br.read())!=-1) {
bw.write((char)length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br!=null||bw!=null) {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4、基本字符流一次读写一个字符数组
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr, tagStr);
}
public static void copy(String srcStr, String tagStr) {
InputStreamReader isr=null;
OutputStreamWriter osw=null;
try {
isr = new InputStreamReader(new FileInputStream(srcStr),"GB2312");
osw = new OutputStreamWriter(new FileOutputStream(tagStr),"GB2312");
char[] cs=new char[1024];
int length=0;
while ((length=isr.read(cs))!=-1) {
osw.write(new String(cs, 0, length));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr!=null) {
try {
isr.close();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5、基本字符流一次读写一个字符
public static void main(String[] args) {
String srcStr="D:\\1.txt";
String tagStr="D:\\IOTest\\2.txt";
copy(srcStr, tagStr);
}
public static void copy(String srcStr, String tagStr) {
InputStreamReader isr=null;
OutputStreamWriter osw=null;
try {
isr = new InputStreamReader(new FileInputStream(srcStr),"GB2312");
osw = new OutputStreamWriter(new FileOutputStream(tagStr),"GB2312");
int length=0;
while ((length=isr.read())!=-1) {
osw.write(length);
osw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr!=null) {
try {
isr.close();
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}