I am writing a program for my comp sci class, andI keep getting the same error.
Exception in thread "main" java.io.FileNotFoundException: data.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.util.Scanner.<init>(Scanner.java:636)
at Search.main(Search.java:18)
Here is the beginning of my code:
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
public class Search{
public static void main(String[] args)throws IOException{
Scanner inData = new Scanner(new File("data.txt"));
String data=inData.nextLine();
String[] arr = data.split(" ");
while(inData.hasNext()){
String search=inData.nextLine();
int len=search.length();
ArrayList<String> result = new ArrayList<String>();
I have a text file in the same Java Project, so I'm not sure what the problem is and I've tried moving the location of the file around but nothing is working.
The working path of your executed code can be determined with this code:
System.out.println(new File(".").getAbsolutePath());
Usually this is the target or the classes or the bin folder, depending on your IDE.
You have to put the file data.txt in the root of your eclipse java project , outside your folder /src/.
Related
I'm trying to read from a file using the Scanner and File class:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class TextFileReaderV1
{
public static void main(String[] args) throws IOException
{
String token = "";
File fileName = new File("data1.txt");
Scanner inFile = new Scanner(fileName);
while( inFile.hasNext() )
{
token = inFile.next( );
System.out.println(token);
}
inFile.close();
}
}
However, it is saying, "no such file or directory". and giving me the "java.io.FileNotFoundException"
I am using IntelliJ IDEA and the file is in the current directory I am working in: src/data1.txt -> next to GetFile.java (current code)
Full Error Message:
Exception in thread "main" java.io.FileNotFoundException: data1.txt (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at GetFile.main(GetFile.java:19)
**Edit: ** It has been solved!! The run configuration was set to the project directory, not the src one, so I implicitly added it in the argument:
File fileName = new File("src/data1.txt");
The run configuration was set to the project directory, not the src one, so I implicitly added it in the argument:
File fileName = new File("src/data1.txt");
Try entering the full path of the file. If that works, you can either be done at that point or look into relative file paths.
I am creating a program where two numbers are read from one file (test.in) and then the sum is output to another file (test.out). I created the two files as TXT documents in the bin folder of my project but it still gives this
Exception in thread "main" java.io.FileNotFoundException: test.in.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at test.main(test.java:11)
The files are not showing up in my project directory on the left hand side of the screen, it just won't work.
import java.io.*;
import java.util.*;
public class test {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("test.in.txt"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out.txt")));
StringTokenizer st = new StringTokenizer(f.readLine());
int i1 = Integer.parseInt(st.nextToken()); // first integer
int i2 = Integer.parseInt(st.nextToken()); // second integer
out.println(i1+i2); // output result
out.close(); // close the output file
}
}
As you can see your file is an textdocument, so it ends with ".txt" even if you can't see it in the folder!
You have to change "test.in" and "test.out" into "test.in.txt" and "test.out.txt"
import java.io.*;
import java.util.*;
public class test {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("test.in.txt"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out.txt")));
StringTokenizer st = new StringTokenizer(f.readLine());
int i1 = Integer.parseInt(st.nextToken()); // first integer
int i2 = Integer.parseInt(st.nextToken()); // second integer
out.println(i1+i2); // output result
out.close(); // close the output file
}
}
If your files are ".txt" your strings "test.in" and "test.out" need to be renamed.
The other option is to change "test.in.txt" to "test.in" and
"test.out.txt" to "test.out".
Your files have to be from type "IN" and "OUT" not "TXT". As your screenshot shows they are not.
The fact that you are not seeing the suffix "txt" may be depending on your settings on your OS.
Your files also have to be in the same directory as your test.java file.
You should check your path and the filename. If "test.in" file is not in the folder that you invoked "java" command, it is normal to get this exception.
Putting your files in the bin of the project is not the solution. The files are supposed to be in the root directory of your project. There might be some other issues like the name of your files; try to reference the files by their absolute path. E.g. BufferedReader f = new BufferedReader(new FileReader("C:/MyProjetFolder/bin/test.in"));.
If that doesn't work then the issue could be the extensions of the files. So check the metadata of your files to see if the extensions are as expected. If it's not, then rename the files to the extensions in the metadata (properties).
You dont have to put the files in the bin folder. Put them in your project folder, the parent of bin/ and it will work.
I know this is a long time after I posted the question but I stumbled upon it and discovered that the real reason that it did not work is because the "f" input reader is never closed while the "out" output reader is closed. I simply had to add "f.close();" in order to make it work. It had nothing to do with the names of the text files, lol.
Here is the working code.
import java.io.*;
import java.util.*;
public class test {
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("test.in.txt"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out.txt")));
StringTokenizer st = new StringTokenizer(f.readLine());
int i1 = Integer.parseInt(st.nextToken()); // first integer
int i2 = Integer.parseInt(st.nextToken()); // second integer
out.println(i1+i2); // output rresult
f.close();
out.close();
// close the output file
}
}
I am trying to use the values from a text file in my program, but first I would like to really understand how to use JFileChooser which I cannot make it work.
The program:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class Hw7Problem2 {
public static void main(String[] args) throws FileNotFoundException {
JFileChooser student_scores = new JFileChooser();
int jfcUserOption = student_scores.showOpenDialog(null);
// To verify it reads
if (jfcUserOption == JFileChooser.APPROVE_OPTION) {
File chosenFile = student_scores.getSelectedFile();
System.out.println("The file you chose was: " + chosenFile.getName());
}
Scanner scanner = new Scanner(new File("student_scores.txt"));
// Print text file on program
System.out.println(scanner);
}
}
The error:
The file you chose was: student_scores.txt
Exception in thread "main" java.io.FileNotFoundException: student_scores.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Hw7Problem2.main(Hw7Problem2.java:21)
You are doing this correctly until you create the scanner. The problem is that you aren't using the result of the JFileChooser. It looks like you put the result in chosenFile. getSelectedFile() will return the file that was chosen, so you just need to create the scanner with it.
If you need to understand more about how the JFileChooser works, you can find the documentation online here.
So there is the code:
public class Main{
public static void main(String[] argv) throws IOException{
new Main().run();
}
PrintWriter pw;
Scanner sc;
public void run() throws IOException{
sc = new Scanner(new File("input.txt"));
int a=sc.nextInt();
pw = new PrintWriter(new File("output.txt"));
pw.print(a*a);
pw.close();
}
}
Error:
Exception in thread "main" java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Main.run(Main.java:14)
at Main.main(Main.java:8)
Like i understand it can't find file named input.txt, BUT! I have that file in same directory where Main class is, what can be the promblem then?
p.s Tried on cmd and eclipse, both give same error.
it is not relative to your Main class, it is relative from where you launch this Java program (i.e. current work directory)
it is relative to
System.getProperty("user.dir")
You probably need to specify the PATH to your file, one thing you can do is test for existence and readability with File.canRead() like
File file = new File("input.txt");
if (!file.canRead()) {
System.err.println(file.getCanonicalPath() + ": cannot be read");
return;
}
An example using a PATH might be (for Windows) -
File file = new File("c:/mydir/input.txt");
You can use System.out.println(System.getProperty("user.dir")) to see where Java is looking for the file by default. This is most likely your project folder. This is were you have to put the file if you don't want to specify an absolute path.
1.Hello
What i really meant is that when i run this program i get an unknown file named'mm' in the desktop.The program is not working in the way which i wanted.i want to copy 'oo.txt' from 'nn' folder to 'mm' folder
import java.nio.file.*;
import static java.nio.file.StandardCopyOption.*;
import java.io.*;
public class ListOfNumbers {
public static void main(String[] args)
{
Path p1 = Paths.get("C:\\Documents and Settings\\Administrator\\Desktop\\nn\\oo.txt");
Path p2 = Paths.get("C:\\Documents and Settings\\Administrator\\Desktop\\mm");
try{
Files.copy(p1,p2,REPLACE_EXISTING);
}catch(IOException e){
System.err.println("ff");
}
}
}
I guess this is the problem line
Path p2 = Paths.get("C:\\Documents and Settings\\Administrator\\Desktop\\mm");
I guess you are trying to copy the file with the same name to "mm" folder. If this is the case then you need to specify the name of the file. What is happening is that the file copy function is copying the file as "mm" on your Desktop folder. To verify open your file in NotePad and see the contents..
Just change the line as
Path p2 = Paths.get("C:\\Documents and Settings\\Administrator\\Desktop\\mm\\oo.txt");
and you should be fine.