62,614
社区成员
发帖
与我相关
我的任务
分享
"\\" + fileStr, ""
就行了.import java.io.File;
public class Filepath {
public static void main(String[] args) {
String fileStr = "text.txt";
File file = new File(fileStr);
//getAbsolutePath()获得路径.
//replace 把后面换成 空 就行了
System.out.println(file.getAbsolutePath().replace("\\" + fileStr, " "));
}
}
E:\workspace\Csdner
String str="c:\book\hey.txt";
int index = str.lastIndexOf("\");
String path = str.substring(0,index);