您的位置:首页 > 编程语言 > Java开发

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"));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: