java 读取中文文本代码
2013-02-19 20:57
363 查看
写道
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"));
}
}
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"));
}
}
相关文章推荐
- java 读取中文文本代码
- win7正常,java代码读取中文文本,到linux下乱码的解决办法
- java读取中文文件的代码
- java读取txt文本中如含有中文,可能会出现乱码,解决方案
- java中用字节流读取文本时中文乱码
- java中用字节流读取文本时中文乱码
- java 逐行读取txt文本如何解决中文乱码
- java文本相似度计算(Levenshtein Distance算法(中文翻译:编辑距离算法))----代码和详解
- java文本相似度计算(Levenshtein Distance算法(中文翻译:编辑距离算法))----代码和详解
- Java IO 读取/写入文件 中文乱码困难解决代码
- Java底层代码实现单文件读取和写入(解决中文乱码问题)
- java读取文本,插到mysql表中,出现问题。内存溢出。
- java读取properties中文乱码
- 【Java】中文转换汉语拼音代码
- 读取zip,不过滤中文名,即中文名称也能成功读取 分类: java 2009-11-06 22:48 296人阅读 评论(0) 收藏
- java读取http请求中的body实例代码
- Java读取文本文件中文乱码问题
- java读取txt文件代码片段
- 来段代码提提神-Java读取config.properties文件中的key=value
- 中文乱码-Javaweb-request读取时乱码解决方法