Java——复制txt文件
2024-08-27 03:26:26
将源文件复制到目标文件,同时输出源文件内容,需要提供一个源文件和目标文件路径参数(如果不存在则自动创建)
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();
}
}
}
最新文章
- [译]使用branch
- Qt界面中嵌入其他exe程序的界面,使用Qt5
- S2SH CRUD 整合
- cvInRangeS函数演示
- Google140道面试题
- swift 类和结构体
- DIV 布局 左中右
- css中var函数
- python调用ice接口
- php 多维数组 arrayList array()
- .NET中IDisposable接口的基本使用
- .24-浅析webpack源码之事件流compilation(2)
- Java Collections API和泛型
- linxu下redis安装实战
- python全栈开发day110-Flask基础语法
- 创建虚拟目录失败,必须为服务器名称指定“localhost”
- HTML中的相对路径与绝对路径(转)
- Lua语言特色
- 以太网安全技术ACL原理+配置
- Python习题(第3课)
热门文章
- 2018-2019-2 20165332 《网络对抗技术》Exp4 恶意代码分析
- 51nod1347思维
- HDU1166 敌兵布阵_线段树
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api(二十)
- 获得文件路径 _pgmptr, _makepath, _splitpath
- Double H2.0
- ios 怎么禁止点击子视图的时候不响应父视图的点击事件
- java的继承(编程思想)
- Alpha阶段贡献分配规则
- Alpha冲刺一 (9/10)