将源文件复制到目标文件,同时输出源文件内容,需要提供一个源文件和目标文件路径参数(如果不存在则自动创建)

    public  static void  copyTxt(String sourceFile, String targetFile){
try{
String encoding = "GBK";
File file = new File(sourceFile);
if(file.exists() && file.isFile()){ //判断文件是否存在
FileInputStream input = new FileInputStream(file); //字节流
FileOutputStream output = new FileOutputStream(new File(targetFile)); InputStreamReader read = new InputStreamReader (input,encoding);//字符流
OutputStreamWriter write = new OutputStreamWriter(output,encoding); BufferedReader bufferedReader = new BufferedReader (read);//缓冲流
BufferedWriter bufferedWriter = new BufferedWriter(write);
String line = "";
while ( (line = bufferedReader.readLine() )!= null ){
System.out.println(line);
bufferedWriter.write(line + "\n");
}
bufferedReader.close();
bufferedWriter.close();
}else {
System.out.println("找不到指定的文件");
}
}catch(IOException e){
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
}

最新文章

  1. [译]使用branch
  2. Qt界面中嵌入其他exe程序的界面,使用Qt5
  3. S2SH CRUD 整合
  4. cvInRangeS函数演示
  5. Google140道面试题
  6. swift 类和结构体
  7. DIV 布局 左中右
  8. css中var函数
  9. python调用ice接口
  10. php 多维数组 arrayList array()
  11. .NET中IDisposable接口的基本使用
  12. .24-浅析webpack源码之事件流compilation(2)
  13. Java Collections API和泛型
  14. linxu下redis安装实战
  15. python全栈开发day110-Flask基础语法
  16. 创建虚拟目录失败,必须为服务器名称指定“localhost”
  17. HTML中的相对路径与绝对路径(转)
  18. Lua语言特色
  19. 以太网安全技术ACL原理+配置
  20. Python习题(第3课)

热门文章

  1. 2018-2019-2 20165332 《网络对抗技术》Exp4 恶意代码分析
  2. 51nod1347思维
  3. HDU1166 敌兵布阵_线段树
  4. Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api(二十)
  5. 获得文件路径 _pgmptr, _makepath, _splitpath
  6. Double H2.0
  7. ios 怎么禁止点击子视图的时候不响应父视图的点击事件
  8. java的继承(编程思想)
  9. Alpha阶段贡献分配规则
  10. Alpha冲刺一 (9/10)