zoukankan      html  css  js  c++  java
  • 反射 与文件读写 泛型参数

    最近公司项目需要根据bean生成对应的yaml配置文件,需要用到反射和文件操作,API总结如下:

    文件读写:

            File file = new File(path);
            file.isDirectory();
            File[] files = file.listFiles();
    
                    String name = file1.getAbsolutePath();
                    name = file1.getName();
    
    
            File file = new File(path + c.getSimpleName() + ".yaml");
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
            FileWriter fileWriter = new FileWriter(file);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    
            bufferedWriter.write("definitions:");
            bufferedWriter.newLine();
    
            bufferedWriter.flush();
            bufferedWriter.close();

    反射:

    Class c =  Class.forName("model." + name);
    Field[] fields = c.getDeclaredFields();
    Class c = field.getType();
    field.getType().getSimpleName()
    //泛型参数
            Type fieldType = field.getGenericType();
            //获取对应的泛型类
            Type type = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
            String name = type.getTypeName();    
  • 相关阅读:
    1001.A+B for Matrices
    1016.火星A+B
    1468.Sharing
    1464.Hello World For U
    约瑟夫问题pascal程序
    约数研究pascal程序
    迷宫pascal程序
    魔法照片pascal程序
    均分纸牌pascal程序
    多项式输出pascal程序
  • 原文地址:https://www.cnblogs.com/heapStark/p/8253431.html
Copyright © 2011-2022 走看看