博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java文件拷贝方式
阅读量:4107 次
发布时间:2019-05-25

本文共 1299 字,大约阅读时间需要 4 分钟。

第一种使用FileInputstream读取,使用FileOutputStream写入到文件中

FileInputStream input = null;		FileOutputStream output = null;		String path="D:\\java\\";		String fileName="test.txt";		String newFilename=System.currentTimeMillis()+".txt";		try {			input = new FileInputStream(new File(path+fileName));			output = new FileOutputStream(new File(path+newFilename));			byte[] bt = new byte[1024];			int realbyte = 0;			while((realbyte=input.read(bt))>0){			   output.write(bt, 0, realbyte);			}		} catch (Exception e) {			e.printStackTrace();		} finally {			try {				if (input != null) {					input.close();				}				if (output != null) {					output.close();				}			} catch (IOException e) {				e.printStackTrace();			}		}

下面是try-with-resource模式,这种模式中对应类的接口要实现java.lang.AutoCloseable接口或者它的继承者java.io.Closeable接口,而FileInputStream 对应的抽象类InputStream就实现了java.io.Closeable接口。

String path="D:\\java\\";		String fileName="test.txt";		String newFilename=System.currentTimeMillis()+".txt";		try		( FileInputStream input = new FileInputStream(new File(path+fileName));		  FileOutputStream output = new FileOutputStream(new File(path+newFilename)))		{ 			byte[] bt = new byte[1024];			int realbyte = 0;			while((realbyte=input.read(bt))>0){			   output.write(bt, 0, realbyte);			}					}catch (Exception e) {			e.printStackTrace();		}

 

转载地址:http://uwtsi.baihongyu.com/

你可能感兴趣的文章
常用js收集
查看>>
mydata97的日期控件
查看>>
如何防止sql注入
查看>>
maven多工程构建与打包
查看>>
springmvc传值
查看>>
Java 集合学习一 HashSet
查看>>
在Eclipse中查看Android源码
查看>>
Android-Socket登录实例
查看>>
Android使用webservice客户端实例
查看>>
层在页面中的定位
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第四章 - 程序计数器
查看>>
第七章 - 本地方法栈
查看>>