Accessing External file Java Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Accessing External file Java without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m creating a Task-list application in Java but am running into a problem trying to access an external file from within my code. This is what I have so far:

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:
                case 2:
                case 3:
                case 0:
                    System.out.println("Invalid Input");

    static int menu(){
        int choice;
        Scanner sc = new Scanner(;
        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 to List");
        System.out.println("3: Sort Tasks by Due Date");
        System.out.println("Enter a Task: ");
        choice = sc.nextInt();
        return choice;

    static void showTaskList(){
        System.out.println("nTask Listn");
        try {
            Scanner inFile = new Scanner(new FileReader(fileName));
            String line;
            int number = 1;
                line = inFile.nextLine();
                System.out.println(number + " ");
        } catch (FileNotFoundException ioe) {
            System.out.println("Can't Access File");

    static void addTask(){
        System.out.println("nAdd Taskn");
        try {
            Scanner input = new Scanner(;
            PrintWriter outFile = new PrintWriter(new FileWriter(fileName, true));
            System.out.println("Enter a Task: ");
            String addedTask = input.nextLine();
            System.out.println("You Must set a Due Date for this task: ");
            String dueDate = input.nextLine();
            outFile.println(addedTask + " " + dueDate);
        } catch (IOException ioe) {
            System.out.println("Can't Access File");
    static void sortList(){}


When I enter choices 1 or 2 I get the “can’t access file” error. Could this just be a bad link to the external file? And suggestions help.


Your so called can't access file error is created by you:

System.out.println("Can't Access File");

What triggers this is when the given filepath and/or filename cannot be located, i.e: FileNotFoundException

Make sure you provide the correct filename and/or filepath.

We are here to answer your question about Accessing External file Java - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji