【问题标题】:read files from folders and its subfolders and convert into one property file java从文件夹及其子文件夹中读取文件并转换为一个属性文件 java
【发布时间】:2018-02-14 07:03:39
【问题描述】:

我想从资源文件夹中读取文件夹和子文件夹,并将文件夹及其子文件夹中的所有 json 文件转换为一个属性文件。

资源文件夹将只包含 json 文件。

下面的代码是从资源文件夹中读取文件。 资源/渠道/短信

在频道内,我有不同的文件夹,如彩信、聊天等。

请帮助我从文件夹及其子文件夹中读取所有 json 文件并将它们转换为一个属性文件。

 public void loadFile() throws IOException {
    String folder = "/channel/sms";
    List<String> files = IOUtils.readLines(getClass().getClassLoader().getResourceAsStream(folder), Charsets.UTF_8);
    System.out.println(JSONUtil.toJSON(files));         
 }

谁能帮我将 JSON 文件转换为属性文件。 Json 文件名应该是keyvalue应该是它的文件值。

例子:

helloWorld.json

{
    "KEY1": {
        "KEY2": "Hello"
    },
    "KEY3":"World"
}

属性文件应该是:

helloWorld = { "KEY1": { "KEY2": "Hello"}, "KEY3":"World"}

提前致谢。

【问题讨论】:

    标签: java json java-8 properties-file


    【解决方案1】:

    你可以试试这段代码,我测试成功了

    这里是我们文件夹中的 json 文件 (D://test//)

    我们的一个属性文件路径(D:\properties\property.properties)

    package com.pms.lot;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    import org.apache.commons.io.FilenameUtils;
    
    /*sibin*/
     public class CustomTest {
    
    public static void main(String ard[]) {
    
        /* Function to get File Name */
        File folder = new File("D://test");
        File[] listOfFiles = folder.listFiles();
    
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                String fileName = listOfFiles[i].getName();
                String basename = FilenameUtils.getBaseName(fileName);
                try {
                    byte[] encoded = Files.readAllBytes(Paths.get("D://test//" + listOfFiles[i].getName()));
                    String str = new String(encoded, StandardCharsets.UTF_8);
    
                    System.out.println(basename + "=" + str);
    
                    // Insertion
    
                    /* create text file and insert text */
    
                    String fileNames = "D:\\properties\\property.properties";
    
                    BufferedWriter bw = null;
                    FileWriter fw = null;
    
                    try {
    
                        String content = basename + "=" + str;
    
                        fw = new FileWriter(fileNames,true);
                        bw = new BufferedWriter(fw);
                        bw.write(content);
    
                        System.out.println("Done");
    
                    } catch (IOException e) {
    
                        e.printStackTrace();
    
                    } finally {
    
                        try {
    
                            if (bw != null)
                                bw.close();
    
                            if (fw != null)
                                fw.close();
    
                        } catch (IOException ex) {
    
                            ex.printStackTrace();
    
                        }
    
                    }
    
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
            }
        }
        /**/
    
    }
    

    }

    【讨论】:

      【解决方案2】:

      我在这里使用 Java 8

      工作代码片段

      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Path;
      import java.nio.file.Paths;
      import java.nio.file.StandardOpenOption;
      import java.util.stream.Stream;
      
      public class ReadFiles {
      
          public static void main(String args[]) {
      
              try (Stream<Path> paths = Files.walk(Paths.get("parent"))) {
                  paths.filter(Files::isRegularFile).forEach(ReadFiles::convert);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      
          public static void convert(Path name) {
              try {
                  // using java 8
                  Path output = Paths.get("property.properties");
                  // read json file from all folder and subfolder and make a single
                  // line string
                  String content = new String(Files.readAllBytes(Paths.get(name.toUri()))).replaceAll("[\t\r\n]", "");
                  // append filename
                  String str = name.getFileName() + "= " + content + "\n";
                  // write into properties file
                  Files.write(output, str.getBytes(), StandardOpenOption.APPEND);
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      

      这是文件夹结构

      这是输出属性文件

      helloword.json= {"KEY1": {"KEY2": "Hello"},"KEY3": "World"} 
      hellowordparent.json= {"Details": {"Name": "Dhiraj"},"Surname": "Pandit"}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        相关资源
        最近更新 更多