【发布时间】:2013-04-29 07:59:35
【问题描述】:
我正在制作一个程序来将 excel 文件中的数据存储在数据库中。我希望用户在控制台中提供文件的完整路径,并在程序之后仅采用文件名继续。
加载全路径的代码为:
String strfullPath = "";
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the fullpath of the file");
strfullPath = scanner.nextLine();
String file = strfullPath.substring(strfullPath.lastIndexOf('/') + 1);
System.out.println(file.substring(0, file.indexOf('.')));
之后我想拥有:String filename = .......
用户输入的完整路径如下:C:\\Users\\myfiles\\Documents\\test9.xls
我将创建的文件名将只使用没有.xls 的名称!
谁能帮助我如何做到这一点?
如果我想将文件名“test9.xls”作为文件名,我会怎么做? ——
【问题讨论】:
-
把 '/' 改成 '\\' String file = strfullPath.substring(strfullPath.lastIndexOf('\\') + 1);
-
如果我想取为文件名“test9.xls”我会怎么做?