liurunyu
7 天以前 fcc1cf275578bd3aa08ef50c6a611fb206b7aac9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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);
    }
 
}