如何将单词写入excel文件(行和列)和unicode字符?使用Java程序

时间:2016-02-04 04:56:00

标签: java excel dictionary unicode

我的程序是从文本文件中找到重复的单词,并显示从最大到最小顺序的重复单词计数。并将其保存在工作簿中。

"குழந்தை6

அவளுடைய4" 这些是我从文件中输出的示例。我将它们保存为.xls格式。 问题是当我在excel中打开它时它没有识别unicode格式,当我在选项中打开excel(更改UTF8)时,我需要打开并直接显示unicode格式。

BufferedWriter writer = Files .newBufferedWriter(Paths.get(fileNameOutput), StandardCharsets.UTF_8))

我使用UTF8仍然没有改成unicode格式。

我需要将Serial no words count计为列标题

序列无字数

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(); 
} 
 } 
 } 
  } }
 } 

0 个答案:

没有答案