ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何按顺序连接顺序文件?

2019-08-25 12:03:53  阅读:156  来源: 互联网

标签:java concatenation file-io


我有一个目录,其中包含按顺序编号的日志文件和一些用于分析的Excel电子表格.日志文件始终从零开始按顺序编号,但它们的数量可以变化.我试图将日志文件按照它们创建的顺序连接到一个文本文件中,该文件将是所有日志文件的串联.

例如,使用日志文件foo0.log,foo1.log,foo2.log将通过在foo0之后附加foo1和foo1之后的foo2输出到concatenatedfoo.log.

我需要使用* .log的扩展名计算给定目录中的所有文件,使用count来驱动for循环,该for循环也生成用于连接的文件名.我很难找到一种方法来使用过滤器对文件进行计数……文件操作中的Java Turtorials似乎都不符合这种情况,但我确定我错过了一些东西.这种方法有意义吗?或者有更简单的方法吗?

int numDocs = [number of *.log docs in directory];
 //
for (int i = 0; i <= numberOfFiles; i++) {
     fileNumber = Integer.toString(i);
     try
     {
          FileInputStream inputStream = new FileInputStream("\\\\Path\\to\\file\\foo" + fileNumber + ".log");
          BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
          try
          {
               BufferedWriter metadataOutputData = new BufferedWriter(new FileWriter("\\\\Path\\to\\file\\fooconcat.log").append());
               metadataOutputData.close();
          }
           //
          catch (IOException e)  // catch IO exception writing final output
          {
               System.err.println("Exception: ");
               System.out.println("Exception: "+ e.getMessage().getClass().getName());
               e.printStackTrace();
          }
     catch (Exception e)    // catch IO exception reading input file
     {
          System.err.println("Exception: ");
          System.out.println("Exception: "+ e.getMessage().getClass().getName());
          e.printStackTrace();
     }
}

解决方法:

怎么样

public static void main(String[] args){

    final int BUFFERSIZE = 1024 << 8;
    File baseDir = new File("C:\\path\\logs\\");

    // Get the simple names of the files ("foo.log" not "/path/logs/foo.log")
    String[] fileNames = baseDir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".log");
        }
    });

    // Sort the names
    Arrays.sort(fileNames);

    // Create the output file
    File output = new File(baseDir.getAbsolutePath() + File.separatorChar + "MERGED.log");
    try{
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(output), BUFFERSIZE);
        byte[] bytes = new byte[BUFFERSIZE];
        int bytesRead;
        final byte[] newLine = "\n".getBytes(); // use to separate contents

        for(String s : fileNames){
            // get the full path to read from
            String fullName = baseDir.getAbsolutePath() + File.separatorChar + s;
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(fullName),BUFFERSIZE);
            while((bytesRead = in.read(bytes,0,bytes.length)) != -1){
                out.write(bytes, 0, bytesRead);
            }
            // close input file and ignore any issue with closing it
            try{in.close();}catch(IOException e){}
            out.write(newLine); // seperation
        }

        out.close();
    }catch(Exception e){
        throw new RuntimeException(e);
    }
}

此代码假定“顺序命名”将为零填充,以便它们将以lexigraphically(?? sp)正确排序.即文件将是

> 0001.log(或blah0001.log,或0001blah.log等)
> 0002.log
> ….
> 0010.log

并不是

> 1.log
> 2.log
> ……
> 10.log

后一种模式将无法使用我给出的代码正确排序.

标签:java,concatenation,file-io
来源: https://codeday.me/bug/20190825/1718722.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有