您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java如何通过字节流实现文件的拷贝

2024/3/19 14:37:03发布26次查看
通过字节流实现文件的拷贝
/** * 通过字节流实现文件的拷贝 * @param sourcepath 源文件路径 * @param targetpath 目标文件路径 */ public static void copyfilebystream(string sourcepath,string targetpath){ //源文件路径 file source = new file(sourcepath); //目标文件路径 file target = new file(targetpath); //如果源文件不存在则不能拷贝 if(!source.exists()){ return; } //如果目标文件目录不存在则创建 if(!target.getparentfile().exists()){ target.getparentfile().mkdirs(); } try { //实现文件的拷贝 inputstream inputstream = new fileinputstream(source); outputstream outputstream = new fileoutputstream(target); int temp = 0; //每次读取1024个字节 byte[] data = new byte[1024]; //将每次读取的数据保存到字节数组里面,并且返回读取的个数 while ((temp = inputstream.read(data)) != -1){ //输出数组 outputstream.write(data,0,temp); } inputstream.close(); outputstream.close(); } catch (ioexception e) { e.printstacktrace(); } }
以上就是java如何通过字节流实现文件的拷贝的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product