下面这个例子是在18.6.1的基础上, 展示了利用 StringReader从内从中将字符串读取, 并用read方法一个个读取字符输出
package com.cnsuning.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferInputFile {
public BufferInputFile() {
// TODO Auto-generated constructor stub
}
public static String read(String fileName) {
return read(new File(fileName));
}
public static String read(File inputFile) {
try {
FileReader fReader = new FileReader(inputFile);
BufferedReader bReader = new BufferedReader(fReader);
StringBuilder sb = new StringBuilder();
String line = "";
while (null != (line = bReader.readLine())) {
sb.append(line + "\n");
}
bReader.close();
return sb.toString();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
// System.out.println(read("D:\\work\\会员账户管理\\sales_svn.txt"));
StringReader sr = new StringReader(
read("D:\\work\\会员账户管理\\sales_svn.txt"));
int c = 0;
try {
while ((c = sr.read()) != -1) {
System.out.print((char) c);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}