IO编程——复制一个文件中的内容到另一个文件
public class TestIO {
public static void main(String[] args) {
File inputFile = new File("a.txt");//这个地方要考虑到a.txt和b.txt在项目中的位置
File outputFile = new File("b.txt");
//b.txt文件不存在,可以创建
if(outputFile.exists()){
}else{
try {
outputFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
InputStream inputStream = null;
OutputStream outputStream = null;
int temp;
try {
inputStream = new FileInputStream(inputFile);
outputStream = new FileOutputStream(outputFile);
//读取a.txt数据并写到b.txt文件中
while((temp = inputStream.read()) != -1){
outputStream.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
Runtime.getRuntime().exit(-1);
} catch(IOException e){
e.printStackTrace();
} finally{
try{
//一定要关闭,否则可能得不到结果
inputStream.close();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}
最新文章
- 安装opensuse时遇到的一些问题
- PHP基础面试题
- 【逐步完善】MySql索引相关
- 【转】Wireshark:“There are no interfaces on which a capture can be done ”
- codeforces 333B - Chips
- 《深度探索c++对象模型》chapter1关于对象对象模型
- 山东省赛J题:Contest Print Server
- linux 防火墙开端口(转)
- Java设计模式--Java Builder模式
- 使用mybatis插入自增主键ID的数据后返回自增的ID
- 10个技巧优化PHP程序Laravel 5框架
- beego框架的最简单登入演示
- bootstrap 解决弹出窗口(modal) 常见问题
- HTML5 full-screen全屏API
- Itween 动画插件中 的画线
- Java-jacob-文件转HTML
- win10 + VS2015 + 64位OSG3.4.0
- ES6基础一
- 仿饿了么增加购物车旋转控件 - 自带闪转腾挪动画 的button
- nginx / apache / tomcat /resin等 http server的benchmark性能测试方法