IO学习笔记

关于IO中的各种类库工具:
File:文件或者目录;
File.list();
File.listFile(); //遍历文件目录

File.canRead();
File.getPath();//这是File的属性。


byte:字节,1个字节
char:字符:2个字节

字节流:InputStream OutputStream
字符流:Reader Writer

newLine(); ,平台无关,文本换行,\r\n


字节流+转换表UTF-8,GBK

字节流+OutputStreamWriter(转换流) 写
字节流+InputStreamReader(转换流) 读


缓冲流:

四个缓冲流:
Buffered+InputStream
Buffered+OutputStream

特有方法newLine();readLine(); 不读出来换行符
Buffered+Reader
Buffered+Writer
使用方法:构造方法中用输入输出流作为参数。

硬盘:FIle
内存:byte char
键盘:system.in
网络:socket


Properties:和IO对象结合使用,实现数据的持久存储。

读入数据到集合,将集合中数据写到输出流中。
setProperty();
getProperty();

load();store();
spring.properties.配置文件通过IO流,结合properties 类,读入到集合中。



ObjectOutputStream.:写对象。序列化 静态成员没法序列化(transient不序列化)

ObjectInputStream:读对象,反序列化 - 和class文件对比。
读写的Object 必须实现接口,serializable.(标记性接口)
writeObject();
readObject();

需要做一个终身不变的序列号:
static final long = serialversionUID = “”;


打印流:

自动刷新:数据目的:流对象,调用方法:println,printf,format三个中的一个。.
PrintStream
PrintWriter
目的端:File类型,字符串文件名,接受字节输出流OutputStream,(Writer)
方法:print(100) 原样输出,写入。write(100)走编码表。


commons-io

package test;

import java.io.*;
import java.util.Properties;

/**
 * Created by Administrator on 2018/3/8.
 */
public class IOCopy {

    /**
     * 字节流读写
     * @throws IOException
     */
    public static  void copy1() throws IOException {
        InputStream inS = new FileInputStream("D:\\demo\\mvnw");
        OutputStream outS = new FileOutputStream("D:\\demo\\MVM");
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len=inS.read(bytes))!= -1){
            outS.write(bytes,0,len);
        }
        inS.close();
        outS.close();
    }

    /**
     * 字符流读写
     * @throws IOException
     */
    public static  void copy2() throws IOException {
        Reader fr = new FileReader("D:\\demo\\pom.xml");
        Writer wr = new FileWriter("D:\\demo\\pox.xml");
        char[] bytes = new char[1024];
        int len = 0;
        while((len=fr.read(bytes))!= -1){
            wr.write(bytes,0,len);
        }
        fr.close();
        wr.close();
    }

    /**
     * 系统缓存字符流读写
     * @throws IOException
     */
    public static  void copy3() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\demo\\pom.xml"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\demo\\poo1.xml"));
        String len = "";
        while((len = br.readLine())!= null){
            bw.write(len);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
    /**
     * IO 实现序列化
     * @throws IOException
     */
    public static  void writeObject() throws  IOException{
        ObjectOutputStream ooStream = new ObjectOutputStream(new FileOutputStream("D:\\demo\\object.txt"));
        Student st = new Student("liuyang",12,"379944104@qq.com");
        ooStream.writeObject(st);
    }
    /**
     * IO实现反序列化
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static  void readObject() throws IOException, ClassNotFoundException {
       ObjectInputStream ostream = new ObjectInputStream(new FileInputStream("D:\\demo\\object.txt"));
       Object stu = (Student)ostream.readObject();
        System.out.println(stu.toString());
    }

    /**
     * 转换流读写
     * @throws IOException
     */
    public static  void copy4() throws  IOException{
        InputStreamReader or = new InputStreamReader(new FileInputStream("D:\\demo\\aa.txt"),"UTF-8");
        OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream("D:\\demo\\bb.txt"),"GBK");
        char[] chars = new char[1024];
        int len = 0;
        while((len = or.read(chars)) != -1){
            os.write(chars,0,len);
        }
        or.close();
        os.close();
    }

    /**
     * Properties 与IO配合使用,比如读取配置文件.properties
     * @param args
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static  void writeProperties() throws  IOException{
        Properties properties = new Properties();
        properties.setProperty("name","张苏");
        properties.setProperty("age","13");
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\demo\\cc.properties"));
        properties.store(bw,"this is a properties");
        bw.close();
    }

    public static  void readProperties() throws  IOException{
        Properties properties = new Properties();
        BufferedReader br = new BufferedReader(new FileReader("D:\\demo\\cc.properties"));
        properties.load(br);
        br.close();
        System.out.println(properties.toString());
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
       /* copy1();*/
      /* copy2();*/
      /*copy3();*/
       /* writeObject();
        readObject();*/
       /* copy4();*/
       /* copy5();*/
        readProperties();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值