Java Path File Name nio toClassName(String path)

Here you can find the source of toClassName(String path)

Description

to Class Name

License

Open Source License

Declaration

public static String toClassName(String path) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class Main {
    public static String toClassName(String path) {
        StringBuilder sb = new StringBuilder();
        sb.append(normalize(path).replace('-', '_').replace('.', '_').replace('/', '.'));
        return sb.toString();
    }//from w  w w  .ja v a2 s  . c om

    public static String normalize(String path) {
        if (path.contains(".")) {
            StringBuilder sb = new StringBuilder(path.length());
            for (String segment : path.split("/")) {
                if (segment.isEmpty() || segment.equals(".")) {
                    // skip
                } else if (segment.equals("..")) {
                    int sep = sb.lastIndexOf("/");
                    if (sep != -1) {
                        sb.setLength(sep);
                    } else {
                        return null;
                    }
                } else {
                    if (sb.length() > 0) {
                        sb.append("/");
                    }
                    sb.append(segment);
                }
            }
            path = sb.toString();
        }
        return path;
    }

    public static String normalize(String path, String subpath) {
        String result;
        if (path.isEmpty() || subpath.startsWith("/")) {
            result = subpath;
        } else if (path.endsWith("/")) {
            result = path + subpath;
        } else {
            result = path + "/" + subpath;
        }
        return normalize(result);
    }

    public static String toString(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder(1000);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
            int n;
            while ((n = reader.read()) != -1) {
                sb.append((char) n);
            }
        }
        return sb.toString();
    }
}

Related

  1. saveNamesToFile(Map namesMap, Path namesFile)
  2. saveToFile(String content, String directoryPath, String filename)
  3. shortenFileName(Path file, List dirs)
  4. storeFile(String fileName, InputStream inputStream, Path targetPath)
  5. subDirectoryNames(final Path dir)
  6. toResourcePath(String fileName)
  7. unzipFile(String fileName, String targetPath)
  8. uploadFileToServer(InputStream input, String filename, String usercode, String basePath)
  9. validateDockerfilePath(String name)