我正在写一个简单的程序,应该将excel页面从希伯来语翻译成英语。 为此,代码读取每个单元格的内容,并将其与从简单的csv文件中获取信息的地图进行比较。 从IntelliJ运行程序,该程序完美地运行并做它应该做的事情,但是将它编译成jar,程序不会这样做。
//Code for loading the csv contents to a map
private static Map<String,String> getLocalization(String pathToJar) {
String path = null;
path = pathToJar + "localization.csv";
String line = "";
HashMap<String, String> list = new HashMap<>();
try {
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] array = line.split(",");
list.put(array[0], array[1]);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
//Code for loading an Excel file and translating it
private static boolean updateExcel(Map<String,String> translation, String filepath, String pathToJar) {
String path = pathToJar + "temp\\week.xlsx";
//Read Excel document first
FileInputStream input_document = null;
XSSFWorkbook my_xlsx_workbook = null;
try {
input_document = new FileInputStream(new File(path));
// convert it into a POI object
my_xlsx_workbook = new XSSFWorkbook(input_document);
} catch (IOException e) {
e.printStackTrace();
}
// Read excel sheet that needs to be updated
XSSFSheet my_worksheet = null;
if (my_xlsx_workbook != null) {
my_worksheet = my_xlsx_workbook.getSheetAt(0);
}
for (Row cells : my_worksheet) {
String name = "";
String shortCode = "";
//Get the row object
Row row = cells;
//Every row has columns, get the column iterator and iterate over them
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
//Get the Cell object
Cell cell = cellIterator.next();
//check the cell type and process accordingly
switch (cell.getCellType()) {
case STRING:
for (Map.Entry<String, String> entry : translation.entrySet()) {
if (cell.getStringCellValue().contains(entry.getKey())) {
cell.setCellValue(entry.getValue());
}
}
break;
}
}
}
my_worksheet.autoSizeColumn(1);
FileOutputStream outFile = null;
try {
if (input_document != null) {
input_document.close();
}
File finishedFile = new File(path);
outFile = new FileOutputStream(finishedFile);
my_xlsx_workbook.write(outFile);
outFile.close();
finishedFile.renameTo(new File(filepath));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
csv文件类似于: hebrew_word,english_word hebrew_word,english_word hebrew_word,english_word ...
我查了几件事: 1.正在从文件中读取地图(尝试将其编码为UTF-8) 2. IntelliJ的设置设置为UTF-8
我相信这是一个编码问题,将地图的键输出到excel单元格会显示一堆特殊字符而不是单词。
如果还有其他需要的信息,请告诉我,并提前致谢。
FileReader
是一个旧的实用程序类,它使用默认的平台编码。在IntelliJ中运行,你说它被设置为UTF-8,就像文件的编码一样。在IntelliJ之外它取决于机器。 Windows到目前为止不使用UTF-8。
从java 8开始,可以用Files.lines
做到这一点:
try {
Path p = Paths.get(path);
Files.lines(p)
.map(line ->line.split(",\\s*"))
.filter(array -> array.length >= 2)
.forEach(array -> list.put(array[0], array[1]));
} catch (IOException e) {
e.printStackTrace();
}
Files.lines
也可以通过Charset,但新的默认值是UTF-8。