几个主要常见流的使用和介绍;

缓冲流:

 *BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter

作用:提供流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区。默认情况下是8kb

每当读入缓冲区的文件满时它才一次性读入写出,也可以手动调用flush刷新缓冲区,直接输出。

注意:1.缓冲流是处理流,用它时要创建相对的节点流,包裹于节点流外;

列:BufferedReade br = new BufferedReader(new FileReader("F:\\EclipseTest\\ioTest\\你好.txt"));
       BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\EclipseTest\\ioTest\\你好(buffer).txt"));

           2.缓冲流中的 读入操作可以使用readline方法,读入一整行字符串;用字符串接受,如果字符串等于null则输出到最后;

            写出时注意加入换行符\n或者调用newline方法也是换行;

列:

String s;
while((s=br.readLine())!=null){//文件中出现中问就会有乱码情况,改成utf-8可以解决,但前面写的程序用gbk编译的所有一改前面就也出问题,或许可以用转化流
bw.write(s);
bw.newLine();
}

转换流:

作用:字节流操作的数据都是字符时转化为字符流更为高效

           解决乱码问题,实现编码和解码的功能

  • InputStreamReader将InputStream转换为Reader
  • OutputStreamWriter将Writer转换为OutputStream
  • 使用的构造器
  • public OutputStreamWriter(OutputStream out)
  • public OutputSreamWriter(OutputStream out,String charsetName)
  • 后面的参数是定义编码集的,如果不写就使用默认的编码集

常见的编码集:

    • ASCII:美国标准信息交换码。用一个字节的7位可以表示。
    • ISO8859-1:拉丁码表。欧洲码表 。用一个字节的8位表示。
    • GB2312:中国的中文编码表。最多两个字节编码所有字符。
    • GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码。
    • Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示。
    • UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。    

对象流:

当文件的输入输出都是对象时,为了方便操作,使用对象流,把对象化成一系列二进制数,再还原,实现这样的操作,而为了保证对象可以化成二进制数,此对象必须是可序列化的(implements Serializable)必须要继承此接口,此接口什么也没有,唯一作用就是标识这是一个可序列化的类,还得提供一个全局常量public static final long serialVersionUID = 45135486731L;后面这串数字自己随意定,末尾要加大写L,为了还原的时候不出错加上这串数字的作用;

对象流列:

public class ObjectStream {

public static void main(String[] args) throws IOException, ClassNotFoundException {
student zz =new student(18,"zz");                              //先造一个对象
student yy =new student(18,"yy");
student xx =new student(18,"xx");
File a=new File("F:\\EclipseTest\\ioTest\\object.txt");;
FileOutputStream ops = new FileOutputStream(a);
ObjectOutputStream oos=new ObjectOutputStream(ops);
oos.writeObject(zz);
oos.writeObject(yy);
oos.writeObject(xx);                                                 //把对象写入文件
FileInputStream fis =new FileInputStream(a);
ObjectInputStream ois =new ObjectInputStream(fis);
student zz1=(student)ois.readObject();                    //读对象;并赋给对象变量
student yy1=(student)ois.readObject();
System.out.println(zz1.age);
System.out.println(zz1.name);
System.out.println(yy1.age);
System.out.println(yy1.name);
ops.close();
oos.close();
}

}

class student implements Serializable{
public static final long serialVersionUID = 45135486731L;
int age;
String name;

public student() {

}

public student(int age, String name) {
this.age = age;
this.name = name;
}

public void read() {
System.out.println("读书");
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

 

posted @   天庭保安猪八戒  阅读(159)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
点击右上角即可分享
微信分享提示