dedmar
dedmar

Reputation: 411

how the user would give the full path of a file using java

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.

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?

Upvotes: 0

Views: 16889

Answers (2)

Rahul Bobhate
Rahul Bobhate

Reputation: 5082

You can use the Scanner class as follows:

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.

Upvotes: 0

Jaydeep Rajput
Jaydeep Rajput

Reputation: 3673

You can either pass the file name as commandline argument to the java program like

java <classname> <filepath>

OR

You can read the filepath provided by user from stad input

Refer Best way for line-by-line reading STDIN?

Upvotes: 2

Related Questions