-1

I am running into an error

java.io.FileNotFoundException: C:\courses2.txt (The system cannot find the file specified)

checked the c drive and the path is correct but for some reason I am not getting the output.

The most I got was a return Teachers copy and Unable to Read the file.

My Output should show course code, course credit hours, and course title

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class courses

{

    public static void main(String[] args) throws Exception

    {

        try {

            File file = new File("C:\\courses2.txt");

            BufferedReader br = new BufferedReader(new FileReader(file));

            String courseCode = "";

            String creditHours = "";

            String courseTitle = "";

            String st;

            System.out.println("Teacher's Copy");

            while ((st = br.readLine()) != null) {

                courseCode = st.substring(0, st.indexOf(" "));

                creditHours = st.substring(6, 8);

                courseTitle = st.substring(9);

                System.out.print("Course code = " + courseCode + " | ");

                System.out.print("Course credit hours = " + creditHours + " | ");

                System.out.print("Course Title = " + courseTitle);

                System.out.println();

            }

        } catch (Exception ex) {

            System.out.println(ex);

        }

    }

}

1 Answers1

1

First, write a simple code to check whether a particular file exists or not.

Code :

public class Test {

   public static void main(String...args) {
       File file = new File("C:\\courses2.txt");
       if(file.exists()) {
           System.out.println("File exists");
       } else {
           System.out.println("File doesn't exist");
       }

   }

} 

If file doesn't exist, then check whether the file is missing from that path or mismatch file name happened.

Anish B.
  • 9,111
  • 3
  • 21
  • 41