字节输入流InputStream
字节输出流OutputStream
FileInputStream:从文件系统中的某个文件中获得输入字节
用于读取注入图像数据之类的原始字节流
如果文件读完,返回值为-1
package www.zxx.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileINputDemo1 {
public static void main(String[] args) {
//创建一个FileInputStream对象
try {
FileInputStream fis = new FileInputStream("imooc.txt");
/*int n = fis.read();
while(n!=-1) {
System.out.print((char)n);
n=fis.read();
}*/
int n = 0;
while((n=fis.read())!=-1) {
System.out.print((char)n);
}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下面代码的输出结果:
package www.zxx.file;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileINputDemo1 {
public static void main(String[] args) {
//创建一个FileInputStream对象
try {
FileInputStream fis = new FileInputStream("imooc.txt");
byte[] b = new byte[100];
fis.read(b,0,2);
System.out.println(new String(b));
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
![点击并拖拽以移动 wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==](https://i-blog.csdnimg.cn/blog_migrate/311b6ad68acc2dda6b9f40faa1593634.png)