java - Using a Dynamic path for a csv file -
i have program saves on file. current code set file save on specific path, when run program different computer program doesn't work , need change path everytime.
public createcustomer() { initcomponents(); arraylist<string> considlist = new arraylist<string>(); string csvfiletoread = "e:\\ryan_assignment_sit2\\consid\\consid.csv"; // reads csv file. bufferedreader br = null; // creates buffer reader. string line = ""; string splitby = ","; // reader delimiter try { br = new bufferedreader(new filereader(csvfiletoread)); // buffer reader file name read. scanner reader = new scanner(system.in); while ((line = br.readline()) != null) { //while there line read. reader = new scanner(line); reader.usedelimiter(splitby); while (reader.hasnext()) { // while there next value (token). considlist.add(reader.next()); } } } catch (filenotfoundexception exception) { // exception handler if file not found. exception.printstacktrace(); } catch (ioexception exception) { // input/output exception exception.printstacktrace(); } { if (br != null) { try { br.close(); // close scanner. } catch (ioexception exception) { exception.printstacktrace(); } }
i placed file in subfolder in program name consid , tried changing path file
string csvfiletoread = "..\\consid\\consid.csv";
but file can't read program.
string csvfiletoread = "e:\ryan_assignment_sit2\consid\consid.csv"; above path applicable windows. if execute program in linux environment filenotfoundexception. eventhough change file, again hardcoding file path. better can runtime parameters program executed irrespective of os.
Comments
Post a Comment