0

Hello I am a new learner in java. I have been trying to learn file related java problems but I can't seem to run the basic file related codes in my eclipse client. I tried online compiler as well but no luck. I am getting errors like "The filename, directory name, or volume label syntax is incorrect" "cannot find symbol" " File not found" . My jdk and everything is up to date. Can anyone point out what am I doing wrong ?

This is the code I have been trying to run. for the input file 1 2 3 4 .

package QuestionA;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.Scanner;

    
public class NewClass1 {
     public static void main(String[] args) {
            try {
              File myObj = new File("input.txt");
              Scanner myReader = new Scanner(myObj);
              while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                System.out.println(data);
              }
              myReader.close();
            } catch (FileNotFoundException e) {
              System.out.println("An error occurred.");
              e.printStackTrace();
            }
          } 
    }    

Error Message

This is my first time posting in stackoverflow. I apologize if I posted this in wrong way.

  • Where are your import statements? These are quite important when debugging "cannot find symbol" errors. And are you importing `java.io.File`? – Hovercraft Full Of Eels Jan 10 '22 at 15:33
  • Sorry, it is `import java.io.File;` – Hovercraft Full Of Eels Jan 10 '22 at 15:34
  • yes I used import java.io.File; – Iftekhar Mahmud Jan 10 '22 at 15:35
  • I was trying out bunch of ways. These are I used import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; – Iftekhar Mahmud Jan 10 '22 at 15:37
  • 2
    Please show the full code and the full error message that you receive. Please check out the [ask] and [tour] links to see how to best use this site. – Hovercraft Full Of Eels Jan 10 '22 at 15:37
  • 1
    Show that information as an [edit] to your question, not in comments. – Hovercraft Full Of Eels Jan 10 '22 at 15:37
  • 1
    The error message says that there is no file "D:\OOP LAB\Assignment3\input.txt" - Have you made sure that a file with that name exists under that directory? Otherwise that error is what you should expect and what you told your program to do (Outputting "An error occurred." and the Exception stacktrace) – OH GOD SPIDERS Jan 10 '22 at 15:49
  • 1
    Please look at this Q&A: [Java says FileNotFoundException but file exists](https://stackoverflow.com/questions/19307622/java-says-filenotfoundexception-but-file-exists). You're likely using the wrong path to the file. Note that if you only need to read from the resource, you're usually better off not using files but rather class resources – Hovercraft Full Of Eels Jan 10 '22 at 15:49
  • Eclipse should have a setting where you run your code. The working directory is probably not the same directory that contains "input.txt" . – matt Jan 10 '22 at 15:52
  • Also, check out [What's the difference between a Resource, URI, URL, Path and File in Java?](https://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-and-file-in-java) – Hovercraft Full Of Eels Jan 10 '22 at 15:53
  • The directory is always ok. I checked them multiple times. Thank you for the links @HovercraftFullOfEels – Iftekhar Mahmud Jan 10 '22 at 16:05
  • @IftekharMahmud: I wouldn't rush to that assumption if I were you. Test it in multiple different ways with simple programs. – Hovercraft Full Of Eels Jan 10 '22 at 16:06
  • Add this line as the first line in method `main`: `System.out.println(System.getProperty("user.dir"));` The value returned by `System.getProperty("user.dir")` is the directory that Java will search in order to locate file _input.txt_ If the file is not in that directory then the following line (of your code) will throw `FileNotFoundException`: `Scanner myReader = new Scanner(myObj);` Also, consider using [try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – Abra Jan 10 '22 at 18:23

0 Answers0