1.FileInStream属于java.io包 输入流
2.创建对象
通过调用一个带file类型的构造方法
通过调用一个带String类型的构造方法
3.常用方法
int类型read();每次从流管道中读取一个字节,返回code码
int类型 read(byte[])从流管道中读取若干个字节 存入这个数组,返回有效数字
int类型available();返回流管道中还有多少缓存的字节数
skip(long n )跳过几个字节
close()将流管道关闭—必须要执行,最好放在finally里,判断要严谨
怎么进行输入
public class TestFileInStream {
public static void main(String[] args) {
FileInputStream fis = null;
try {
//创建一个file对象
File file = new File(“D://Test//test.txt”);
fis = new FileInputStream(file);
byte[] b = new byte[3];
int count = fis.read();
while(count!=-1){
String value = new String(b);
System.out.println(value);
count = fis.read(b);
}
System.out.println(count);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.FileOutputStream 将文件写入文件中
public class TestFileoutStream {
public static void main(String[] args) {
File file = new File(“D://Test//test.txt”);
FileOutputStream fos = null;
{//如果存储多个,定义一个数组就可以了。
try {
fos = new FileOutputStream(file,true);
try {
fos.write(97);
fos.write(48);
fos.flush();
System.out.println(“写完了”);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
注意:这个不用因为你找不到文件而不去写,会默认在你的盘创建文件
fos = new FileOutputStream(file,true);如果不写true的话,每写入一个值都会默认覆盖
如果想存入多个数据的话,可以用byte数组去完成
例: byte[] b = new byte[97,98,99];
fos.write(b);
总结:多多复习,close判断逻辑要严谨。