有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java首先计算文件的帐户,每分钟发送一个不同的文件

我有一个名为collect的文件夹,里面会有一些文件,比如selectData01。json,选择Data02。json,选择Data03。json等等

我必须首先计算文件的帐户,然后我会每分钟发送一个不同的文件

现在我想知道如何达到目的

public String getData() {

        String strLocation = new SendSituationData().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        log.info("strLocation = ");
//        String strParent = new File(strLocation).getParent() + "/collectData/conf.properties";
//        System.out.println("strParent = " + strParent);

        File fileConf = new File("collect/");
        System.out.println("fileConf = " + fileConf.getAbsolutePath());

        List<List<String>> listFiles = new ArrayList<>();

        //File root  = new File(DashBoardListener.class.getClassLoader().getResource("collectData/").getPath());
        //File root = new File("collectData/application.conf");



        File root  = new File(fileConf.getAbsolutePath());

        System.out.println("root.listFiles( ) = " + root.listFiles( ));
        Arrays
                .stream(Objects.requireNonNull(root.listFiles( )))
                .filter(file -> file.getName().endsWith("json"))
                .map(File::toPath)
                .forEach(path -> {
                    try {
                        //List<String> lines = Files.readAllLines(path);
                        //System.out.println("lines = " + lines);
                        List<String> lines = Files.readAllLines(path);
                        listFiles.add(lines);
                    } catch (IOException e) {
                        e.printStackTrace( );
                    }
                });

        String dataBody = listToString(listFiles.get(0));

        //log.info(dataBody);

        ResultMap result = buildRsult();
        //String jsonString = JSON.toJSONString(result);
}

public static String listToString(List<String> stringList){
        if (stringList == null) {
            return null;
        }

        StringBuilder result=new StringBuilder();
        boolean flag=false;
        for (String string : stringList) {
            if (flag) {
                result.append("");
            }else {
                flag=true;
            }
            result.append(string);
        }
        return result.toString();
    }

补充资料

我的朋友,也许我没有明确表达我的目的。如果我有三个文件,我会在0:00发送第一个文件,在0:01发送第二个文件,在0:03发送第三个文件,在0:04发送第一个文件,在0:05发送第二个文件,依此类推

如果我有五个文件,我会在0:00发送第一个文件,在0:01发送第二个文件,在0:03发送第三个文件,在0:04发送第四个文件,在0:05发送第五个文件,依此类推

我想知道如何实现这个功能

补充资料

我有一个struct项目,其中包含一个名为collect的文件夹。每个文件代表一个字符串

首先,我想计算collect文件夹中的文件数,然后每分钟发送一个文件

有什么建议吗


共 (1) 个答案

  1. # 1 楼答案

    我会将Apache camel与file2组件结合使用。 http://camel.apache.org/file2.html

    在运行任何测试之前,请阅读“noop”选项。 据我记忆所及,处理过的文件默认会被删除

    更新-添加了简单示例:

    1. 我建议从https://start.spring.io/开始

    2. 至少添加两个依赖项:Web和Camel(需要Spring Boot>;=1.4.0.RELEASE和<;2.0.0.M1)

    3. 创建新路线,可以从以下示例开始:

      @Component
      public class FileRouteBuilder extends RouteBuilder {
          public static final String DESTINATION = "file://out/";
          public static final String SOURCE = "file://in/?noop=true";
      
          @Override
          public void configure() throws Exception {
              from(SOURCE)
                      .process(exchange -> {
                          //your processing here
                      })
                      .log("File: ${file:name} has been sent to: " + DESTINATION)
                      .to(DESTINATION);
          }
      }
      
    4. 我的输出:

      • 2018-03-22 15:24:08.917文件:test1。txt已发送至:file://out/
      • 2018-03-22 15:24:08.931文件:test2。txt已发送至:file://out/
      • 2018-03-22 15:24:08.933文件:test3。txt已发送至:file://out/