Java新手学习 2021-2-20 记录每天学习内容(如有侵权请联系删除!!!)

2021-2-20

1.IO流简述和分类

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.字节流输出入门(字节流写数据)

在这里插入图片描述

package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/20/17:51
 */
public class FileOutPut {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流的对象---告诉虚拟机往哪个文件写数据
        //FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
        FileOutputStream fos=new FileOutputStream("D:\\test\\test.txt");
        //写入数据
        fos.write(97);
        //释放资源
        fos.close();
    }
}

在这里插入图片描述
在这里插入图片描述

package com.wc.fileout;


import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/20/17:51
 */
public class FileOutPut {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流的对象---告诉虚拟机往哪个文件写数据
        //FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
        FileOutputStream fos=new FileOutputStream("Stream\\a.txt");
        byte [] bys={
    
    97,98,99,100,101,102};
        //写入数据
        fos.write(bys,2,3);
        //fos.write(bys);
        //释放资源
        fos.close();
    }
}

3.字节流换行和追加写入

在这里插入图片描述

package com.wc.fileout;


import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/20/17:51
 */
public class FileOutPut {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字节输出流的对象---告诉虚拟机往哪个文件写数据
        //FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
        //append,续写开关,true打开,false关闭
        FileOutputStream fos=new FileOutputStream("Stream\\a.txt",true);

        //写入数据
        fos.write(97);
        //字符串转字节流.getBytes(),String的用法
        fos.write("\r\n".getBytes());
        fos.write(98);
        //字符串转字节流.getBytes(),String的用法
        fos.write("\r\n".getBytes());
        fos.write(99);
        //字符串转字节流.getBytes(),String的用法
        fos.write("\r\n".getBytes());

        //释放资源
        fos.close();
    }
}

4.字节流try catch捕获异常以及小结

在这里插入图片描述

package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/20/17:51
 */
public class FileOutPut1 {
    
    
    public static void main(String[] args) {
    
    
        //创建字节输出流的对象---告诉虚拟机往哪个文件写数据
        //FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
        //append,续写开关,true打开,false关闭
        FileOutputStream fos=null;
        try {
    
    
            fos=new FileOutputStream("Stream\\a.txt");
            fos.write(97);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //finally里面的代码一定会执行
            //加if判断是防止出现空指针异常
            if (fos!=null){
    
    
                try {
    
    
                    fos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

在这里插入图片描述

5.字节输入流(FileInputStream ,FileOutputStream 读取字节流多个字节)

在这里插入图片描述

package com.wc.fileout;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/22/11:45
 */
public class FileIntPut {
    
    
    public static void main(String[] args) throws IOException {
    
    

        FileInputStream fis=new FileInputStream("Stream\\a.txt");
        int read = fis.read();
        System.out.println(read);
        fis.close();
    }
}

在这里插入图片描述

6.字节流文件复制案例

在这里插入图片描述

package com.wc.fileout;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author wc
 * @Date: 2021/02/22/14:15
 */
public class FileCopy {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字节输入-----读
        FileInputStream fis=new FileInputStream("D:\\test\\test.txt");
        //创建字节输出-----写
        FileOutputStream fos=new FileOutputStream("Stream\\test.txt");
        //有多少字节就读多少字节通过while来实现
        int b;
        while((b=fis.read())!=-1){
    
    
            fos.write(b);
        }
        
        fis.close();
        fos.close();
    }
}

在这里插入图片描述
在这里插入图片描述

7.字节缓冲流加小结(BufferedInputStream,BufferedOutputStream)

在这里插入图片描述

扫描二维码关注公众号,回复: 12632286 查看本文章
package com.wc.fileout;

import java.io.*;

/**
 * @author wc
 * @Date: 2021/02/22/14:42
 */
public class FileBuffered {
    
    
    public static void main(String[] args) throws IOException {
    
    

        //创建一个字节缓冲输出流,(括号里放字节流)
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("Stream\\a.txt"));
        //创建一个字节缓冲输入流,(括号里放字节流)
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("Stream\\copy.txt"));

        byte [] bytes=new byte[1024];
        int length;
        while ((length=bis.read(bytes))!=-1){
    
    
            bos.write(bytes,0,length);
        }
        bis.close();
        bos.close();
    }
}

在这里插入图片描述

8.字符流(编码表简介)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

9.字符流编码和解码(getBytes)

在这里插入图片描述

package com.wc.charstream;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo {
    
    
    public static void main(String[] args) throws UnsupportedEncodingException {
    
    
        //使用平台默认字符集将String编码为一系列字节
        String s="我爱学java";
        byte[] bytes = s.getBytes();
        //System.out.println(bytes);//[B@1b6d3586  打印的为内存地址
        System.out.println(Arrays.toString(bytes));
        //使用指定字符集将String编码为一系列字节
        byte[] bytes1 = s.getBytes("UTF-8");
        byte[] bytes2 = s.getBytes("GBK");
        System.out.println(Arrays.toString(bytes1));
        System.out.println(Arrays.toString(bytes2));
        System.out.println("========================================");
        //使用平台默认字符集解码指定字节数组来构造新的string
        byte [] bytes3={
    
    -101,-120,-100,-11,-12,-14};
        byte [] bytes4={
    
    -26, -120, -111, -25, -120, -79, -27, -83, -90, 106, 97, 118, 97};
        String s1=new String(bytes4);
        //使用指定字符集解码指定字节数组来构造新的string
        String s2=new String(bytes3,"GBK");
        System.out.println(s1);
        System.out.println(s2);
    }
}

在这里插入图片描述

10.字符流写数据(write和flush和close)

在这里插入图片描述

package com.wc.charstream;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo1 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //创建字符输出流对象
        FileWriter fw = new FileWriter("Stream\\a.txt");
        //写一个字符
        fw.write(97);
        fw.write(98);
        fw.write(99);

        char [] chars={
    
    97,98,99,100};
        fw.write(chars);

        char [] chars1={
    
    97,98,99,100};
        fw.write(chars1,0,2);

        String s="我爱学java";
        fw.write(s);

        fw.close();
    }
}

