1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 获取项目根路径
String configPath=System.getProperty("user.dir")+"/config/config.json";
String filePath =new ClassPath

new ClassPathResource("/").getFile().getAbsolutePath();
new ClassPathResource("").getFile().getAbsolutePath();

//获取src目录
Test t = new Test();
InputStream resourceAsStream = t.getClass().getResourceAsStream("/a.txt");


//加斜杆为项目根路径,不加斜杆为当前路径
System.err.println(t.getClass().getResource("/"));
System.err.println(Test.class.getClassLoader().getResource(""));

int x = 0;
byte[] bytes = new byte[100];
while ((x = resourceAsStream.read(bytes)) > 0) {
System.out.println(new String(bytes, 0, x));
}

image-20200925205735253

1
2
//升序排序
list.sort(Map.Entry.comparingByKey());

读取配置文件中的JSON数组文件

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

[
{
"id": 1,
"wcCode": "1100",
"isoCode": "TW",
"name": "台湾",
"currencyCode": "TWD",
"currencySymbol": "NT$",
"rateUsd": 0.03239,
"ratePcost": 0.15,
"shipping": 5.4,
"rateRefuse": 0.16,
"createTime": 1587881532000
},
{
"id": 2,
"wcCode": "1104",
"isoCode": "MY",
"name": "马来西亚",
"currencyCode": "MYR",
"currencySymbol": "RM",
"rateUsd": 0.2417,
"ratePcost": 0.15,
"shipping": 7,
"rateRefuse": 0.2,
"createTime": 1587881532000
},
{
"id": 3,
"wcCode": "1105",
"isoCode": "SG",
"name": "新加坡",
"currencyCode": "SGD",
"currencySymbol": "S$",
"rateUsd": 0.7374,
"ratePcost": 0.15,
"shipping": 9,
"rateRefuse": 0.2,
"createTime": 1587881532000
}
]
configData = Files.lines(Paths.get(config), StandardCharsets.UTF_8)
.collect(Collectors.joining(System.lineSeparator()));
# Files.lines()
一行一行读取文件

#Collectors.joining()
拼接,有三个重载方法,底层实现是StringBuilder,通过append方法拼接到一起,并且可以自定义分隔符(这个感觉还是很有用的,很多时候需要把一个list转成一个String,指定分隔符就可以实现了,非常方便)、前缀、后缀。

# System.lineSeparator() System.getProperty("line.separator")
行分隔符(换行符) 那么其与‘\n’ 有什么区别呢。系统的环境变量,那么系统就有肯能有差别 一般的为Window 下和Unix下其所表示意义就会不同。
这样写的话,则剔除了平台无关性,写一次代码跑通在Linux上和Window上都能够运行。屏蔽了 Windows和Linux的区别 ,更保险一些.

image-20200925221124251

读取配置文件中的JSON文件

1
2
3
4
5
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
1
2
3
String content = FileUtils.readFileToString(new File("./config/config2.json"), "UTF-8");
System.out.println(content);
System.out.println(JSONObject.parseObject(content, ConfigEntity.class));

image-20200925224309888


动态修改配置文件

1
2
3
4
5
6
7
8
9
10
11
@Component
public class AppRunner implements ApplicationRunner {

@Resource
private MonitorThread monitorThread;

@Override
public void run(ApplicationArguments args) throws Exception {
monitorThread.start();
}
}
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
@Slf4j
@Component
public class MonitorThread extends Thread {

@Resource
private Settings settings;

@Resource
private Environment env;

@Override
public void run() {
try {
WatchService watchService= FileSystems.getDefault().newWatchService();
String rootPath = env.getProperty("LOG_HOME");
log.info("monitor root path: " + rootPath);
Path path= Paths.get(rootPath);
path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);

while (true) {
WatchKey watchKey = watchService.take();
for (WatchEvent event : watchKey.pollEvents()) {
log.info("配置文件修改");
settings.update();
}

watchKey.reset();

}

} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
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
@Slf4j
@Component
public class Settings {

private final Map<String, SettingEntity> isoMap = new HashMap<>();

public Settings() {
loadSettings();
}

private void loadSettings() {
File jsonFile;
String content;
try {
// TODO: move root path to config file
String fileName = ("/root/config/shipping-formula.json");
jsonFile = new File(fileName);
content = FileUtils.readFileToString(jsonFile, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
return;
}

JsonSettingEntity jsonSetting = JSONObject.parseObject(content, JsonSettingEntity.class);

for (SettingEntity entity : jsonSetting.getShippingInfo()) {
isoMap.put(entity.getIsoCode(), entity);
}

log.debug(jsonSetting.toString());
}

public void update() {
loadSettings();
}
}
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
v 可视模式
V 可视行模式 j k 向上 向下移动
ctrl +V {} 向左向右选择段落

删除后面一个字母 x 5x
% 选中括号
x可以将 V选中模式直接删除

D关光标到行尾

先d 后w删除一个单词

d0删除到起始位置

r R替换

w e 一个单词一个单词跳

0 行首 $行尾
>> <<缩近
. c重复执行
单词快速匹配 * 向后查找#向前查找

%s///g 替换




插入模式

i
I 行首插入文本
a 当前字符后
A 行末
o 当前行后面
O 当前行前面

末行模式

:e .
:n demo.py 新建

:w zzz.py 阶段性备份
:sp[文件名]
:vs[.]

ctrl +w w 切换
grep -n 'xxx' a.txt 包含 xxx的包括行号
grep -i 忽略大小写

grep ^a 以a开头的行

grep a$ 以a结尾的行

ln -s 被链接的源文件(绝对路径) 链接文件

win +v