java反射

java反射

  • 一、基本知识
  • 二、测试


一、基本知识

* Java中的文件 File 和 IO流
 *
 * File类:
 * 1. 创建:
 *    File file1 = new File("hello.txt");//相对于当前module,相对路径
 *    File file2 =  new File("D:\\hello.txt");绝对路径
 * 2.方法,所有需求就有相应的方法
 *
 * IO类
 *  一、流的分类:
 * 1.操作数据单位:字节流(InputStream,图片视频)、字符流(Reader,文本)
 * 2.数据的流向:输入流、输出流
 * 3.流的角色:节点流、处理流(处理流,就是“套接”在已有的流的基础上。)
 *
 * 二、流的体系结构
 * 抽象基类         节点流(或文件流)                               缓冲流(处理流的一种)
 * InputStream     FileInputStream   (read(byte[] buffer))        BufferedInputStream (read(byte[] buffer))
 * OutputStream    FileOutputStream  (write(byte[] buffer,0,len)  BufferedOutputStream (write(byte[] buffer,0,len) / flush()
 * Reader          FileReader (read(char[] cbuf))                 BufferedReader (read(char[] cbuf) / readLine())
 * Writer          FileWriter (write(char[] cbuf,0,len)           BufferedWriter (write(char[] cbuf,0,len) / flush()
 *

 *
 * 三、知识点
 *  1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
 *  2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
 *  3. 读入的文件一定要存在,否则就会报FileNotFoundException。
 *  4. 关闭外层处理流的同时,内层节点流也会自动的进行关闭。
 *
 * 四、 对象流 ObjectInputStream 和 ObjectOutputStream
 *  将对象用文件形式保存
 *  1. oos.writeObject(),写对象  , oos.readObject(),读出对象
 *  2. 类需要实现接口:Serializable,提供一个全局常量:serialVersionUID
 *  3. 必须保证其内部所有属性,基本数据类型可序列化,不能序列化statictransient修饰的成员变量
 *     public class Person implements Serializable{
     
 *          public static final long serialVersionUID = 475463534532L;
 *          。。。
 *     }
 *
 * 工具类:Paths Files Path
 *  Path可以看做是 java.io.File 类的升级版本。也可以表示文件或文件目录
 *  Path path = Paths.get("d:\\hello.txt");
 *
 *  JAVA中类的加载器:
 *  ClassLoader 代表类加载器,是 java 的核心组件,可以说所有的 class 文件都
 *  是由类加载器从外部读入系统,然后交由 jvm 进行后续的连接、初始化等操作
 *
 *  一共有三种加载器
 * 	1.bootstrap classloader :负责加载JAVA核心类( jre下 lib和class目录中的内容)
 * 	2.extension classloader :负责加载JAVA扩展类(jre下 lib/ext 目录中的内容)
 * 	3.system classloader :负责加载应用指定的类 (环境变量classpath中配置的内容)
 *

二、测试

//测试字符流
    public static void main(String[] args) {
     
        FileReader fr = null;
        FileWriter fw = null;
        try {
     
            File file = new File("D:\\MyCompute_Learning\\MyJava\\MyDataStructure\\src\\ReviewEveryday\\helloread.txt");
            File file2 = new File("D:\\MyCompute_Learning\\MyJava\\MyDataStructure\\src\\ReviewEveryday\\hellowrite.txt");
            fr = new FileReader(file);
            fw = new FileWriter(file2);

            int len ;
            char[] wchar = new char[5];
            //记录每次读入到cbuf数组中的字符的个数
            while((len = fr.read(wchar))!=-1){
     
                fw.write(wchar,0,len);
            }
            test();
        } catch (IOException e) {
     
            e.printStackTrace();
        } finally {
     
            if(fw!=null){
     
                try {
     
                    fw.close();
                } catch (IOException e) {
     
                    e.printStackTrace();
                }
            }

            if(fr!=null){
     
                try {
     
                    fr.close();
                } catch (IOException e) {
     
                    e.printStackTrace();
                }
            }
        }
    }

    //测试字节流
    public static void test(){
     
        BufferedInputStream bis  = null;
        BufferedOutputStream bos = null;
        try {
     
            File pic1 = new File("D:/MyCompute_Learning/MyJava/MyDataStructure/src/ReviewEveryday/pic1.jpg");
            File pic2 = new File("D:/MyCompute_Learning/MyJava/MyDataStructure/src/ReviewEveryday/copy.jpg");
            FileInputStream fis = new FileInputStream(pic1);
            FileOutputStream fos = new FileOutputStream(pic2);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            int len;
            byte[] buffer = new byte[1024];
            while((len = bis.read(buffer)) != -1){
     
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
     
            e.printStackTrace();
        } finally {
     
            try {
     
                if(bis != null){
     
                bis.close();
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }

            try {
     
                if(bos!= null){
     
                    bos.close();
                }
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(java后端面试准备)