如何写excel文件(行和列)和unicode字符的单词?使用Java程序
我的程序是从文本文件中查找重复的单词,并显示重复的单词数从最大到最小的顺序,并将其保存在工作簿中。
“குழந்தை6
அவளுடைய4“这些是我从一个文件输出的示例,我将它们保存为.xls格式。问题是,当我在Excel中打开这些文件时,它无法识别unicode格式,当我通过选项打开excel(更改UTF8)它确实需要打开并直接显示unicode格式。
BufferedWriter writer = Files .newBufferedWriter(Paths.get(fileNameOutput), StandardCharsets.UTF_8))
我用UTF8,它仍然不会变成unicode格式。
我需要包括序列号没有字作为列标题
连续没有字数
1குழந்தை6
2அவளுடைய4
像这样我需要
这是我的完整代码
import java.io.*; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.Map.Entry; public class maxoccurrence2 { final static Charset ENCODING = StandardCharsets.UTF_8; public Map<String, Integer> getWordCount(String fileName) { FileInputStream fis = null; DataInputStream dis = null; BufferedReader br = null; Map<String, Integer> wordMap = new HashMap<String, Integer>(); try { fis = new FileInputStream(fileName); dis = new DataInputStream(fis); br = new BufferedReader(new InputStreamReader(dis)); String line = null; while ((line = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, " "); while (st.hasMoreTokens()) { String tmp = st.nextToken().toLowerCase(); if (wordMap.containsKey(tmp)) { wordMap.put(tmp, wordMap.get(tmp) + 1); } else { wordMap.put(tmp, 1); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (Exception ex) { } } return wordMap; } public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap) { Set<Entry<String, Integer>> set = wordMap.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return (o2.getValue()).compareTo(o1.getValue()); } }); return list; } public static void main(String a[]) throws IOException { String path = "D:\\Projectpath\\"; String fileNameIn; File folder = new File(path); File[] listOfFiles = folder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile()) { fileNameIn = path + listOfFiles[i].getName(); maxoccurrence2 mdc = new maxoccurrence2(); Map<String, Integer> wordMap = mdc.getWordCount(fileNameIn); List<Entry<String, Integer>> list = mdc.sortByValue(wordMap); String fileNameOutput = path + "\\Projectoutput\\"+listOfFiles[i].getName() .substring(0, listOfFiles[i].getName().length() - 4) + "output.xls"; try (BufferedWriter writer = Files .newBufferedWriter(Paths.get(fileNameOutput), StandardCharsets.UTF_8)) { for (Map.Entry<String, Integer> entry : list) { writer.write(entry.getKey() + " " + entry.getValue()); writer.newLine(); } } } } } }