IO框架笔记

流的分类

  • 输入流vs输出流。字节流vs字符流。节点流vs过滤流
  • 父类=超类=基类。子类=派生类

IO

流文件的读写

  • InputStream(File),OutputStream(File)
  • 读取fis.read
#从文件中读取
		FileInputStream fis = new FileInputStream("e:\\aaa.txt");

        byte[] buffer = new byte[1024];
        int count;
        while ((count=fis.read(buffer))!=-1){
            System.out.println(new String(buffer, 0, count));
        }
        
        fis.close();
  • 写入fos.write()
#直接进行文件的输出
public class Test{
    public static void main(String[] args) throws Exception{
        //append:true。接着上次的写
        FileOutputStream fos = new FileOutputStream("e:\\bbb.txt",true);
        fos.write("gj".getBytes());
        fos.write('a');
        fos.write('b');

        String s = "hello world";
        fos.write(s.getBytes());
        fos.close();
    }
}
===================================================
#fis → buffer → fos
public class Test{
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("e:\\a.png");
        FileOutputStream fos = new FileOutputStream("e:\\b.png");
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        fis.close();
        fos.close();
    }
}
  • BufferedInputStream(fis)
public class Test{
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("e:\\aaa.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
            System.out.println(new String(buffer, 0, len));
        }
        bis.close();//自动会关闭fis
    }
}
  • BufferedOutputStream(fos)
public class Test{
    public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("e:\\buffer.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        for (int i = 0; i < 10; i++) {
            bos.write("hello-world\r\n".getBytes());//写入8k缓冲区
            bos.flush();    //刷新到硬盘
        }
        bos.close();//还会调用flush()
    }
}

对象的读写

  • new ObjectOutputStream(fos)new ObjectOutputStream(fos)oos.writeObject(s1)
public class Test{
    //oos实现的序列化
    public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("e:\\student.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        //Student implements Serializable--
        Student s1 = new Student("zhangsan", 10);
        oos.writeObject(s1);

        oos.close();
    }
}
=============================================================
public class Student implements Serializable {	
	private static final long serialVersionUID = 100L;//也可以不设置,设置之后序列号必须一样
	//如果age被transient修饰,则序列化中无age的value
	//static也是不可被序列化的
}
  • ois.readObject()
public class Test{
    //反序列化
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("e:\\student.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student student = (Student) ois.readObject();//已经读完,不可再读
        System.out.println(student);
    }
}
  • oos.writeObject(students)ois.readObject()
#从(new Student键盘)内存 → oos → fos
public class Test2 {
    public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("e:\\student.bin");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        
        Student s1 = new Student("zhangsan", 10);
        Student s2 = new Student("lisi", 11);
        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        
        oos.writeObject(students);
        oos.close();
    }
}
==========================================================
#从(硬盘)fis → ois(arraylist)
public class Test{
    //反序列化
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("e:\\student.bin");
        ObjectInputStream ois = new ObjectInputStream(fis);
        ArrayList<Student> students = (ArrayList<Student>) ois.readObject();
        System.out.println(students);
    }
}

字符编码

  • iso-8859-1、utf-8、gb2312、gbk、big5

字符

