Here you can find the source of filePathToPackagePathOrNull(String path)
public static String filePathToPackagePathOrNull(String path)
//package com.java2s; //License from project: Apache License public class Main { /**// w w w . j a va2 s . c o m * Return the package name part of the given file path or null if no package name could be determined */ public static String filePathToPackagePathOrNull(String path) { path = toForwardSlashes(path); if (path.charAt(0) == '/') { //e.g /Foo/Bar - > Foo/Bar, Foo/Bar --> Foo/Bar path = path.substring(1); } int last = path.lastIndexOf('/'); if (last == -1) { //no package part return null; } String pkg = path.substring(0, last); //e.g Foo/Bar - > Foo pkg = pkg.replace('/', '.'); if (pkg.length() == 0) { return null; } return pkg; } public static String toForwardSlashes(String path) { if (path == null) { return null; } return path.replace('\\', '/'); } }