file--工具---实用小函数-写数据到本地

判定是什么系统 ,不同的系统给与不同的路径

    /**
     * @Describle: mac return  mac / window return win  / linux return lin
     * @Author:leiwang15
     */
    public static String systemGet() {
        String osName = System.getProperties().getProperty("os.name");
        if (osName != null && osName.contains("indow")) {
            return "win";
        }
        if (osName != null && osName.startsWith("Max")) {
            return "max";
        } else {
            return "lin";
        }
    }

使用写出String到本地


import java.io.*;
import java.util.List;

public class WriteOutUtils {
    private static String hs ;//不同系统给到不同的换行方式
    private static String codeWay ;//不同系统给到不同的编码方式
    static {
        String sys = OtherHelp.systemGet();
        if ("win".equals(sys)) {
            hs = "\n\r";
            codeWay = "GBK";
        } else {
            hs = "\n";
            codeWay = "UTF-8";
        }
    }
    /**
     * 顺序写出
     * @param  path 写入文件的位置和名称,可以是虚值,我的方法会帮助创建
     * @param li
     * @throws Exception
     * path可以填,不填写是我自己的默认值
     */

    public static void writeOut(String path, List<String> li) throws Exception {
        String pathDefault = "g:\\mv\\ts\\last.bat";
        File file;
        if (null == path) {
            file = FileUtils.checkExist(pathDefault);
        } else {
            file = FileUtils.checkExist(path);
        }

        FileOutputStream s = new FileOutputStream(file);
        //给win用,所以为GBK

        OutputStreamWriter w = null;
        w = new OutputStreamWriter(s, codeWay);
        BufferedWriter bw = new BufferedWriter(w);
        for (int i = 0; i < li.size(); i++) {
            bw.write(li.get(i));
            bw.write(hs);//使用系统判别
        }
        //先打开的后关闭
        bw.close();
        w.close();
        s.close();
    }
//按照追加的方式写入
    public static void appendWriteOut(String file, String conent) throws Exception {
        FileUtils.checkExist(file);
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
            out.write(conent+hs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        appendWriteOut("c:/rz/1.txt","haha");
    }
}

从命名

com.gzzbjkj.modules.monitor.m.Monitor1#rename
1 将隐藏文件去掉了的

  public static void rename(String directoryPath) {

        // 创建文件过滤器,过滤掉隐藏文件
        FileFilter fileFilter = file -> {
            // 过滤掉隐藏文件
            return !file.isHidden();
        };
        // 获取目录下所有文件
        List<File> files = FileUtil.loopFiles(directoryPath, fileFilter);

        // 遍历文件列表进行重命名操作
        for (File file : files) {
            // 获取文件名和扩展名
            String fileName = FileNameUtil.getName(file);
            String head = StrUtil.subBefore(fileName, '.', true);
            head = RSADecode(head) + '.' + FileNameUtil.extName(file);

            // 定义新文件路径
            String newFilePath = directoryPath + '/' + head;

            // 重命名文件
            FileUtil.rename(file, newFilePath, false);

        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值