public class Separator {
//取最后一个分隔符
public static int getPathLastIndex(String fileName)
{
int point = fileName.lastIndexOf('/');
if (point == -1) {
point = fileName.lastIndexOf('\\');
}
return point;
}
//取第一个分隔符
public static int getPathFirstIndex(String fileName)
{
int point = fileName.indexOf('/');
if (point == -1) {
point = fileName.indexOf('\\');
}
return point;
} public static void main(String[] args) {
File file = new File("D:\\temp\\SendTest1.0\\");
}}
说简单点就是,给一段路径,如何取得倒数第二个分隔符和最后一个分隔符之间的东西。
路径:D:\\temp\\SendTest1.0\\hello.java
我想从这个路径中得到SendTest1.0。
//取最后一个分隔符
public static int getPathLastIndex(String fileName)
{
int point = fileName.lastIndexOf('/');
if (point == -1) {
point = fileName.lastIndexOf('\\');
}
return point;
}
//取第一个分隔符
public static int getPathFirstIndex(String fileName)
{
int point = fileName.indexOf('/');
if (point == -1) {
point = fileName.indexOf('\\');
}
return point;
} public static void main(String[] args) {
File file = new File("D:\\temp\\SendTest1.0\\");
}}
说简单点就是,给一段路径,如何取得倒数第二个分隔符和最后一个分隔符之间的东西。
路径:D:\\temp\\SendTest1.0\\hello.java
我想从这个路径中得到SendTest1.0。
只需要 System.out.println(file.getParentFile().getName());
或者 File file = new File("D:\\temp\\SendTest1.0");
就 System.out.println(file.getName());