如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
private static void copyFile_1() throws IOException {
// 复制文件使用的时间是:89352毫秒
// 创建对象
FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;
// 一次读取一个字节
int by = 0 ;
while((by = fis.read()) != -1){
fos.write(by) ;
}
// 释放资源
fos.close() ;
fis.close() ;
}
一次读一个字节数组
private static void copyFile_2() throws IOException {
// 复制文件使用的时间是:154毫秒
// 创建对象
FileInputStream fis = new FileInputStream("C:\\waiting for you.mp3") ;
FileOutputStream fos = new FileOutputStream("D:\\waiting for you.mp3") ;
// 一次读取一个字节数组
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = fis.read(bytes)) != -1){
fos.write(bytes, 0, len) ;
}
// 释放资源
fos.close() ;
fis.close() ;
}
public static void copyFile_3() throws IOException {
// 复制文件使用的时间是:920毫秒
// 创建高效流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
// 一次读取一个字节
int by = 0 ;
while((by = bis.read()) != -1){
bos.write(by) ;
}
// 释放资源
bos.close() ;
bis.close() ;
}
一次读取一个字节数组
public static void copyFile_4() throws IOException {
// 复制文件使用的时间是:53毫秒
// 创建高效流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\waiting for you.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\waiting for you.mp3")) ;
// 一次读取一个字节数组
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
// 释放资源
bos.close() ;
bis.close() ;
}
public static void main(String[] args) throws IOException {
// 创建转换输入流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("OutputStreamWriterDemo.java")) ;
// 创建转换输出流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copyFile.java")) ;
// 复制文件
// 一次读取一个字符复制
// int ch = 0 ;
// while((ch = isr.read()) != -1){
// osw.write(ch) ;
// }
// 一次读取一个字符数组复制文件
char[] chs = new char[1024] ;
int len = 0 ;
while((len = isr.read(chs)) != -1){
osw.write(chs, 0, len) ;
}
// 释放资源
osw.close() ;
isr.close() ;
}
public static void main(String[] args) throws IOException {
// 创建高效的字符输入流对象
BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
// 创建高效的字符输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile3.java")) ;
// 一次读取一个字符数组复制文件
char[] chs = new char[1024] ;
int len = 0;
while((len = br.read(chs)) != -1){
bw.write(chs, 0, len) ;
}
// 释放资源
bw.close() ;
br.close() ;
}
public static void main(String[] args) throws IOException {
/**
* 需求: 使用高效的字符流中特有的功能复制文本文件
*/
// 创建高效的字符输入流对象
BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
// 高效的字符输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile4.java")) ;
// 复制文件
// 一次读取一行复制文件
String line = null ;
while((line = br.readLine()) != null) {
bw.write(line) ;
bw.newLine() ;
bw.flush() ;
}
// 释放资源
bw.close() ;
br.close() ;
}
public class IOUtils {
public static void copyFolder(String srcPahtName , String destPathName , FilenameFilter filenameFilter) throws IOException {
File srcFolder = new File(srcPahtName) ;
File destFolder = new File(destPathName) ;
if(!destFolder.exists()) {
destFolder.mkdir() ;
}
copyFolder(srcFolder , destFolder , filenameFilter) ;
}
public static void copyFolder(File srcFolder , File destFolder , FilenameFilter filenameFilter) throws IOException {
File[] files = null ;
if(filenameFilter == null) {
files = srcFolder.listFiles() ;
}else {
files = srcFolder.listFiles(filenameFilter) ;
}
// 遍历
for(File f : files) {
// 创建目标文件
String destFileName = f.getName() ;
File destFile = new File(destFolder , destFileName) ;
// 复制文件
copyFile(f , destFile) ;
}
}
public static void copyFile(File srcFile , File destFile) throws IOException {
// 创建流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)) ;
// 一次读取一个字节数组复制文件
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
// 释放资源
bos.close() ;
bis.close() ;
}
}
参考文章:https://github.com/yangchong211/YCBlogs/blob/master/java/IO流知识