IO流 序列化&反序列化 Properties 缓冲流 字节流 字符流

在这里插入图片描述

package com.llb.io;

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

/**
 * 字节流
 */
public class MyByteIO {
    public static void main(String[] args) throws Exception {
//        myFileOutPut();
        myFileInPut();
    }
    private static void myFileOutPut()throws Exception{
        //创建字节输出流对象
        //告诉JVM要往哪个文件中写数据了
        //如果文件不存在  会自动创建一个
        //如果文件存在   会把文件清空
        //第二个参数表示续写功能  true
        FileOutputStream fos =
                new FileOutputStream(
                        new File("E:\\itheima\\test\\aaa\\c.txt"),true);
        //写数据   传递的是一个整数的话  实际写到文件中的是整数在码表中对应的那个字符
        fos.write(97);
        //实现换行
        fos.write("\r\n".getBytes());
        String str = "云想衣裳花想容,\t春风拂槛露华浓。\n若非群玉山头见,\t会向瑶台月下逢。";
        fos.write(str.getBytes());

        /*byte[] bytes = {97,98,99};
        fos.write(bytes);*/
        /*byte[] bytes = {97,98,99,100,101,102,103};
        fos.write(bytes,3,4);*/
        //释放资源
        //告诉操作系统  此文件已经不在操作中了
        fos.close();
    }

    private static void myFileInPut()throws Exception{
        //获取字节输入流对象
            //如果文件不存在会报错
        FileInputStream fis =
                new FileInputStream(
                        new File("E:\\itheima\\test\\aaa\\c.txt"));
        //读数据  一次读取一个字节
            //如果想要看到字符数据,要强转成char类型
//        int read = fis.read();
//        System.out.println((char)read);
        int b ;
        while ((b = fis.read()) != -1){
            System.out.println((char)b);
        }

        fis.close();
    }
}

package com.llb.io;

import java.io.*;

public class MyBuffered {
    public static void main(String[] args) throws Exception {
        //在底层创建了一个默认长度为8192的字节数组
        BufferedInputStream bis =
                new BufferedInputStream(
                        new FileInputStream("E:\\itheima\\test\\aaa\\a.mp4"));
        BufferedOutputStream bos =
                new BufferedOutputStream(
                        new FileOutputStream("E:\\itheima\\test\\copy.mp4"));
//        int b ;
//        while ((b=bis.read())!=-1){
//            bos.write(b);
//        }
        int len ;
        byte[] bytes = new byte[1024];
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
    }
}

package com.llb.io;

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

/**
 * 文件拷贝
 */
public class FileCopy {
    public static void main(String[] args)throws Exception {
//        copy();
        copyBig();
    }
    private static void copy()throws Exception{
        FileInputStream fis =
                new FileInputStream(
                        new File("E:\\itheima\\test\\aaa\\a.mp4"));
        FileOutputStream fos =
                new FileOutputStream(
                        new File("E:\\itheima\\test\\a.mp4"));
        int b ;
        while ((b=fis.read())!=-1){
            fos.write(b);
        }
        fos.close();
        fis.close();
    }
    private static void copyBig() throws Exception {
        FileInputStream fis =
                new FileInputStream(
                        new File("E:\\itheima\\test\\aaa\\a.mp4"));
        FileOutputStream fos =
                new FileOutputStream(
                        new File("E:\\itheima\\test\\a.mp4"));
        byte[] bytes = new byte[1024];
        int count=0;
        int len;//本次读到的有效字节个数   读到了几个字节
        //把读取到的字节个数放到len里面
        //用 len 和 -1 比较,如果不等于-1表示读到了数据,如果等于-1表示读到了文件末尾
        while ((len = fis.read(bytes))!=-1){
            fos.write(bytes,0,len);
            count++;
        }
        System.out.println(count);
        fos.close();
        fis.close();
    }
}

package com.llb.io;

import java.io.*;

public class BufferedCopy {
    public static void main(String[] args) throws Exception {

        BufferedInputStream bis =
                new BufferedInputStream(
                        new FileInputStream("E:\\itheima\\test\\aaa\\a.mp4"));
        BufferedOutputStream bos =
                new BufferedOutputStream(
                        new FileOutputStream("E:\\itheima\\test\\copy.mp4"));

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

package com.llb.转换流;

import java.io.*;

/**
 *转换流
 */
public class Demo {
    public static void main(String[] args) throws Exception {

        OutputStreamWriter osw =
                new OutputStreamWriter(new FileOutputStream("Day12\\c.txt"),
                        "UTF-8");
        osw.write("悲莫悲兮生别离");

        osw.write("乐莫乐兮新相知");
        osw.close();
        InputStreamReader isr =
                new InputStreamReader(
                        new FileInputStream("Day12\\c.txt"),
                        "UTF-8");
        int len;
        char[] chars = new char[1024];
        while ((len=isr.read())!=-1){
            System.out.println(new String(chars,0,len));
        }
        isr.close();
    }
}

/**
* 序列化和反序列化
* 对象流
*/
package com.llb.对象操作流;

import java.io.*;
import java.util.ArrayList;

public class Exercise {
    public static void main(String[] args) throws Exception {
        Student student1 = new Student("刘备",18);
        Student student2 = new Student("关羽",19);
        Student student3 = new Student("张飞",17);
        ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream("Day12\\e.txt"));
//        oos.writeObject(student1);
//        oos.writeObject(student2);
//        oos.writeObject(student3);
        ArrayList<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        oos.writeObject(list);
        oos.close();
        ObjectInputStream ois =
                new ObjectInputStream(new FileInputStream("Day12\\e.txt"));
        ArrayList<Student> o = (ArrayList<Student>) ois.readObject();
        for (Student student : o) {
            System.out.println(student);
        }
        ois.close();
    }
}
class Student implements Serializable {
    private static final long serialVersionUID=1L;
    private String username;
    private Integer age;

