-1
package files;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;

public class file {

    public static void main(String[] args)throws FileNotFoundException {

            File file = new File("txtfile.txt");
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                System.out.println(input.nextLine());
            }

    }

}

Where it says file.txt I have to enter the full file path. All tutorials I watch do not have to do this.

  • This may also help answer your question: https://stackoverflow.com/questions/16313260/file-path-or-file-location-for-java-new-file – Derek Dec 08 '18 at 05:35

2 Answers2

1

Yeah! File file = new File("txtfile.txt"); txtfile.txt is a path to your file that you want to read. Provide the path where file is like "C:\Users\me\Desktop\txtfile.txt" if the file is not in the same directory where your java file is. After you compile the java file a .class file is created it that .class file is also created in the same folder it will work with.

File file = new File("txtfile.txt"); and you don't need to specify the full path.

If not you then you have to provide the absolute file path like above.

Maseed
  • 533
  • 5
  • 19
  • 1
    @SirStrategic I believe Maseed is saying that if your file is not in the working directory, then you have to include the whole path. Otherwise you can just to `text.txt` – GBlodgett Dec 08 '18 at 05:38
  • Oh.... thanks for that.... But yes, text.txt is in the same directory as the java class. – SirStrategic Dec 08 '18 at 05:40
  • `text.txt` should be there where the file `file.class` is created, otherwise you have to provide an absolute `path` to your file. – Maseed Dec 08 '18 at 05:50
-2

If you don't enter the path it won't compile and show error. To set a path..

Open the command prompt and it show something like this C:user>admin You need to change it and point it your program saved location (use cd for change it)

Then type path="

And go to localdisc C: and open programfile->java->jdk->bin

Then save the path at above Which is something like c:/programfile/java/jdk1. 0./bin

Save and copy it in front of path="c:/programfile/java/jdk1. 0./bin";

Then press enter

Then compile the program using javac filename. Java

And run using java filename

J O C KER
  • 1
  • 1