Store the information in txt in the object (two)

Store the information in txt into the object

 //获得文件路径,获得数据对象,将对象放在list集合中
    public List insertTxts(String path) throws Exception{
    
    
        //调用该类中的insertTxt方法获取所有txt文件的路径
        List listPath = insertTxt(path);
        //创建一个list1用于存放所有从txt文件中提取出来的对象
        List list1 = new ArrayList();
        /*listPath中每个路径就是一个txt文件,所以循环listPath集合
        * 并将每一个txt文件封装成一个对象,然后将对象添加到list1集合中*/
        for (int j = 0;j<listPath.size();j++){
    
    
            //获取filepath的第j个值
            String filepath = (String) listPath.get(j);
            //将第j个路径的txt文件进行转码(本人操作时,不转码不行,各位在使用时请根据实际情况考虑是否要转码)
            convert(filepath, "GBK", "UTF-8");
            //通过特定的规则处理txt获取对象
            ImportTxtEntity importTxtEntity = importTxt(filepath);
            //将获取的对象放入list1中
            list1.add(importTxtEntity);
        }
        //返回list1对象集合
        return list1;
    }
//按照固定格式将txt中的数据存储到对象
    public ImportTxtEntity  importTxt(String filePath) throws Exception {
    
    
        //创建一个importTxtEntity对象,用来存储txt文件中的信息
        ImportTxtEntity importTxtEntity = new ImportTxtEntity();
        //创建各种流来读取路径的txt文件的内容
        FileInputStream fileInputStream = new FileInputStream(filePath);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, "utf-8"));
        //用来收集txt每一行的内容,list一个元素就是一行(索引从0开始)
        List list = new ArrayList();
        String line = "";
        //将txt中的一行赋值给line,然后判断line是否为空,如果不为空,就将该line复制给list集合
        while ((line = bufferedReader.readLine()) != null) {
    
    
            if (!line.equals("")) {
    
    
                list.add(line);
            }
        }
        //获取list集合中的第一个作为名字(就是txt的第一行)
        String name = (String) list.get(0);
        importTxtEntity.setLineName(name);
        //将list集合中的数据按照一定规则进行处理,并赋值给importTxtEntity的各个属性
        for (int i = 0; i < list.size(); i++) {
    
    
            importTxtEntity = txtRule(i,importTxtEntity,list);
        }
        //返回importTxtEntity对象
        return importTxtEntity;
    }
 //处理txt规则,并保存进importTxtEntity对象中
    public ImportTxtEntity txtRule(int i,ImportTxtEntity importTxtEntity,List list){
    
    
        String result = (String) list.get(i);
        /*判断该行中是否存在:(冒号),如果存在:(冒号),则对冒号进行分割,获得一个数组
        * 示例"你的名字:如此优秀"则会被分割为a[]={你的名字,如此优秀}
        * 如果是多个冒号"你的名字:如此优秀:帅比"则a[]={你的名字,如此优秀,帅比}*/
        //判断是否存在冒号
        if (result.contains(":")) {
    
    
            //对其进行冒号分割
            String a[] = result.split(":");
            if (a[0].equals("你的名字")) {
    
    
                importTxtEntity.setName(a[1]);
            } else if (a[0].equals("你的年龄")) {
    
    
                importTxtEntity.setAge(a[1]);
            }
          }
        //将设置好值的对象返回
        return importTxtEntity;
    }
/*以下几个方法都是转换编码的方法,直接复制粘贴使用*/
    public static void convert(String fileName, String fromCharsetName, String toCharsetName) throws Exception {
    
    
        convert(new File(fileName), fromCharsetName, toCharsetName, null);
    }
    public static void convert(File file, String fromCharsetName, String toCharsetName) throws Exception {
    
    
        convert(file, fromCharsetName, toCharsetName, null);
    }
    public static void convert(String fileName, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
    
    
        convert(new File(fileName), fromCharsetName, toCharsetName, filter);
    }
    public static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter) throws Exception {
    
    
        if (file.isDirectory()) {
    
    
            File[] fileList = null;
            if (filter == null) {
    
    
                fileList = file.listFiles();
            } else {
    
    
                fileList = file.listFiles(filter);
            }
            for (File f : fileList) {
    
    
                convert(f, fromCharsetName, toCharsetName, filter);
            }
        } else {
    
    
            if (filter == null || filter.accept(file.getParentFile(), file.getName())) {
    
    
                String fileContent = getFileContentFromCharset(file, fromCharsetName);
                saveFile2Charset(file, toCharsetName, fileContent);
            }
        }
    }
    public static String getFileContentFromCharset(File file, String fromCharsetName) throws Exception {
    
    
        if (!Charset.isSupported(fromCharsetName)) {
    
    
            throw new UnsupportedCharsetException(fromCharsetName);
        }
        InputStream inputStream = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(inputStream, fromCharsetName);
        char[] chs = new char[(int) file.length()];
        reader.read(chs);
        String str = new String(chs).trim();
        reader.close();
        return str;
    }
    public static void saveFile2Charset(File file, String toCharsetName, String content) throws Exception {
    
    
        if (!Charset.isSupported(toCharsetName)) {
    
    
            throw new UnsupportedCharsetException(toCharsetName);
        }
        OutputStream outputStream = new FileOutputStream(file);
        OutputStreamWriter outWrite = new OutputStreamWriter(outputStream, toCharsetName);
        outWrite.write(content);
        outWrite.close();
    }

Guess you like

Origin blog.csdn.net/MYNAMEL/article/details/113590686