java写文件路径 java程序运行步骤

0x 01:file inputstream/file output stream字节流***文件。private static void streamCopyFile(File srcFile, File desFile) { try{ // 使用字节流进行文件*** FileInputStream fi = new FileInputStream(srcFile); FileOutputStream fo = new FileOutputStream(desFile);...

0x 01:
file inputstream/file output stream字节流***文件。

private static void streamCopyFile(File srcFile, File desFile) { try{ // 使用字节流进行文件*** FileInputStream fi = new FileInputStream(srcFile); FileOutputStream fo = new FileOutputStream(desFile); Integer by = 0; //一次读取一个字节 while((by = fi.read()) != -1) { fo.write(by); } fi.close(); fo.close(); }catch(Exception e){ e.printStackTrace(); } }

0x02:
缓冲输入流/缓冲输出流用于***文件的有效字节流

private static void bufferedStreamCopyFile(File srcFile, File desFile){ try{ // 使用缓冲字节流进行文件*** BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile)); byte[] b = new byte[1024]; Integer len = 0; //一次读取1024字节的数据 while((len = bis.read(b)) != -1) { bos.write(b, 0, len); } bis.close(); bos.close(); }catch(Exception e){ e.printStackTrace(); }}

0x03: FileReader/FileWriter字符流***文件

private static void readerWriterCopyFile(File srcFile, File desFile){ try{ // 使用字符流进行文件***,注意:字符流只能***只含有汉字的文件 FileReader fr = new FileReader(srcFile); FileWriter fw = new FileWriter(desFile); Integer by = 0; while((by = fr.read()) != -1) { fw.write(by); } fr.close(); fw.close(); }catch(Exception e){ e.printStackTrace(); }}

0x04:
用于文件***的缓冲读取器/缓冲写入器高效字符流

private static void bufferedReaderWriterCopyFile(File srcFile, File desFile){ try{ // 使用带缓冲区的高效字符流进行文件*** BufferedReader br = new BufferedReader(new FileReader(srcFile)); BufferedWriter bw = new BufferedWriter(new FileWriter(desFile)); char[] c = new char[1024]; Integer len = 0; while((len = br.read(c)) != -1) { bw.write(c, 0, len); } //方式二 /* String s = null; while((s = br.readLine()) != null) { bw.write(s); bw.newLine(); } */ br.close(); bw.close(); }catch(Exception e){ e.printStackTrace(); }}

0x05: NIO实现文件***(使用transferTo或transferFrom)

public static void NIOCopyFile(String source,String target) { try{ //1.采用RandomAccessFile双向通道完成,rw表示具有读写权限 RandomAccessFile fromFile = new RandomAccessFile(source,"rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile(target,"rw"); FileChannel toChannel = toFile.getChannel(); long count = fromChannel.size(); while (count > 0) { long transferred = fromChannel.transferTo(fromChannel.position(), count, toChannel); count -= transferred; } if(fromFile!=null) { fromFile.close(); } if(fromChannel!=null) { fromChannel.close(); } }catch(Exception e){ e.printStackTrace(); }}

0x06: java.nio.file.Files.copy()实现文件***,其中第三个参数决定是否覆盖。

public static void copyFile(String source,String target){ Path sourcePath = Paths.get(source); Path destinationPath = Paths.get(target); try { Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); }}

本文来自又何必自找失落╮投稿,不代表舒华文档立场,如若转载,请注明出处:https://www.chinashuhua.cn/24/584212.html

打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
() 0
上一篇 06-09
下一篇 06-09

相关推荐

  • 魔兽世界怀旧服魔盒插件安装路径详细教程

    1没有选择正确的文件夹,编辑后的魔兽世界文件夹路径为C \ war craft 2世界。那么怀旧服魔盒的安装路径就是C \魔兽世界\classic_。如果是正规的服务路径,那就是C \魔兽世界\retail_\了。1.首先双击桌面上大脚怀旧的快捷方式启动大脚插件,如下图所示。2然后,在打开的窗口中

    2023-07-23 13:37:01
    983 0
  • 国内yum源镜像地址 linux yum安装软件默认路径

    看到这就是感觉不得不有,不多说,直接卷。构建本地yum源本地yum源是指只有构建yum源的服务器可以使用,其他服务器不能使用。构建本地yum源的所有步骤如下:#准备好一个centos 的镜像,我这里是CentOS-7-x86_***-DVD-1810.iso# 在/local-yum目录创建挂载镜像的文件夹# 将iso镜

    2023-07-23 05:58:01
    515 0
  • 360文件恢复软件的安装路径 相册图片永久删除了恢复方法

    有时候,我们在操作自己的电脑时,会不小心删除了不想删除的数据,而回收站却被清空空。我们做什么呢这时候就需要恢复这些数据了。今天,我们将介绍如何恢复这些误删除的数据。1.从回收站还原数据。当用户不小心删除了一个文件,很可能只是删除到了“回收站”。如果您尚未清除

    2023-07-15 01:15:01
    453 0
  • mac路径查找 linux退出vim编辑方法

    ProFind Mac是一款自然语义搜索软件,可以使用多种搜索条件在Mac中搜索文件,包括隐藏文件(文件夹)和压缩包。而且这个软件内置了文件预览、语义语法参考、文件操作等功能。,可以说是配合档案管理使用的利器。打开ProFind,在搜索结果的左栏可以看到每个时间段修改的条件过滤

    2023-07-12 19:13:01
    926 0

评论列表

联系我们

在线咨询: QQ交谈

邮件:admin@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信