try {
File file = new File("d:/1.txt");
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] b = new byte[(int)file.length()]; raf.read(b);
raf.close(); String s = new String(b, "UTF-8");
System.out.println(s);
}
catch (IOException ex) {
ex.printStackTrace();
}注:
d:/1.txt 应换成你自己的文件
UTF-8 应换成你的文件的编码方式。
File file = new File("d:/1.txt");
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] b = new byte[(int)file.length()]; raf.read(b);
raf.close(); String s = new String(b, "UTF-8");
System.out.println(s);
}
catch (IOException ex) {
ex.printStackTrace();
}注:
d:/1.txt 应换成你自己的文件
UTF-8 应换成你的文件的编码方式。
import java.util.*;
import java.io.*;public class Test
{
public static void main(String agrs[])
{
InputStreamReader isr = new InputStreamReader(new FileInputStream("test.txt"),"GBK");
// "GBK"是文件的编码方式,如果你的文件是UTF-8编码,那么就把"GBK"替换成"UTF-8"
// 如果是这样:new InputStreamReader(new FileInputStream("test.txt")),表示用系统默认编码 char buf[1024];
// 读文件
int count = isr.read(buf,1024);
// 构造String
String data = new String(buf, 0, count);
//...
}
}