Java Path Exist nio getNonExisting(Path path)

Here you can find the source of getNonExisting(Path path)

Description

get Non Existing

License

Open Source License

Declaration

private static Path getNonExisting(Path path) 

Method Source Code

//package com.java2s;
/*-//  www .j  a  va  2 s . c o m
 * ============================LICENSE_START============================
 * data.messie (core)
 * =====================================================================
 * Copyright (C) 2013 - 2017 Dr. Raphael Romeikat
 * =====================================================================
 * This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
    
You should have received a copy of the GNU General Public
License along with this program.  If not, see
<http://www.gnu.org/licenses/gpl-3.0.html>.
 * =============================LICENSE_END=============================
 */

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

public class Main {
    private static Path getNonExisting(Path path) {
        // Return path, if it does not exist
        if (!Files.exists(path)) {
            return path;
        }
        // Determine name and extension
        final String name = path.getFileName().toString();
        final int fileTypeIndex = name.lastIndexOf(".");
        String nameWithoutExtension;
        String extension;
        if (fileTypeIndex == -1) {
            nameWithoutExtension = name;
            extension = "";
        } else {
            nameWithoutExtension = name.substring(0, fileTypeIndex);
            extension = name.substring(fileTypeIndex);
        }
        // Determine number
        final int underscoreIndex = name.lastIndexOf("_");
        String nameWithoutExtensionWithoutNumber;
        Integer nextNumber = 1;
        if (underscoreIndex == -1) {
            nameWithoutExtensionWithoutNumber = nameWithoutExtension;
        } else {
            nameWithoutExtensionWithoutNumber = nameWithoutExtension.substring(0, underscoreIndex);
            final String numberString = name.substring(underscoreIndex + 1);
            try {
                nextNumber = Integer.parseInt(numberString) + 1;
            } catch (final NumberFormatException e) {
            }
        }
        // Determine next candidate
        String nextName;
        while (true) {
            nextName = nameWithoutExtensionWithoutNumber + "_" + nextNumber++ + extension;
            path = Paths.get(path.getParent().toString(), nextName);
            if (!Files.exists(path)) {
                return path;
            }
        }
    }
}

Related

  1. exists(Path value)
  2. exists(Path... files)
  3. exists(String path)
  4. existsAndIsDirectory(Path path)
  5. fileExists(Path path)
  6. isExists(final Path path)
  7. isFileExists(Path file)
  8. lenientExists(Path file)
  9. makeTempFilePath(boolean exists)