判定是什么系统 ,不同的系统给与不同的路径
/**
* @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);
}
}
8577





