用户如何使用 java 给出文件的完整路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16232535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how the user would give the full path of a file using java
提问by dedmar
i have a problem and i think that someone maybe could help me because i am a little bit confused. I had made a program where i am reading excel files and i am storing their data in a database. In this database each excel file create a table. I have made the program and everything works fine up to here. Now, after the connection that i had done, i would like the user to be able to give the full path of the file that would read in the client and afterwards to continue the rest of the program, read the data and store them in a table. Could anyone help me how i would do this? Below is the code for connection.
我有一个问题,我认为有人可以帮助我,因为我有点困惑。我制作了一个程序,我在其中读取 excel 文件并将它们的数据存储在数据库中。在这个数据库中,每个 excel 文件创建一个表。我已经制作了程序,到这里一切正常。现在,在我完成连接之后,我希望用户能够提供在客户端读取的文件的完整路径,然后继续程序的其余部分,读取数据并将它们存储在一个桌子。谁能帮助我如何做到这一点?下面是连接代码。
public static void main (String[] args) throws Exception {
try {
System.out.println("get the connection");
}
catch( Exception e )
{
System.out.println( "SQLException: " + e.getMessage() );
}
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/kainourgia", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()){
System.out.println (" " +res.getString(1));
}
//String filename = "C:\Users\myfiles\Documents\test5.xls";
String fullPath = "C:\Users\myfiles\Documents\test5.xls";
String Path = "C:\Users\myfiles\Documents\";
String filename = "test5.xml";
String[] parts = filename.split("\.");
String tablename = parts[0];
Could anyone help me?
有人可以帮助我吗?
回答by JRR
You can either pass the file name as commandline argument to the java program like
您可以将文件名作为命令行参数传递给 java 程序,如
java <classname> <filepath>
java <classname> <filepath>
OR
或者
You can read the filepath provided by user from stad input
您可以从 stad 输入读取用户提供的文件路径
回答by Rahul Bobhate
You can use the Scanner class as follows:
您可以按如下方式使用 Scanner 类:
Scanner scanner = new Scanner(System.in);
String filename = scanner.nextLine();
The user can input the name of the File to the standard input. And in this case, the nextLine()
method will read the line from the standard input of the system.
用户可以将文件的名称输入到标准输入中。在这种情况下,该nextLine()
方法将从系统的标准输入中读取该行。