File file = new File(path);
FileInputStream fis = new FileInputStream(file);
int ch = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((ch = fis.read()) != -1){
baos.write(ch);
}
baos.close();
fis.close();
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while((len = fis.read(buffer)) != -1){
baos.write(buffer,0,len);
}
fis.close();
baos.close();
File file = new File(path);
BufferedReader br = new BufferedReader(new Filereader(file));
String string = null;
StringBuffer sb = new StringBuffer();
while((string = br.readLine()) != null){
sb.append(string);
}
br.close();
还有一些其他的方式,根据具体的需求选择。对于文件不是很大的情况下,效率都是很高的。ByteArrayOutputStream是很好的工具,能用的话,可以放心使用。StringBuffer 和 StringBuilder 的效率也都是很高的,不用太纠结使用哪个。