javaIOFileInputStream - juedaiyuer/researchNote GitHub Wiki
#IO-FileInputStream#
FileInputStream可以以字节流的形式读取文件内容
FileInputStream是InputStream的子类
##代码示例##
public static void method2(){
InputStream in = null;
try{
// BufferedInputStream和FileInputStream的区别
// BufferdInputStream有可能会读取比你规定的更多的东西到内存,以减少访问IO的次数
// 访问IO的次数越少,性能就越高,CPU和内存的速度远大于硬盘或其它外部设备的速度
// BufferedInputStream在小型文件中的性能无法体现出来
in = new BufferedInputStream(new FileInputStream("src/nomal_io.txt"));
byte [] buf = new byte[1024];
// 将文件中的内容读取到字节数组中
int bytesRead = in.read(buf);
while(bytesRead != -1)
{
for(int i=0;i<bytesRead;i++)
System.out.print((char)buf[i]);
bytesRead = in.read(buf);
}
}catch (IOException e)
{
e.printStackTrace();
}finally{
try{
if(in != null){
in.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
##代码实例二##
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by juedaiyuer on 16-9-28.
*/
public class testInputStream {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream(new File("/home/juedaiyuer/opensource/jdk1.8/README.html"));
try{
byte[] b = new byte[fis.available()];
fis.read(b);
fis.close();
String str2 = new String(b);
System.out.println(str2);
}catch (IOException e){
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
##source##
- 攻破JAVA NIO技术壁垒(上) evernote