For example, when you want to get a text template. If the development environment and the production are different (I think it's almost different), the absolute path is environment-dependent, isn't it? You can write the location for each environment to the property, but it is better to have a small setting value, and if possible, include it in the jar. So, this time it is a utility to get the resources in the jar.
FileUtil.java
package jp.demo.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
private static final String CHARSET_UTF8 = "UTF-8";
private static final String LF = "\n";
/**
*Return line by line in List. (Excluding line feed characters)
* @param string
* @return
* @throws IOException
*/
public static List<String> getTextLines(String string) throws IOException {
List<String> list = new ArrayList<>();
try (InputStream is = ClassLoader.getSystemResourceAsStream(string);
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET_UTF8))) {
String line;
while ((line = br.readLine()) != null) {
list.add(line);
}
}
return list;
}
/**
*Return as a String. (Line feed code is LF)
* @param string
* @return
* @throws IOException
*/
public static String getTextStr(String string) throws IOException {
StringBuilder sb = new StringBuilder();
try (InputStream is = ClassLoader.getSystemResourceAsStream(string);
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET_UTF8))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line + LF);
}
}
return sb.toString();
}
}
Main.java
package jp.demo;
import java.io.IOException;
import java.util.List;
import jp.demo.util.FileUtil;
public class Main {
public static void main(String[] args) {
System.out.println("### START ###");
List<String> list = null;
String str = null;
try {
list = FileUtil.getTextLines("jp/demo/text1.txt");
str = FileUtil.getTextStr("jp/demo/text1.txt");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("getTextLines:");
list.forEach(System.out::println);
System.out.println("\ngetTextStr:");
System.out.println(str);
System.out.println("### END ###");
}
}
Character code: UTF-8, line feed code: CRLF The following is saved in src / main / resources / jp / demo / text1.txt.
text1.txt
row1 test test
row2 ah
row3 Mr. Yamada
Create a jar by maven install.
I was able to get the text file with both methods.
Execution result
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
### START ###
getTextLines:
row1 test test
row2 ah
row3 Mr. Yamada
getTextStr:
row1 test test
row2 ah
row3 Mr. Yamada
### END ###
C:\sts\workspace\simple_maven\target>
This is the case when the text is saved in the character code SJIS. ↓
Execution result
C:\sts\workspace\simple_maven\target>java -jar test-0.0.1-SNAPSHOT.jar
### START ###
getTextLines:
row1 test test
row2 ??????
row3 ?R?c????
getTextStr:
row1 test test
row2 ??????
row3 ?R?c????
### END ###
C:\sts\workspace\simple_maven\target>
I read the SJIS text in UTF-8, so the characters were garbled. Then, when a character that does not exist in SJIS (the famous one is Takahashi's "Taka") is output as a file with the character set SJIS, it is replaced with "?".
On Eclipse, Maven projects are src / main / ** java **, src / main / ** resources ** However, once you build it, it will be the same, so it is the same regardless of where you save the text.
Expand the jar and jp/demo/Look under
C:\Users\xxx\Desktop\test-0.0.1-SNAPSHOT\jp\demo directory
2019/06/12 00:57 <DIR> .
2019/06/12 00:57 <DIR> ..
2019/06/12 00:27 1,815 Main.class
2019/06/11 01:39 26 text1.txt
2019/06/12 00:57 <DIR> util
Recommended Posts