FileUtil + FileUtils常用写法参考

FileUtil + FileUtils常用写法参考

文件权限
参考资料: https://www.breakyizhan.com/java/5013.html
文件与文件夹在这里是一回事。

	public static boolean setExecutable(String path) {
        File file = FileUtils.getFile(path);
        file.setReadable(true, false);
        return file.setExecutable(true, false);
    }

创建文件:

file.createNewFile()

创建文件夹:

dir.mkdirs()

文件与文件夹的删除:

file.delete()

创建新file的时候,安全上建议使用FileUtils.getFile(String path),不要直接new

文件信息:
获取最近的修改时间:

file.lastModified()

返回值是long,即毫秒数,作为参考:现在时刻的毫秒数为:System.currentTimeMillis();
二者格式保持一致

写文件:以append的形式不断在文件末尾添加

	try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)))) {
            writer.write(str);
        } catch (IOException e) {
            logger.error("Failed to close resource", e);
        }

冲洗的方式

		try (FileWriter writer = new FileWriter(file)) {
            writer.write("");
            writer.flush();
        } catch (IOException e) {
            logger.error("IOException in writing file ", e);
        }

多个文件(fpath)内容统一集成到一个文件(resultFile)中,使用性能更高的FileChannel

		File resultFile = FileUtils.getFile(resultPath);
		//向resultFileChannel写入字节流
        try (FileChannel resultFileChannel = new FileOutputStream(resultFile, true).getChannel()) {
            for (int i = 0; i < fpaths.length; i++) {
//建立blk,从blk中读取字节流写入resultFileChannel
                try (FileChannel blk = new FileInputStream(files[i]).getChannel()) {
// 在result的末尾插入blk大小的字节流
                    resultFileChannel.transferFrom(blk, resultFileChannel.size(), blk.size());
                } catch (IOException e) {
                    logger.error("IOException occurs", e);
                }
            }
        }

查询FileChannel:https://blog.csdn.net/developlee/article/details/90543160
参考:https://blog.csdn.net/akon_vm/article/details/7429245
FileChannel在读写性能上优于RandomAccessFile,是所谓的内存映射文件

读文件:

	StringBuffer sb = new StringBuffer();
    try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
        String str = null;
        str = reader.readLine();
        if (str != null) {
            sb.append(str);
        }
        str = reader.readLine();
        while (str != null) {
            sb.append(System.lineSeparator()).append(str);
            str = reader.readLine();
        }
    } catch (IOException e) {
        logger.error("IOException occurs!", e);
        sb.setLength(0);
    }
    return sb.toString();

使用RandomAccessFile从后向前读一个固定的值,比如这里是10:

	long pos = 0L;
    try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
        long len = raf.length();
        if (len == 0) {
            return 0;
        } else {
            pos = len - 1;
            while (pos-- > 0) {
                raf.seek(pos);
                if (raf.read() == 10) {
                    break;
                }
            }
            return pos + 1;
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值