public class Test{
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("e:\\hello.txt");
//        int data;
//        while ((data = fr.read())!=-1){  //读取一个字符,不是字节
//            System.out.println((char) data);
//        }
        int len;
        char[] buffer = new char[1024];     //按照字符来读,可中文/英文 
        while ((len=fr.read(buffer))!=-1){
            System.out.println(new String(buffer, 0, len));
        }
        fr.close();
    }
}
public class Test{
    //fr,fw不可以复制图片等二进制流文件。只可用于字符的
    //fis,fos可以复制任意文件(all在硬盘都是二进制),但是编码会有问题
    public static void main(String[] args) throws Exception{
        FileWriter fw = new FileWriter("e:\\write2.txt");
        FileReader fr = new FileReader("e:\\write.txt");
        int data = 0;
        while ((data=fr.read())!=-1){
            fw.write((char)data);
        }
        fr.close();
        fw.close();
    }
}
public class Test{
    //fr,fw不可以复制图片等二进制流文件。只可用于字符的
    //fis,fos可以复制任意文件(all在硬盘都是二进制),但是编码会有问题
    public static void main(String[] args) throws Exception{
        FileReader fr = new FileReader("e:\\write.txt");
        BufferedReader br = new BufferedReader(fr);
//        int len = 0;
//        char[] buffer = new char[1024];
//        while ((len=fr.read(buffer))!=-1){
//            System.out.println(new String(buffer, 0, len));
//        }
        String line = null;
        while ((line = br.readLine())!=null){   //空行不是null
            System.out.println(line);
        }
        br.close();
        fr.close();
    }
}
public class Test{
    public static void main(String[] args) throws Exception{
        FileWriter fw = new FileWriter("e:\\fb.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.newLine();   //windows \r\n。linux  \n
        for (int i = 0; i < 3; i++) {
            bw.write("你是谁\r\n");
        }
        bw.close();//关闭bw,自动关闭fw
    }
}
public class Test{
    public static void main(String[] args) throws Exception{
        PrintWriter pw = new PrintWriter("e:\\pw.txt");
        pw.println('a');
        pw.println(123);
        pw.println("\r\n"); //两个空行
        pw.println("xxx");
        pw.close();
    }
}

ctrl+p可以提供构造器

转换流

public class Test{
    public static void main(String[] args) throws Exception{
        FileInputStream fis = new FileInputStream("e:\\write3.txt");    //ansi编码=gbk 
        InputStreamReader isr = new InputStreamReader(fis,"gbk");
        int data;
        while ((data = isr.read())!=-1){
            System.out.print((char)data);
        }
    }
}
public class Test{
    public static void main(String[] args) throws Exception{
        FileOutputStream fos = new FileOutputStream("e:\\fos.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
        for (int i = 0; i < 3; i++) {
            osw.write("你是废物\r\n");
        }
        osw.close();
        fos.close();
    }
}
public class Test{
    public static void main(String[] args) throws Exception{    }
    public static void separator(){
        System.out.println(File.pathSeparator);
        System.out.println(File.separator);
    }
    public static void fileOpen() throws Exception{
        File file = new File("file.txt");//只是创建对象,硬盘并没有此文件

        if(!file.exists()){
            boolean newFile = file.createNewFile();//只可以创建一次
        }
        System.out.println(file.toString());
        System.out.println(file.getAbsolutePath());
        System.out.println(new Date(file.lastModified()));
        file.canWrite();
//        file.delete();
        file.deleteOnExit();    //推出jvm则删除
        Thread.sleep(1000); //1s后删除
    }
    public static void directoryOpen() throws Exception{
        File dir = new File("e:\\aaa\\bbb\\ccc");
        if(!dir.exists())   dir.mkdirs();
        System.out.println(dir.toString());
        dir.delete();   //删除最后面的文件夹,必须为空

        dir.deleteOnExit();
        Thread.sleep(1000);

        File dir2 =  new File("E:\\Picture\\wallpaper");
        String[] list = dir2.list();
        for (String s : list) {
            System.out.println(s);
        }
        File[] files = dir2.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".jpg");
            }
        });
        for (File file : files) {
            System.out.println(file.getName());
        }
    }
}
public class Test{
    public static void main(String[] args) throws Exception{    }
    public static void dir(File dir) throws Exception{
        File[] files = dir.listFiles();
        if(files!=null){
            for (File file : files) {
                if(file.isDirectory())  dir(file);
                System.out.println(file.getName());
            }
        }
    }
    public static void dirDelete(File dir) throws Exception{
        File[] files = dir.listFiles();
        if(files!=null){
            for (File file : files) {
                if(file.isDirectory())  dirDelete(file);
                file.delete();
            }
        }
        dir.delete();//再把自己删除
    }
}
public class Test{
    public static void main(String[] args) throws Exception{
        Properties properties = new Properties();
        properties.setProperty("name","zhangsna");
        properties.setProperty("age","20");
        System.out.println(properties);

        //properties.values();
        for (Object o : properties.keySet()) {
            System.out.println(properties.getProperty((String) o));
        }
        PrintWriter pw = new PrintWriter("e:\\print.txt");
        properties.list(pw);
        pw.close();

        FileOutputStream fos = new FileOutputStream("e:\\store.properties");
        properties.store(fos,"注释");//comment如果为汉字,会转码#\u6CE8\u91CA
        fos.close();

        Properties properties1 = new Properties();
        FileInputStream fis = new FileInputStream("e:\\store.properties");
        properties1.load(fis);
        System.out.println(properties1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值