different categories of false file path java -


i making program read data excel file , store them in mysql database. in program user give path of file use. code doing below.

 string strfullpath = "";  scanner scanner = new scanner(system. in );  system.out.println("please enter fullpath of file");  strfullpath = scanner.nextline();  string file = strfullpath.substring(strfullpath.lastindexof('/') + 1);  system.out.println(file.substring(0, file.indexof('.')));  @suppresswarnings("unused")  string filename = strfullpath.substring(strfullpath.lastindexof('\\') + 1);  system.out.println(filename);  string[] parts = filename.split("\\."); 

then user when gives full path in console have options of making mistake. example if write file there not in folder or if write special caracter not recognized or example if write path in not appropriate way. how give these command in java? how user understand type wrong?

you should make validation loop:

while (true) {     system.out.println("please enter fullpath of file:");     strfullpath = scanner.nextline();     file f = new file(strfullpath );     if (f.canread()) break;     system.out.println("error: file not exist"); } 

edit method checks if path valid , tries correct it:

public static final string correctpath(final string path) {     try {         final string returnvalue = new file(path).topath().toabsolutepath().normalize().tofile().tostring();         system.out.println(returnvalue);         return returnvalue;     } catch (final invalidpathexception e) {         system.err.println(e.getmessage());         final int errorindex = e.getindex();         final string newpath = path.substring(0, errorindex - 1) + path.substring(errorindex + 1);         return correctpath(newpath);     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -