I am getting an error when I try to read my file abc.txt in my D drive.
Even I tried format : "D:\EDU\java\abc.txt"
Here's my code :
package javapro;
import java.io.FileInputStream;
public class office {
public static void main (String[] args)throws Exception {
FileInputStream apple = new FileInputStream ("D:/EDU/java/abc.txt");
int din;
while ((din=apple.read())!=-1){
System.out.println((char)din);
}
apple.close();
}
}
My Error :
Exception in thread "main" java.io.FileNotFoundException: D:\EDU\java\abc.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 javapro.office.main(office.java:8)
Make sure the file is actually located in that directory. Right-click and click on Properties to check the path.
If you've done that, change all the \ to / or \\.
The error is self-explanatory. The file isn't where you have told the application it is. Check your path to make sure that it leads to the file.
1) Change the code as below
FileInputStream apple = new FileInputStream ("D:\\EDU\\java\\abc.txt");
or
InputStream is = getClass().getResourceAsStream("abc.txt");
//if abc.txt is present in classpath
From InputStream, you have to read the data.
EDIT: Resolve non static error
InputStream is = office.class.getClass().getResourceAsStream("abc.txt");
Related
I am trying to get a JSON file form my resource folder in the main method using relative path. The code works using absolute path but this breaks once I build a jar file from my project which is want I want.
public static void main(String[] args) throws FileNotFoundException {
// Read in database
db = Database.read(Thread.currentThread().getContextClassLoader().getResource("JSON/inhabitants.json").toExternalForm());
names = db.getAllNames();
Read calls a method in Database which uses a inputstream to read the file.
public static Database read(String filename) throws FileNotFoundException {
InputStream is = new FileInputStream(filename);
Reader reader = new InputStreamReader(is);
return gson.fromJson(reader, Database.class);
}
The error I am getting is the following :
java.io.FileNotFoundException:
file:/Users/timpelser/IdeaProjects/TurfApp/target/classes/JSON/inhabitants.json
(No such file or directory) at java.io.FileInputStream.open0(Native
Method) at java.io.FileInputStream.open(FileInputStream.java:195) at
java.io.FileInputStream.(FileInputStream.java:138) at
java.io.FileInputStream.(FileInputStream.java:93) at
Core.Database.read(Database.java:22) at Main.main(Main.java:51) ...
11 more
The file in directory /Users/timpelser/IdeaProjects/TurfApp/target/classes/JSON/inhabitants.json
does exist however so I have no idea what is going wrong.
Here is my folder structure (Maven basic structure):
Is there a solution which will still able me to deploy it as a jar file ?
EDIT (25/09) : If I use getResourceAsStream instead of getResource, I am getting the following error :
Caused by: java.io.FileNotFoundException: java.io.BufferedInputStream#4f8e5cde (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.io.FileInputStream.<init>(FileInputStream.java:93)
at Core.Database.read(Database.java:22)
at Main.main(Main.java:51)
... 11 more
You have to use getResourceAsStream to read files from within the running jar (which contains the files within src\main\resources)!
Use This,
Resource resource = new classpathResource(json);
new ObjectMapper.readValue(resource.getInputStream(),Object.class);
I am currently creating a err... annoying program that repeatedly plays a ding sound, but there is a error whenever I run it. I have tried everything, and the file IS in the correct spot. Here is my current code:
public class PlaySound {
public static void main(String[] args) throws Exception {
while (true) {
String path = PlaySound.class.getProtectionDomain().getCodeSource().getLocation().getPath().replaceAll("%20", " ");;
InputStream in = new FileInputStream(path + "//src//ding.wav");
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
TimeUnit.SECONDS.sleep(1);
}
}
}
And yes, I have used other formats of the code like //src//ding.wav
Any help would be appreciated.
EDIT: also the error is
Exception in thread "main" java.io.FileNotFoundException: C:\Users\*** ******\Desktop\ding.jar\src\ding.wav (The system cannot find the path 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 net.lookatthis.PlaySound.main(PlaySound.java:19)
EDIT2: The error is older before I renamed the file. I changed the error to reflect the current filename
I think you're trying to read a resource in a jar file specifying the absolute path of your hard disk drive.
ding.jar\src\hit.wav
So there are two alternatives, or unzip your ding.jar file into a directory.
Or specify the relative path and access to the file using the classloader resource reader.
Using the resource reader you can find to the hit.wav file using
InputStream in = PlaySound.class.getResource("/ding.wav").openStream();
AudioStream audioStream = new AudioStream(in);
You probably need to unjar your ding.jar to expand its file structure. FileInputStream (probably?) can't access the jar'ed version of this file.
I'm making a simple game.
Eclipse reads a text file but when I export my project as runnable jar file it doesn't read it.
My resource folder looks like this: http://i.stack.imgur.com/LK2SF.png
The code:
BufferedReader br = new BufferedReader(
new FileReader("Resources/LevelManager/LevelManager.txt"));
When i run runnable jar file I get this error:
java.io.FileNotFoundException: Resources\LevelManager\LevelManager.txt (System cant find the path)
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 LevelManager.Utils.getLevel(Utils.java:13)
at LevelManager.LevelManager.<init>(LevelManager.java:15)
at State.GameState.<init>(GameState.java:26)
at Main.Game.init(Game.java:54)
at Main.Game.run(Game.java:80)
at java.lang.Thread.run(Unknown Source)`
Thank you for your answers.
This is a classical error.
Your application will run in its own jar; which means its resources will be included in the jar and not available as File objects. You need to use a classloader.
I'm not sure how you intend to read from the resource but here is how to obtain a stream from a resource (and please note that slashes are used for separators, always:
#WillNotClose //this is a JSR 305 annotation
public static InputStream loadResource(final String resourcePath)
throws IOException
{
final URL url = EnclosingClass.class.getResource(resourcePath);
if (url == null)
throw new IOException(resourcePath + ": resource not found");
return url.openStream();
}
this is my first time posting on stackoverflow. I have a question about a series of errors that I have been encountering when I try to read in data from a generic text file in Netbeans IDE 7.4. I am using a 2009 iMac with Mac OS X Mavericks.
import java.util.Scanner;
import java.io.File;
public class two {
public static void main(String[] args) throws Exception
{
System.out.println("Directory: "+System.getProperty("user.dir"));
File f = new File("newfile.dat");
Scanner s = new Scanner(f);
}
}
And this code will return this set of errors:
Directory: /Users/omavine/Desktop/aPlusComputerScience
Exception in thread "main" java.io.FileNotFoundException: newfile.dat (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:143)
at java.util.Scanner.<init>(Scanner.java:656)
at apluscomputerscience.two.main(two.java:22)
at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)
Java Result: 1
This would indicate to me that the file simply was not being searched for in the correct path, however, when I compare the filename with the absolute path:
System.out.println("Directory: "+System.getProperty("user.dir"));
File f = new File("newfile.dat");
System.out.println("Path: "+f.getAbsolutePath());
Then the output is as follows:
Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat
Which would indicate (to me) that the file is being searched for in the correct place whether I look for it with the explicit pathname or not.
However, when I try to construct the Scanner, (even with the absolute pathname):
Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/maven/Desktop/aPlusComputerScience/newfile.dat
Exception in thread "main" java.io.FileNotFoundException: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:143)
at java.util.Scanner.<init>(Scanner.java:656)
at apluscomputerscience.two.main(two.java:23)
at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)
Java Result: 1
Still the same error. Interestingly enough, attempting to construct the scanner by using:
File f = new File("newfile.dat");
Scanner s = new Scanner(f.getClass().getResourceAsStream("newfile.dat"));
Returns:
Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/omavine/Desktop/aPlusComputerScience/newfile.dat
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at java.util.Scanner.<init>(Scanner.java:608)
at apluscomputerscience.two.main(two.java:23)
at apluscomputerscience.APlusComputerScience.main(APlusComputerScience.java:21)
Java Result: 1
(Reading in the file with the getClass().getResourceAsStream() seems to return a null pointer exception, for whatever reason.)
I'd like to be able to read files on my home computer. This type of scenario has never happened to me at other computers with JCreator IDE. Can anyone make heads or tails of this dilemma?
the output clearly indicates you are looking at different directories.
Directory: /Users/omavine/Desktop/aPlusComputerScience
Path: /Users/maven/Desktop/aPlusComputerScience/newfile.dat
the working directory is in omavine's desktop. your file is in maven's desktop.
Instead of giving the relative path, try giving the actual path of the file.
File f = new File("/Users/maven/Desktop/aPlusComputerScience/newfile.dat");
this ought to work.
EDIT: about the NullPointerException for getResourceAsStream
the java API doc statest that
Returns:
An input stream for reading the resource, or null if the resource could not be found
and the Scanner throws the NullPointerException.
refer javadocs
I have a program that scans a file and then closes the file.
import java.util.*;
import java.io.*;
public class FileTester{
public static void main(String[] args) throws IOException {
File test = new File("MyDatta.in.txt");
Scanner sf = new Scanner(test);
sf.close();
}
}
When I run the program I get an error message like this:
Exception in thread "main" java.io.FileNotFoundException: MyDatta.in.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 FileTester.main(FileTester.java:6)
I have a mac that runs on Mac OS. I have reason to believe it has to do with the pathway to my file which is in documents. I know in windows one would use C:\folder name\file to scan it but I just don't know with Mac and I cannot find it anywhere
From Java documentation for FileInputStream: "If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown."
Maybe file is used by another program?
You should put this as the file path:
~/Documents/MyDatta.in.txt
to tell java that your file is in documents. The ~ is your home folder.