您的位置:首页 > 其它

IO流文件的拷贝

2016-06-06 10:08 253 查看
<pre name="code" class="java">package com.zhu.io;
import java.io.*;

/**
* Created by idea on 2016/6/6.
* @author 我就是你们的小星星
*
* 使用字节流拷贝文件(只能作用于纯文本)
*/
@SuppressWarnings("all")
public class CopyFile2 {
public static void main(String[] args) {
String srcPath = "d:/testio/a.txt";
String descPath = "d:/testio/c.txt";
copyFile(srcPath , descPath);
}
public static void copyFile(String srcPath , String descPath){
Reader reader = null;
Writer writer = null;
try {
File src = new File(srcPath);
File desc = new File(descPath);
reader = new BufferedReader(new FileReader(src));
writer = new BufferedWriter(new FileWriter(desc));
char [] data = new char [1024];
int len = 0 ;
while (-1 != (len = reader.read(data))) {
writer.write(data , 0 , len);
writer.flush();
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
writer = null;
}
if (reader != null ) {
reader.close();
reader = null;
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}


package com.zhu.io;import java.io.*;/** * Created by idea on 2016/6/6. * @author 我就是你们的小星星 */public class CopyFile { public static void main(String[] args) { String srcPath = "d:/testio/a.txt"; String descPath = "d:/testio/b.txt"; copyFile(srcPath , descPath);
} public static void copyFile(String srcPath , String descPath){ InputStream is = null; OutputStream os = null; try { File src = new File(srcPath); File desc = new File(descPath); is = new BufferedInputStream(new FileInputStream(src)); os = new BufferedOutputStream(new
FileOutputStream(desc)); byte [] data = new byte[1024]; int len = 0; while (-1 != (len = is.read(data))){ os.write(data , 0 , len); os.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally
{ try { if (os != null) { os.close(); os = null; } if (is != null) { is.close(); is = null; } } catch (IOException e) { e.printStackTrace(); } } }}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  idea io流 文件的拷贝