字节流
/**
* 按字节读取字符串
* 读完需要转码
* @param path
* @return
*/
public static String readerByte(String path){
File file = new File(path);
String str = null;
Long size = file.length();
byte[] date = new byte[size.intValue()];
try {
InputStream in = new FileInputStream(file);
in.read(date);
in.close();
str = new String(date,"GBK");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
字符流
/**
* InputStreamReader+BufferedReader读取字符串
* InputStreamReader类是从字节流到字符流的桥梁
* 按行读对于要处理的格式化数据是一种读取的好方式
* @param path
* @return
*/
public static String readerChar(String path){
int len=0;
StringBuffer str=new StringBuffer("");
File file=new File(path);
try {
FileInputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is,"GBK");
BufferedReader in = new BufferedReader(isr);
String line = null;
while( (line = in.readLine())!=null )
{
if(len != 0)
{
str.append("\r\n" + line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}