- 1.字节流写数据:
- 2.字节流写输入的三种方式:
- 3.如果:字节流遇到异常:
- 4字节流读数据:
- 5.字节流读数据:数组
FileOutputStream f1=new FileOutputStream(路径);
f1.write(78); 输入文件中文本,输出:N(字符)
f1.close(); // 释放资源(必须的)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class bty {
public static void main(String[] args) throws IOException {
FileOutputStream f1=new FileOutputStream("D:\javac\projects\input\fos.txt");//有异常:alt+enter 添加throws FileNotFoundException
f1.write(78); //有异常:加throws IOException
f1.write(97);
f1.close(); // 释放资源(必须的)
}
}
结果:
前:
后:
1.f1.write(78);
2.byte[] bys={98,97,100,65,44};
f1.write(bys);
3.byte[] ree=“abcdef”.getBytes();
f1.write(ree,1,3);//1是开始索引为1的字符串数组开始,长度为3
输出是:bcd
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class bty {
public static void main(String[] args) throws IOException {
FileOutputStream f1=new FileOutputStream("D:\javac\projects\input\fos.txt");//有异常:alt+enter 添加throws FileNotFoundException
// f1.write(78); //有异常:加throws IOException
// f1.write(97);
byte[] bys={98,97,100,65,44};
byte[] ree="abcdef".getBytes();
f1.write(bys);
f1.write(ree,1,3);
f1.close();
}
}
结果:
3.如果:字节流遇到异常:
加:tiy catch
finally
FileInputStream
创建字节输入流对象:
FileInputStream f1=new FileInputStream(“D:javacprojectsinputfos.txt”);
读取文件内容:
f1.read();// 可以持续读取数据
如果:读取完后:遇到空的字符:显示-1
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class kert {
public static void main(String[] args) throws IOException {
FileInputStream f1=new FileInputStream("D:\javac\projects\input\fos.txt");
int by=f1.read(); //读取第一个
System.out.println(by);
System.out.println((char)by);
int bc=f1.read(); //读取第二个
System.out.println(bc);
int bce=f1.read(); //读取第三个
System.out.println(bce);
}
}
结果:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class kert {
public static void main(String[] args) throws IOException {
FileInputStream f1=new FileInputStream("D:\javac\projects\input\fos.txt");
// int by=f1.read();
// System.out.println(by);
// System.out.println((char)by);
// int bc=f1.read();
/// System.out.println(bc);
// int bce=f1.read();
// System.out.println(bce);
int by;
while((by=f1.read())!=-1){
System.out.print((char)by); //println 和print 区别:print:一行;println:分行
} //(char)by :改变数据类型
f1.close();
}
}
5.字节流读数据:数组
FileInputStream f1=new FileInputStream(“D:javacprojectsinputfos.txt”);
byte[] bys=new byte[25];
int len=f1.read(bys);//从输入流最多读取25个字节,到字节数组里,
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class wert {
public static void main(String[] args) throws IOException {
FileInputStream f1=new FileInputStream("D:\javac\projects\input\fos.txt");
byte[] bys=new byte[25];
int len=f1.read(bys);//从输入流最多读取1024个字节,到字节数组里
System.out.println(len);
len=f1.read(bys);
System.out.println(len);
len=f1.read(bys);
System.out.println(len);
// System.out.println((char)bys[0]);
// System.out.println(new String(bys));//输出:字符串
// System.out.println((char)bys[2]);
//f1.read(bys):读取的数据放在数组中
// while((len=f1.read(bys))!=1){
// System.out.println(new String(bys,0,len));
}
}
结果:
每一次:
byte[] bys=new byte[25];
len=f1.read(bys); //从数据从第一个读取
System.out.println(len);
len=f1.read(bys); //从数据前面已经读了25个,从25个开 始读取读取直到没数据,返回 -1
System.out.println(len);
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class wert {
public static void main(String[] args) throws IOException {
FileInputStream f1=new FileInputStream("D:\javac\projects\input\fos.txt");
byte[] bys=new byte[25];
int len;
while((len=f1.read(bys))!=-1){
System.out.println(new String(bys,0,len));//字符串
}
}}
结果:



