`
tao674613438
  • 浏览: 7494 次
社区版块
存档分类
最新评论

java 读取中文文本代码

    博客分类:
  • java
阅读更多

 写道
package com.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 LoadUserWords {
public static List<String> loadUserWords(InputStream input) {
String line;
List<String> userWords = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(input,
"UTF-8"));
while ((line = br.readLine()) != null) {
if (line.indexOf("//") != -1) {
line = line.substring(0, line.indexOf("//"));
}
line = line.trim();
if (line.length() != 0)
userWords.add(line.toLowerCase());
}
br.close();
} catch (IOException e) {
System.err.println("WARNING: cannot open user words list!");
}
return userWords;
}
}
   主类:写道
package com.ciku;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.util.LoadUserWords;

public class ReadUserWord extends LoadUserWords {

private static ReadUserWord singleInstance;
private static List<String> userWords = new ArrayList<String>();

public ReadUserWord() {
}

public static List<String> getUserWords() {
return userWords;
}

public static void setUserWords(List<String> userWords) {
ReadUserWord.userWords = userWords;
}

public synchronized static ReadUserWord getInstance() {
if (singleInstance == null) {
singleInstance = new ReadUserWord();
try {
singleInstance.read();
} catch (Exception e) {
}
}
return singleInstance;
}

public void read() {
userWords = loadUserWords((InputStream) this.getClass()
.getResourceAsStream("userword.txt"));
}

}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics