Posted on 2011-08-15 00:42
java小爬虫 阅读(369)
评论(0) 编辑 收藏
读取文件的时候,要指定文件输入
流的编码格式。否则读取的中文文件就是乱码。
.txt unicode
.java 可以指定编码格式
.xml 有成熟的读写工具
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class FileTest {
static String fileName = "d:\\word.txt";
public static void main(String[] args) throws Exception {
System.out.println(readFile(fileName));
}
public static String readFile(String filePathAndName) {
String fileContent = "";
try {
File f = new File(filePathAndName);
if (f.isFile() && f.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(f), "unicode");
BufferedReader reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
fileContent += line;
}
read.close();
}
} catch (Exception e) {
System.out.println("读取文件内容操作出错");
e.printStackTrace();
}
return fileContent;
}
}