bufferedreader - Java Cannot Read From File -
i writing java program can take user entries , save them arraylist, use arraylist open series of webpages. program should able read in web addresses file. i'm having issues.
i'm gettting: file bills.txt not found. //the file in src folder
exception in thread "main" java.lang.nullpointerexception @ paybills.main(paybills.java:92) //this when bufferdreader closed
this isn't homework program shares concepts homework do, don't want change fundamental how i'm reading in text. advice appreciated!
import java.io.*; import java.util.arraylist; public class paybills implements serializable { /*the purpose of program assist user in opening series of webpages * pay bills online. user able save , edit list of webpages, * open newly created list, or read list file. */ public static void main(string[] args) throws ioexception { char input1; string line = new string(); arraylist<string> list1 = new arraylist<string>(); runtime rt = runtime.getruntime(); string filename = new string(); try { // print out menu rt.exec( "rundll32 url.dll,fileprotocolhandler " + "http://www.google.com"); printmenu(); // create bufferedreader object read input keyboard inputstreamreader isr = new inputstreamreader (system.in); bufferedreader stdin = new bufferedreader (isr); { system.out.println("\nwhat action perform?"); line = stdin.readline().trim(); //read line input1 = line.charat(0); input1 = character.touppercase(input1); if (line.length() == 1) //check if user entered 1 character { switch (input1) { case 'a': //add address process array system.out.println("\nplease enter web address add list:\n"); string str1 = stdin.readline().trim(); if(str1.startswith("http://www.") || str1.startswith("https://www.")) list1.add(str1); else { system.out.println("please enter valid web address (starting http:// or https://)."); } break; case 'd': //show current list system.out.println(list1.tostring()); break; case 'e': //execute current list //rt.exec( "rundll32 url.dll,fileprotocolhandler " + "http://www.speedtest.net"); for(int = 0; < list1.size(); i++) { process p1 = runtime.getruntime().exec("cmd /c start " + list1.get(i)); } break; case 'r': //read list file system.out.println("\nplease enter filename read: "); { filename = stdin.readline().trim(); } filereader fr = null; bufferedreader infile = null; try { fr = new filereader(filename); infile = new bufferedreader(fr); line = infile.readline(); system.out.println("test2"); while(line != null) { system.out.println("test3"); if(line.startswith("http://www.") == false || line.startswith("https://www.") == false) system.out.println("error: file not in proper format."); else list1.add(line); } system.out.println(filename + " read."); } catch(filenotfoundexception exception) { system.out.println("the file " + filename + " not found."); break; } catch(ioexception exception) { system.out.println("error. " + exception); } { infile.close(); } break; case '?': //display menu printmenu(); break; case 'q': //quit system.out.println("goodbye!"); system.exit(0); }//end switch }//end if else system.out.print("unknown action\n"); }//end while (input1 != 'q' || line.length() != 1); } catch(ioexception e1) { system.out.println("error: " + e1); } }//end main public static void printmenu() { system.out.print("choice\t\taction\n" + "------\t\t------\n" + "a\t\tadd web address list\n" + "d\t\tdisplay current list\n" + "e\t\texecute current list\n" + "r\t\tread list file\n" + "?\t\tdisplay menu\n" + "q\t\tquit\n"); }//end of printmenu() }//end paybills
edit: ok, program no longer crashing because fixed npe, still getting "the file bills.txt not found.", caught exception. stated above, file located in src folder path should correct.
if passing file name, have append location of file before reading it
file file = new file(location + filename);
this how works if pass filename argument file constructor, try file @ /project/ directory (e.g. c:/workspace/project/test , if project name test , located @ c:/workspace/project)
so in order pass correct location have specify complete path of file trying read
so create string location hold location of folder file , append file name
string location = "c:/opt/files/"; string filename = "abc.txt"; file file = new file(location + filename);
this abc.txt should located @ "c:/opt/files/"
remember have added drive name because trying run main
but if same code running on server have specify path relative root directory server running
string location = "/opt/files/";
if pass file name code file in project folder not in folder java class is.
hope helps.
Comments
Post a Comment