    @Override
    public String toString() {
        return "Student{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

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

    public Student(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    public Student() {
    }
}

package com.llb.对象操作流;

import java.io.*;

/**
 * 把对象以字节的方式写到本地文件,直接打开文件,是读不懂的,需再次用对象操作流读到内从中
 *
 * Serializable
 *   标记行接口,里面没有任何的抽象方法
 *   只要有一个类实现了这个方法,就标志这个类可以被序列化
 *
 *   serialVersionUID   序列号
 *     如果我们没有定义  JVM会帮我们根据类里面的信息计算出一个序列号
 *     如果我们修改了类中的信息  JVM会再次计算出一个序列号
 *
 */
public class Demo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /**
         * 序列化
         */
//        User user = new User("tom","123");
//        ObjectOutputStream oos =
//                new ObjectOutputStream(new FileOutputStream("Day12\\d.txt"));
//        oos.writeObject(user);
//        oos.close();
        /**
         * 反序列化
         */
        ObjectInputStream ois =
                new ObjectInputStream(new FileInputStream("Day12\\d.txt"));
        User user = (User) ois.readObject();
        System.out.println(user);
        ois.close();
    }
}
class User implements Serializable {
    
    private String username;
    //瞬态关键字
    private transient String password;
	//自定义序列号
    private static final long serialVersionUID=1L;

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User() {
    }
}

package com.llb.字符流;

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


public class Exercise {
    public static void main(String[] args) throws IOException {
        write();
    }
    /**
     * 键盘录入用户名和密码保存到本地实现永久化存储
     */
    private static void write()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(new File("Day12\\a.txt"));
        //将用户名和密码写到文件中
        fw.write(username);
        fw.write("=");
        fw.write(password);
        fw.flush();
        fw.close();
    }
    private static void read() throws FileNotFoundException {
        FileReader fr = new FileReader(new File("Day12\\a.txt"));
        int len;

    }
}

package com.llb.字符流;

import javax.xml.transform.Source;
import java.io.*;
import java.util.Arrays;

public class BufferedExe {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
                new BufferedReader(new FileReader("Day12\\b.txt"));
        String s = br.readLine();
        String[] s1 = s.split(" ");
        //字符串数组变成int类型数组
        int[] arr = new int[s1.length];
        //遍历s1数组  进行类型转换
        for (int i = 0; i < s1.length; i++) {
            String s3 = s1[i];
            //类型转换
            int num = Integer.parseInt(s3);
            arr[i] = num;
        }
        System.out.println("排序前的数组 : "+Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("排序后的数组 : "+Arrays.toString(arr));

        //将排序后的数组存入到源文件中
        BufferedWriter bw =
                new BufferedWriter(new FileWriter("Day12\\b.txt"));
        for (int i : arr) {
            bw.write(i+" ");
            bw.flush();
        }
        bw.close();
        br.close();
    }
}

package com.llb.字符流;

import java.io.*;

public class BufferedDemo {
    public static void main(String[] args) throws Exception {
//        reader();
//        writer();
        /**
         * 字符缓冲流特有功能
         * newLine()  回车换行
         *
         * redaLine()  读一行
         */
        /*BufferedWriter bw =
                new BufferedWriter(new FileWriter("Day12\\a.txt"));
        bw.write("云想衣裳花想容");
        bw.newLine();
        bw.write("春风拂槛露华浓");
        bw.newLine();
        bw.flush();
        bw.close();*/

        BufferedReader br =
                new BufferedReader(new FileReader("Day12\\a.txt"));
        //读一行   如果读不到数据会返回null值
        String line;
        //一直读,读到换行为止,但是不会读到回车换行符
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
//        String s1 = br.readLine();
//        String s2 = br.readLine();
//        System.out.println(s1);
//        System.out.println(s2);
        br.close();


    }
    private static void reader() throws Exception {
        //字符缓冲输入流
        BufferedReader br =
                new BufferedReader(new FileReader("Day12\\a.txt"));
        //读取数据
        char[] chars = new char[1024];
        int len;
        while ((len=br.read(chars))!=-1){
            System.out.println(new String(chars,0,len));
        }
        br.close();
    }
    private static void writer() throws Exception{
        BufferedWriter bw =
                new BufferedWriter(new FileWriter("Day12\\b.txt"));
        bw.write(97);
        char[] chars = {98,99,100,101,102,103};
        bw.write(chars);
        bw.write(chars,0,chars.length);
        bw.close();
    }
}

在这里插入图片描述

package com.llb.properties;

import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * Properties
 *   Map集合的一个类
 */
public class Demo {
    public static void main(String[] args) {
        Properties prop = new Properties();
        //增
        prop.put("1003","hinata3");
        prop.put("1001","hinata1");
        prop.put("1002","hinata2");
        prop.setProperty("中国","北京");
        System.out.println(prop);
        //删
        prop.remove("1001");
        //改
        prop.put("1002","naruto");
        System.out.println(prop);
        //获取所有键的集合
        Set<String> set = prop.stringPropertyNames();
        for (String s : set) {
            System.out.print("key's set "+s+"\t");
        }
        System.out.println();
        //通过entrySet()方法获取全部key value对象
        Set<Map.Entry<Object, Object>> entries = prop.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            Object key = entry.getKey();//获取key
            Object value = entry.getValue();//获取value
            System.out.print(key);
            System.out.println(value);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值