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(); } } }}
相关文章推荐
- java IO流文件的读写具体实例
- IntelliJ IDEA & Android Studio的调整优化
- Intellij IDEA控制台乱码解决方法
- idea中切换svn地址不起作用
- 使用Ant构建SSH项目(Intellij IDEA版)
- idea常用设置
- Intellij idea使用Git@Osc发布项目(干货)
- IntelliJ IDEA使用Application Server开发普通Java Web项目
- IntelliJ IDEA使用Maven快速开发Java Web项目,并添加JFinal依赖
- 在线安装 IntelliJ IDEA的 golang 插件
- 工具使用【IDEA】---plugins篇
- 工具使用【IDEA】---使用技巧
- Intellij IDEA整理
- 为了响应大家对IDEA 12的支持,特奉上一个解决中文字体问题的好东东
- IntelliJ Idea 常用快捷键列表
- 给大家推荐一个IDEA快捷键等的设置
- JAVA IO流的基本认识
- 使用IntelliJ IDEA 14和Maven创建java web项目
- IntelliJ Idea 常用快捷键列表
- idea代码调试debug篇