import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class ll{


public static void main(String[] a) {


FileReader fr;
    try {
      fr = new FileReader (new File("C:\\University Study\\Component Engineering\\Assignments\\Assignment !\\yourFile.txt"));
      BufferedReader br = new BufferedReader (fr);
      String line = br.readLine();
      while (line != null) {
          System.out.println(line);
          line = br.readLine();
      }
      br.close();
      
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }



		
}
}

The text file and the class are in the same folder and even when i specified the location I still get this exception. It's not a compile error it's a runtime error. I've tried to be as specific as i can but this is my first time posting.

Recommended Answers

All 12 Replies

Most probably, your file location is wrong. It might be "Assignment 1".

also the filenames are case sensitive, and windows has an annoying habit of naming things name.txt.txt

Try "right" clicking on the file and select "Properties". You will find the exact location and file name to use at your code

Are you sure about that explanation mark "!" in your file path string? fr = new FileReader (new File("C:\\University Study\\Component Engineering\\Assignments\\Assignment !\\yourFile.txt")); Also if you just learning to work with files try to keep your file in same folder as your compiled program so then you do not need to call absolute path as above and can simple use relative just yourFile.txt

PS: You class name doesn't have proper name in according to recommended naming conventions which for class says the first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase

Are you sure about that explanation mark "!" in your file path string? fr = new FileReader (new File("C:\\University Study\\Component Engineering\\Assignments\\Assignment !\\yourFile.txt")); Also if you just learning to work with files try to keep your file in same folder as your compiled program so then you do not need to call absolute path as above and can simple use relative just yourFile.txt

PS: You class name doesn't have proper name in according to recommended naming conventions which for class says the first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase

Hi, I am sorry it took a while to respond my internet has been playing up, and i had to spend hours on the phone with a guy from my ISP. I tried changing the folder name to just Assignment, not Assignment !, but it did not work, also i tried just writing fr = new FileReader (new File("yourFile.txt")); but it still will not work. here is the error i get at runtime

--------------------Configuration: <Default>--------------------
java.io.FileNotFoundException: yourFile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at ReadFromFile.main(ReadFromFile.java:14)

Process completed.
i also renamed my class and the class file name as ReadFromFile instead of ll. The java file and the txt file are in the same folder. Please help I am stuck, but i am pretty sure my code is correct.

Are you sure about that explanation mark "!" in your file path string? fr = new FileReader (new File("C:\\University Study\\Component Engineering\\Assignments\\Assignment !\\yourFile.txt")); Also if you just learning to work with files try to keep your file in same folder as your compiled program so then you do not need to call absolute path as above and can simple use relative just yourFile.txt

PS: You class name doesn't have proper name in according to recommended naming conventions which for class says the first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be uppercase

Hi, I am sorry it took a while to respond my internet has been playing up, and i had to spend hours on the phone with a guy from my ISP. I tried changing the folder name to just Assignment, not Assignment !, but it did not work, also i tried just writing fr = new FileReader (new File("yourFile.txt")); but it still will not work. here is the error i get at runtime

--------------------Configuration: <Default>--------------------
java.io.FileNotFoundException: yourFile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileReader.<init>(FileReader.java:55)
at ReadFromFile.main(ReadFromFile.java:14)

Process completed.
i also renamed my class and the class file name as ReadFromFile instead of ll. The java file and the txt file are in the same folder. Please help I am stuck, but i am pretty sure my code is correct.

What OS are you using? do you actually have read/write permissions on the file?

also the filenames are case sensitive, and windows has an annoying habit of naming things name.txt.txt

Thank you, you were right windows name it yourfile.txt.txt

no problem. If its xp go into my computer -> tools -> folder options and uncheck the box for "hide extensions for known filetypes" - its called something like that . From now on it will always show the extensions.

Make sure that you have in Folder Options deselected Hide extension option so you by accident do not name file name.txt.txt

this error is due to administrative rights. Most probably u dont have rights to read file on that location.

this error is due to administrative rights. Most probably u dont have rights to read file on that location.

Well done for replying 1 year old thread. If you read other replies you would find this was suggested. Nevertheless we did not learn what was cause of this.

Either-way thread close before another bright soul kick off...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.