java基础学习笔记5

java基础学习笔记5

所有流类型都继承以下四个抽象流类型

字节流字符流
输入InputStreamReader
输出OutputStreamWriter

flush colse

fulsh会把缓存区的内容直接写入文件,不管缓存区满没满。

1.FileReader

FileReader:读一个文件,读出来是int类型,char转换一下

    public static void main(String[] args) throws IOException {
        int c = 0;
        FileReader a = new FileReader("F:\\filetest\\ufo.txt");
        while ((c = a.read()) != -1) {
            System.out.println((char)c);
        }
        a.close();
    }
2.FileWriter

FileWriter:往文件写数据进去,

写入的int,会转成unicode对应的字符

unicode对照表:https://blog.csdn.net/hherima/article/details/9045861

写入的string,直接就是string

    public static void main(String[] args) throws IOException {
       FileWriter a=new FileWriter("F:\\filetest\\ufo.txt");
       for(int i=0;i<60000;i++){
           a.write(i);
       }
       a.close();
    }

image-20200930192825101

10进制9398=16进制24B6 unicode代表Ⓐ

FileWriter写9398进去为Ⓐ,FileOutputStream写9398进去为?

FileReader读Ⓐ出来是9398 ,FileInputStream读Ⓐ出来为226

因为FileReader可以读16位,FileInputStream只能读8位,

9398是10进制,其实是存的16进制的24B6,FileInputStream会当成十六进制的24和B6 ,转成10进制就是36和182,加起来226.

System.out.println((char)9398);//打印Ⓐ
3.BufferrReader

BufferrReader

他的readline挺好用的!直接读出数字转化成的字符串。

    public static void main(String[] args) throws IOException {
        FileReader a=new FileReader("F:\\filetest\\ufo.txt");
        BufferedReader b=new BufferedReader(a);
        System.out.println(b.readLine());//打印Ⓐ
         System.out.println(b.read());//打印9398
    }

直接把Ⓐ可以读取出来,而不是数字了。

4.BufferrWriter

BufferrWriter

没啥好说的。就是有缓存区。能减小硬盘读取次数,延长硬盘寿命。

5.InputStreamReader
5.InputStreamReader

就是可以把InputStream转为Reader

之前的9398 Ⓐ,通过FileInputStream再套上InputStreamReader,读出来不再是226了,就是正确的9398了

  public static void main(String[] args) throws IOException {
       InputStreamReader a=new InputStreamReader(new FileInputStream("F:\\filetest\\ufo.txt"));
        System.out.println( a.read());
    }
6.InputStreamWriter

InputStreamWriter

类似。

7.DataOutputStream DataInputStream
8.ByteArrayOutputStream ByteArrayInputStream

DataOutputStream DataInputStream

ByteArrayOutputStream ByteArrayInputStream

方便看,上面首字母缩写

image-20200930202720714

按照传入的顺序,先进先出。所以先writedouble,所以就要先readdouble。

9.PrintStream

PrintStream

这个只有输出,也就是output,没有input。

例如:

首先定义一个fileoutputstream,不能是filewriter哦!亲测!后面才知道,因为自己定义的是PrintStream,其实定义PrintWriter就可以传入filewriter了。

然后用PrintStream包裹起来。然后把这个PrintStream放到System.setOut中,就可以实现把print的内容写入到文件中了。


        PrintStream a = null;
        FileOutputStream b = new FileOutputStream("F:\\filetest\\ufo.txt");
        a = new PrintStream(b);
        System.setOut(a);
        System.out.println("123123123");
        a.close();

下面例子:读取控制台输入的内容,回车输入下一个,保存在指定文档中。


        String s = "";
        PrintWriter a = null;
        FileWriter b = new FileWriter("F:\\filetest\\ufo.txt");
        a = new PrintWriter(b);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        while ((s = br.readLine()) != null) {
            if (s.equalsIgnoreCase("exit")) {
                break;

            }
            a.println("-----------------");
            a.println(s);
            a.println(new Date());
        }
        a.flush();
        a.close();
10.ObjectOutputStream和ObjectInputStream
public static void main(String[] args) throws IOException, ClassNotFoundException {

        //把t通过ObjectOutputStream写入文件
        T t=new T();
        ObjectOutputStream a=null;
        FileOutputStream b = new FileOutputStream("F:\\filetest\\ufo.txt");
        a=new ObjectOutputStream(b);
        a.writeObject(t);

        //读取
        ObjectInputStream a1=null;
        FileInputStream b1 = new FileInputStream("F:\\filetest\\ufo.txt");
        a1=new ObjectInputStream(b1);
        T t1=(T)a1.readObject();
        System.out.println(t1.toString());

        a.flush();
        a.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值