网上找来的实例
import java.io.*;
public class ReadFile
{
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException
{
String path="D:\\f.txt";
ReadFile rf=new ReadFile();
rf.readFileByChars(path);
}
}
同时在D盘根目录新建一个f.txt文件
内容为:
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零
一二三四五六七八九十零一二三四五六七八九十零一二三四五六七八九十零