Accessing External file Java -
i'm creating task-list application in java running problem trying access external file within code. have far:
import java.io.*; import java.util.scanner; public class main { public static string filename = "users/bobsmith/desktop/tasklistjava/src/javatask.txt"; public static void main(string[] args) throws ioexception { int menuitem = -1; while(menuitem != 0){ menuitem = menu(); switch (menuitem){ case 1: showtasklist(); break; case 2: addtask(); break; case 3: sortlist(); break; case 0: break; default: system.out.println("invalid input"); } } } static int menu(){ int choice; scanner sc = new scanner(system.in); system.out.println("\n task list menu \n"); system.out.println("0: exit menu"); system.out.println("1: show tasks in list"); system.out.println("2: add task list"); system.out.println("3: sort tasks due date"); system.out.println(); system.out.println("enter task: "); choice = sc.nextint(); return choice; } static void showtasklist(){ system.out.println("\ntask list\n"); try { scanner infile = new scanner(new filereader(filename)); string line; int number = 1; while(infile.hasnextline()){ line = infile.nextline(); system.out.println(number + " "); system.out.println(line); ++number; } system.out.println(); infile.close(); } catch (filenotfoundexception ioe) { system.out.println("can't access file"); } } static void addtask(){ system.out.println("\nadd task\n"); try { scanner input = new scanner(system.in); printwriter outfile = new printwriter(new filewriter(filename, true)); system.out.println("enter task: "); string addedtask = input.nextline(); system.out.println("you must set due date task: "); string duedate = input.nextline(); outfile.println(addedtask + " " + duedate); outfile.close(); } catch (ioexception ioe) { system.out.println("can't access file"); } } static void sortlist(){} }
when enter choices 1 or 2 "can't access file" error. bad link external file? , suggestions help.
your called can't access file
error created you:
system.out.println("can't access file");
what triggers when given filepath and/or filename cannot located, i.e: filenotfoundexception
make sure provide correct filename and/or filepath.
Comments
Post a Comment