0

I want to save multiple txt files in one txt file and delete the previous ones. What method can I use? Code I wrote myself:

public static void mergeFile() throws IOException {
        long unixTime = System.currentTimeMillis() / 1000L;
        final String sysName = String.valueOf(unixTime);
        File directory = new File("src/");
        FileWriter myWriter = new FileWriter("src/"+sysName+".txt",true);

        File[] files = directory.listFiles();
        for(File file : files) {
            FileInputStream fis = new FileInputStream(file);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            String aLine;
            String fileName = file.toString();
            int split = fileName.indexOf(".");
            long fName = Long.parseLong(fileName.substring(5, split)) ;
            if (fName < unixTime) {
                while ((aLine = in.readLine()) != null) {
                    myWriter.write(aLine);
                    myWriter.write("\n");
                }
                in.close();
                file.delete();
            }
        }
    }
Lashgari
  • 41
  • 11
  • What is the problem with your current code? Does it compile? – Amongalen Dec 14 '20 at 12:11
  • it's work but I want after merge. delete any file and it cant work current. – Lashgari Dec 14 '20 at 12:15
  • Is there an exception? If yes, show us the whole stack trace. What ist the result of `file.delete()`? According to the [File.delete() documentation](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#delete--) there is a boolean return value indicating whether the deletion was successfull or not. – vanje Dec 14 '20 at 12:23
  • This is an operational task. I would use shell command(s), which are the appropriate choice. Is you absolutely must do it from java, call a shell command from java. – Bohemian Dec 15 '20 at 07:16
  • 1
    Use NIO, as NIO will tell you why deletion failed, instead of just returning a `boolean`. Consider the code of [this answer](https://stackoverflow.com/a/25548570/2711488). I suppose, you could even pass [`DELETE_ON_CLOSE`](https://docs.oracle.com/javase/8/docs/api/java/nio/file/StandardOpenOption.html#DELETE_ON_CLOSE) to `FileChannel.open(inFile, READ)` to delete the input files right after reading, but I’d prefer to copy all files first and only delete them after the entire operation succeeded. – Holger Dec 15 '20 at 17:27

1 Answers1

0

I rewrote the above code as follows and it worked properly.

public static void mergeFile() throws IOException {
   long unixTime = System.currentTimeMillis() / 1000L;
   final String sysName = String.valueOf(unixTime);
   File directory = new File("src/raw/");
   File[] files = directory.listFiles();
   FileWriter myWriter = new FileWriter("src/ok/"+sysName+".txt");
   for(File file : files) {
      String aLine;
      String fileName = file.toString();
      int split  = fileName.indexOf(".");
      long fName = Long.parseLong(fileName.substring(15, split)) ;
      if (fName <= unixTime) {
         FileInputStream fis = new FileInputStream(file);
         BufferedReader in = new BufferedReader(new InputStreamReader(fis));
         while ((aLine = in.readLine()) != null) {
              myWriter.write(aLine);
              myWriter.write("\n");
            }
            in.close();
         }
         Files.move(Paths.get("src/raw/"+fName+".txt"),
                    Paths.get("src/old/"+fName+".txt"));
      }
      myWriter.close();
      Query.insertSyslog("src/ok/"+sysName+".txt");
}

Lashgari
  • 41
  • 11