Java Path File Name nio renameLogFile(Path original)

Here you can find the source of renameLogFile(Path original)

Description

This method renames the file denoted by the file parameter by appending the current date to it and a counter if there is already a file with that name.

License

Open Source License

Parameter

Parameter Description
original the original file

Exception

Parameter Description
IOException if the log file could not be renamed

Declaration

public static void renameLogFile(Path original) throws IOException 

Method Source Code


//package com.java2s;
/*//from   ww w .j  ava 2 s.  co m
 *     BasePlugin
 *   Copyright (C) 2014 Viciouss <http://www.doncarnage.de>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 3 of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *   USA
 */

import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static final String FILE_NAME_FORMAT = "%s%s.%d.%s";

    /**
     * This method renames the file denoted by the file parameter by appending the current date to it and a counter if there is already a file with that name.
     * This is recursive, so it will look for the first free file name and rename it, e.g. current.log will get transformed into current20140101.1.log or
     * counting further up the number before the file ending until there is a free file name like current20140101.6.log.
     *
     * @param original the original file
     * @throws IOException if the log file could not be renamed
     */
    public static void renameLogFile(Path original) throws IOException {

        if (!Files.exists(original)) {
            return;
        }

        String fullFileName = original.getFileName().toString();
        int lastDot = fullFileName.lastIndexOf(".");
        String nameBeforeDot = fullFileName.substring(0, lastDot);
        String nameAfterDot = fullFileName.substring(lastDot + 1, fullFileName.length());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date());

        Path parent = original.getParent();
        int i = 1;
        Path renamed = getPathByFormat(nameBeforeDot, nameAfterDot, parent, i, date);
        while (Files.exists(renamed)) {
            i++;
            renamed = getPathByFormat(nameBeforeDot, nameAfterDot, parent, i, date);
        }

        Files.move(original, renamed);

    }

    private static Path getPathByFormat(String nameBeforeDot, String nameAfterDot, Path parent, int i,
            String date) {
        Path renamed;
        String fileName = String.format(FILE_NAME_FORMAT, nameBeforeDot, date, i, nameAfterDot);
        if (parent == null) {
            renamed = Paths.get(fileName);
        } else {
            renamed = parent.resolve(fileName);
        }
        return renamed;
    }
}

Related

  1. pathToPackageName(Path path)
  2. readFileFromPath(String filename)
  3. relativizePath(Path relativeTo, String filename)
  4. rename(Path origin, Path destination)
  5. renameFile(Path filePath, String postfix)
  6. saveFileFromByteArray(String filename, String pathDirectory, byte[] data)
  7. saveNamesToFile(Map namesMap, Path namesFile)
  8. saveToFile(String content, String directoryPath, String filename)
  9. shortenFileName(Path file, List dirs)