在这里插入图片描述
在这里插入图片描述

11.字符流读取数据(filereader以及案例)

在这里插入图片描述
在这里插入图片描述

package com.wc.charstream;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo2 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.next();
        System.out.println("请输入密码:");
        String passWord = sc.next();

        FileWriter fw=new FileWriter("Stream\\a.txt");
        FileReader fr=new FileReader("Stream\\a.txt");
        fw.write(username);
        fw.write("\r\n");
        fw.write(passWord);
        fw.write("\r\n");
        fw.flush();
        fw.close();
    }
}

12.字符缓冲流(bufferedewriter,bufferedreader)

在这里插入图片描述
在这里插入图片描述

13.缓冲流特有方法(newline,readline)

在这里插入图片描述

package com.wc.charstream;

import java.io.*;
import java.util.Scanner;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo3 {
    
    
    public static void main(String[] args) throws IOException {
    
    

        BufferedWriter bfw = new BufferedWriter(new FileWriter("Stream\\a.txt"));
        bfw.write("橙子666");
        //换行newLine
        bfw.newLine();
        bfw.write("橙子66");
        bfw.newLine();
        bfw.write("橙子6");
        bfw.newLine();
        bfw.flush();
        bfw.close();
    }
}
package com.wc.charstream;

import java.io.*;
import java.util.Scanner;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo3 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        BufferedReader br = new BufferedReader(new FileReader("Stream\\a.txt"));
        String line;
        while ((line=br.readLine())!=null){
    
    
            System.out.println(line);
        }
        br.close();
    }
}

14.缓冲流案例

在这里插入图片描述

package com.wc.charstream;

import java.io.*;
import java.util.Arrays;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo4 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //先读取a.txt文件里面的数据,直接用缓冲流读取
        BufferedReader br = new BufferedReader(new FileReader("Stream\\a.txt"));
        //只有一行数据直接读取
        String line = br.readLine();
        System.out.println(line);
        br.close();

        //开始排序,排序要对int类型的数据进行操作,将数组转换成int类型
        //将字符按照空格进行切割
        String[] split = line.split(" ");
        int[] arr = new int[split.length];
        //遍历数组,进行转换
        for (int i = 0; i < arr.length; i++) {
    
    
            String smallStr = split[i];
            int number = Integer.parseInt(smallStr);
            arr[i] = number;
        }
        //排序
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        //把排序之后的写入
        BufferedWriter bw = new BufferedWriter(new FileWriter("Stream\\a.txt"));
        for (int i = 0; i < arr.length; i++) {
    
    
            bw.write(arr[i] + " ");
            bw.flush();
        }
        bw.close();
    }
}

15.转换流(指定编码读写InputStreamReader,OutputStreamWriter)

在这里插入图片描述

16.对象操作流(ObjectOutputStream,ObjectInputStream)

在这里插入图片描述
在这里插入图片描述
对象序列化,保证文件安全性

package com.wc.charstream;

import java.io.*;
import java.util.Arrays;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo5 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        Student student=new Student("橙子","123456");
        //采用对象操作流,增强代码安全性,output往外写
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Stream\\a.txt"));
        oos.writeObject(student);
        oos.close();
    }
}

在这里插入图片描述
对象操作反序列化
在这里插入图片描述

17.对象操作流程注意点

在这里插入图片描述

private static final long serialVersionUID=1L;

在这里插入图片描述

package com.wc.charstream;

import java.io.*;
import java.util.Arrays;

/**
 * @author wc
 * @Date: 2021/02/22/15:26
 */
public class CharStreamDemo5 {
    
    
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
        //method1();
        ObjectInputStream ois =new ObjectInputStream(new FileInputStream("Stream\\a.txt"));
        Student student=(Student) ois.readObject();
        System.out.println(student);
        ois.close();
    }

    private static void method1() throws IOException {
    
    
        Student student=new Student("橙子","123456");
        //采用对象操作流,增强代码安全性,output往外写
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Stream\\a.txt"));
        oos.writeObject(student);
        oos.close();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_49221590/article/details/113887950
今日推荐