package com.dy.pipIrrParamSet.util;
|
|
import java.io.IOException;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.util.List;
|
|
/**
|
* @Author: liurunyu
|
* @Date: 2025/5/28 17:54
|
* @Description
|
*/
|
public class FileUtil {
|
/**
|
* @param absolutePath 绝对路径
|
* @param abstractPath 相对路径
|
* @param fileName 文件名
|
* @return
|
*/
|
public static Path getFilePath(String absolutePath, String abstractPath, String fileName){
|
boolean flag ;
|
do {
|
flag = false ;
|
if (absolutePath.endsWith("/")) {
|
absolutePath = absolutePath.substring(0, absolutePath.length() - 1);
|
flag = true ;
|
} else if (absolutePath.endsWith("\\")) {
|
absolutePath = absolutePath.substring(0, absolutePath.length() - 1);
|
flag = true ;
|
}
|
}while (flag) ;
|
|
do {
|
flag = false ;
|
if (abstractPath.startsWith("/")) {
|
abstractPath = abstractPath.substring(1);
|
flag = true ;
|
} else if (abstractPath.startsWith("\\")) {
|
abstractPath = abstractPath.substring(1);
|
flag = true ;
|
}
|
}while (flag) ;
|
abstractPath = "\\" + abstractPath ;
|
|
do {
|
flag = false ;
|
if (abstractPath.endsWith("/")) {
|
abstractPath = abstractPath.substring(0, abstractPath.length() - 1);
|
flag = true ;
|
} else if (abstractPath.endsWith("\\")) {
|
abstractPath = abstractPath.substring(0, abstractPath.length() - 1);
|
flag = true ;
|
}
|
}while (flag) ;
|
abstractPath = abstractPath + "\\" ;
|
|
do {
|
flag = false ;
|
if (fileName.startsWith("/")) {
|
fileName = fileName.substring(1);
|
flag = true ;
|
} else if (fileName.startsWith("\\")) {
|
fileName = fileName.substring(1);
|
flag = true ;
|
}
|
}while (flag) ;
|
String filePath = absolutePath + abstractPath + fileName ;
|
|
return Paths.get(filePath) ;
|
}
|
|
|
/**
|
* @param filePath 文件路径
|
* @return
|
*/
|
public static Path getFilePath(String filePath){
|
return Paths.get(filePath) ;
|
}
|
|
/**
|
* 读文件所有行
|
* @param filePath
|
* @return
|
* @throws IOException
|
*/
|
public static List<String> readFile(Path filePath) throws IOException {
|
// 读取所有行
|
return Files.readAllLines(filePath);
|
}
|
|
public static void writeFile(Path filePath, List<String> lines) throws IOException {
|
Files.write(filePath, lines);
|
}
|
|
public static void main(String[] args) throws Exception{
|
Path path = FileUtil.getFilePath("E:\\javaWorkspaces\\pipIrr\\pipIrr-platform\\pipIrr-global", "\\src\\main\\resources", "test.xml") ;
|
List<String> lines = readFile(path) ;
|
for(String line : lines){
|
System.out.println(line);
|
}
|
writeFile(path, lines);
|
}
|
|
}
|