A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 1049355629 初级黑马   /  2019-8-1 15:58  /  890 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

经历了几天的io学习 我对io输入输出有了新的认识
1. File类

1.1 File类的概述

- File类是对抽象路径名的表示 ,所表示的文件/目录不一定存在
- 创建方法
  - File(String pathname)
  - File(String parent,String child)
  - File(File parent,String child)

1.2 创建文件或目录

- File类的创建功能
  - 创建文件:boolean createNewFile(); 用来创建一个文件,如果没有则创建,如果有则不创建
  - 创建目录:
    - mkdir();
    - mkdirs();
- 判断功能和获取功能
  - boolean exists()
  - boolean isDirectory()
  - boolean isFile()
  - String getAbsolutePath()
  - String getPath()
  - String[] list();   
  - File[] listFiles();
- 删除方法
  - boolean delete()
      public class Test{
      
              public static void main(String args[]){
                      File file = new File("d:/");
                      String[] str = file.list();
                      for(String s:str){
                              if(s.endWiths(".java")){
                                      System.out.println(s)
                              }
                             
                      }
              }
      }
  1.3流的分类
  - 按处理的类型划分
    - 字节流
    - 字符流
  - 按流向划分
    - 输入流
      - 字节输入流
      - 字符输入流
    - 输出流
      - 字节输出流
      - 字符输出流
  - final和finally
    - final 可以修饰类、变量、方法
      - final修饰类:不可被继承
      - final修饰变量:表示常量,不可被修改
      - final修饰方法:不可被重写
    - finally 在异常处理时使用,代码一定会被执行,通常用来释放资源
  1.4 需求
  - java copy hello.txt helloworld.txt



- 按流向分
  - 输入流
    - 字节输入流 InputStream   FileInputStream
      - 读数据方法:read()/read(byte[])/read(byte[],0,length)
      - 缓冲字节输入流 BufferedInputStream
    - 字符输入流 InputStreamReader(InputStream is)     Reader   FileReader
      - 缓冲字符输入流BufferedReader
        - String readLine()
  - 输出流
    - 字节输出流OutputStream FileOutputStream
      - 写数据 write(int b)/write(byte[] b)/ write(byte[] b, int offset, int length)
      - 缓冲字节输出流 BufferedOutputStream
    - 字符输出流 OutputStreamWriter(OutputStream os)  Writer FileWriter
      - 缓冲字符输出流 BufferedWriter
        - newLine()
- 按处理单位分
  - 字节流
  - 字符流
  -
- 集合回顾
  - Collection
    - List 有序可重复
      - ArrayList
      - LinkedList
    - Set 无序不重复
      - HashSet
      - TreeSet
    - Map
      - Hashmap
      - TreeMap
  File file = new File(pathname);
  File file = new File(parent,child);
  File file = new File(File parent, String child);

  boolean file.createNewFile();

  mkdir();

  mkdirs();

  file.delete();

  String[] files =  file.list();

  File[] files = file.listFiles();

  

  file.isDirectory();

  file.isFile();

  file.exists();

  

  

  InputStream

          FileInputStream

      BufferedInputStream

             read()

                read(byte[] b)

            read(byte[] b,0,length)

  OutputStream

       FileOutputStream

       BufferedOutputStream

              write()

              write(byte[] b)

              write(byte[] b,,0,length)

  

  字符集

          ASCII

            ASCII

      GBXXX

                GB2312

            GBK

            GB18030

      Unicode

             utf-8

             utf-16

             utf-32

  字符串编码解码

  String str = "hello";

  str.getBytes();//使用平台默认字符集进行编码

  str.getBytes(String charsetName);//使用指定的字符集进行编码

  

  new String(byte[] b);

  new String(byte[] b ,String charsetName)

  

  InputStreamReader(new FileInputStream(""))

  InputStreamReader(new FileInputStream(""), String charsetName)  //转换流

  

  OutputStreamWriter(new FileOutputStream(""))

  OutputStreamWriter(new FileOutputStream(""),String charsetName)


复制文件的异常处理

- throws
- try{} ....catch(){}....finally{}
- try{
  //放可能出现异常的代码
- }catch(Exception e){
  //异常的处理
- }finally{
  //释放资源
- }

jdk7

- try(//放可能出现异常的代码){
  //业务代码
- }catch(Exception e){
  //异常的处理
- }

jdk9

- //放可能出现异常的代码
- try(br;bw){
  //业务代码
- }catch(Exception e){
  //异常的处理
- }
  标准输入输出流的本质
  - System.out   PrintStream
  - System.in     InputStream
  字节、字符打印流
  - PrintStream
  - PrintWriter
  对象序列化
  - 对象如何实现序列化:implements Serializable
  - 实现序列化的对象才能够写到文件
    - ObjectInputStream    反序列化
    - ObjectOutputStream  序列化
  - 序列版本号:
  - 如果某些属性不想序列化:transient
  Properties和IO结合
  - A=B   split("=")[0]
  - <K,V>  - > K=V

username=zhangsan

password=123   ->ab2342b124j1l2j


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马