i was trying to use a txt file but i got this eror "FileNotFoundException"
but it was readable and it exist but on line FileInputStream i got that error
whats the matter?
System.out.println(Files.isReadable(Paths.get("I:/Code/Coding/src/Files/" + path + ".txt")));
System.out.println(Files.exists(Paths.get("I:/Code/Coding/src/Files/" + path + ".txt")));
FileInputStream f1=new FileInputStream("I:/Code/Coding/src/Files/" + path + ".txt");
reader = new ObjectInputStream(f1);
java.io.FileNotFoundException: I:\Code\Coding\src\Files\Artists.txt (The system cannot find the path specified)
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 sample.Datebase.Server.readFiles(Server.java:70)
at sample.Datebase.Server.run(Server.java:99)
at sample.Datebase.Server.main(Server.java:54)
Exception in thread "main" java.lang.NullPointerException
at sample.Datebase.Server.readFiles(Server.java:94)
at sample.Datebase.Server.run(Server.java:99)
at sample.Datebase.Server.main(Server.java:54)
I had the same problem and i solved it by using File class. Please try this code. maybe your problem will be solved :
File file = new File("I:/Code/coding/src/Files/" + path + ".txt");
reader = new ObjectInputStream(new FileInputStream(file));
Object o = reader.readObject();
The first 3 lines of this code snippet looks ok, ideally it should not throw a FileNotFoundException if the Files.exists, and Files.isReadable gives true.
But you can not use an ObjectInputStream to read a normal text file, as it will look for certain file headers to interpret the java object serialized.
Can you please copy paste the exception trace ?
FileNotFoundException - if the file exist but cannot be opened for reading then also throws FileNotFoundException exception
Read for Details FileInputStream
First, check the permission of that file. Is it opened for reading or not.
public static boolean isReadable(Path path): return true if the file
exists and is readable but it is not guaranteed; Note that the result of this
method is immediately outdated, there is no guarantee that a
subsequent attempt to open the file for reading will succeed (or even
that it will access the same file). Care should be taken when using
this method in security sensitive applications.
Related
I've the below project structure in my eclipse.
and my code in servlet is as below.
File entityFile = new File(getServletContext().getContextPath() + "/EntityList/entities.txt");
FileWriter fout = new FileWriter(entityFile);
fout.write("The Content");
fout.close();
here basically I'm trying to write to a file available at /EntityList/entities.txt, but when I run this, I get the exception as below.
SEVERE: Servlet.service() for servlet
[com.luis.servlets.WriteEntityToAFile] in context with path
[/LUISWebUI] threw exception java.io.FileNotFoundException:
\LUISWebUI\EntityList\entities.txt (The system cannot find the path
specified)
I know that I'm going wrong with the path, Can someone please put me in the right direction.
Update
Apologies for the confusion.
I'm sending some data from jsp to servlet to write to the entities.txt file. I'm able to capture it in servlet(Cross checked it by doing sysout).
First of all I think you have a typo problem, try /EntitiesList/entities.txt instead of /EntityList/entities.txt.
Also, move the /EntitiesList/entities.txt under /WEB-INF/ for example, so that your servlet class can access it.
You can read a more detailed explanation in this SO answer.
Edit:
About writing to file: your application will be packaged inside a WAR file so you won't be able to write to it, only read from it (more about this here).
But you can just use this way of creating directly a file outside the WAR and using this location to write your content (before that, make sure you have appropriate rights):
File entityFile = new File(getServletContext().getContextPath() + "entities.txt");
FileWriter fOut = new FileWriter(entityFile);
fOut.write("The Content");
fOut.close();
Or if you want the directory also, you'll have to execute some additional steps, create it first then specify the file name inside it where you want to write:
File entityFolder = new File(getServletContext().getContextPath() + "EntitiesList");
entityFolder.mkdir();
File entityFile = new File(entityFolder, "entities.txt");
FileWriter fOut = new FileWriter(entityFile);
fOut.write("The Content");
fOut.close();
First there is Typo error in your path
Second you should use getResourceAsStream() for loading file for writing.
Code example:
InputStream input = getServletContext().
getResourceAsStream("/WEB-INF/EntityList/entities.txt");
Files.copy(InputStream input , Path target)
//Or Files.copy(Path source, OutputStream out)
It seem easier to use FileInputStream, but it is better to use ResourceStream.
Read here https://stackoverflow.com/a/2161583/8307755
https://stackoverflow.com/a/2308224/8307755
I get the below exception.
Failed to GET File Details from http://192.168.2.111/storage java.io.FileNotFoundException: http:/192.168.2.111/storage/data/18-Apr-2017-ops (No such file or directory)
junit.framework.AssertionFailedError: Failed to GET File Details from http://192.168.2.111/storage java.io.FileNotFoundException: http:/192.168.2.111/storage/data/18-Apr-2017-ops (No such file or directory)
at com.billing.getlogs.GetLogsTest.countLinesOperationsUpload(GetLogsTest.java:222)
at com.billing.getlogs.GetLogsTest.GetLogsLineCountTest(GetLogsTest.java:264)
If you look, the URL after java.io.FileNotFoundException: only has one / in the after http:. The URL is read from a properties file. Is there any reason why this would be getting removed as the / character correctly appears in other parts of the url.
Got it.
I changed
InputStream is = new BufferedInputStream(new FileInputStream(text));
to
InputStream is = new BufferedInputStream(new URL(text).openStream());
I had been trying to read the contents of a URL but my original code was for reading the contents of a file.
I'm trying to get to some text file in my computer but I keep getting this exception although the path is correct and the file is exist.
Here is my code:
public static void main(String[] args) throws IOException {
File wordFile = new File("D:\\IDC\\Stuff\\wordList.txt");
RandomAccessFile wordsList = new RandomAccessFile(wordFile, "rw");
System.out.println(wordFile.exists());
}
The error:
Exception in thread "main" java.io.FileNotFoundException: D:\IDC\Stuff\wordList.txt (The filename, directory name, or volume label syntax is incorrect)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at WordChecker.main(WordChecker.java:12)
When I copied your code and tried to save it in Eclipse. I got the below error
I concluded from this, although your path looks 'D:\\IDC\\Stuff\\wordList.txt' but actually it is not.So what I did, just type this line File file =new File("D:\\IDC\\Stuff\\wordList.txt"); instead of copy it from your code. And it worked. It seems you also copied it from somewhere and for encoding issue you are getting the problem.
One more point, you should use System.getProperty("file.separator") instead of \\ or / just like below
File wordFile = new File("D:" + System.getProperty("file.separator")
+ "IDC" + System.getProperty("file.separator") + "Stuff"
+ System.getProperty("file.separator") + "wordList.txt");
file.separator
Character that separates components of a file path. This is "/" on UNIX and "\" on Windows.
Can you rename the file? This can help if you copied the file name from another location and it had non-visible characters in it.
Normally when using ...
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(source,result);
(without the xmlparserv2.jar file) a File Not Found Exception looks like this.
Exception in thread "main" java.io.FileNotFoundException: C:\Documents and Settings\username\nonExistentFile.xml (The system cannot find the file specified)
when you include the xmlparserv2.jar, the Exception turns to this
Caused by: java.io.FileNotFoundException: C:\Documents%20and%20Settings\username\existingFile.xml (The system cannot find the path specified)
The file is actually there (the transform method finds it when i dont include the jar) but when i include the jar, the transform method cant find it due to the %20 that is inserted for whitespace. Can someone please tell me how to fix this?
Sorry, should have included more code... the answer was in the Result Object i was passing in.
originally it looked like this
File f = new File(filePath);
Result result = new StreamResult(f);
changed to this..
File f = new File(filePath);
StreamResult result = new StreamResult();
FileOutputStream fos = new FileOutputStream(f);
result.setOutputStream(fos);
I'm sure I'm missing something basic here.
I'm trying to create a new file on my drive, but I'm getting an error:
Exception in thread "main" java.io.FileNotFoundException: C:\ProgramData\msena\test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at net.meosoft.relatetoit.core.HibernateSessionFactory.main(HibernateSessionFactory.java:89)
My code at the moment is:
final File file = new File("C:\\ProgramData\\uname2\\test.txt");
final BufferedReader in = new BufferedReader(new FileReader(file));
while(in.ready()) {
System.out.println(in.readLine());
}
in.close();
What's wrong at the moment? I want to just read, even if it's there (so file should be made).
Java doesn't automatically check that File() exists, nor will it automatically create it if you ask it.
You'll need to do one of the following:
Add in a check for the file's existence: if(file.exists()) { ... }.
Add in a check, similar to above, but then if it doesn't exist, call: file.createNewFile();. This will make a new file on the file system for you to use.
If that still doesn't work, I'd check you have write permissions to that directory. :)
The File class represents the path to a file, not the file itself. If the file does not exist (!File.exists()), an exception will be thrown when you try to access it. Make sure the path to the file is correct and that you have permission to read from that location.
If you want to create the file, you can use File.createNewFile().
this is the method to create file.
Formatter output;//pointer to an object that will write to a file
public void createFile(){
try{
output = new Formatter("C:\\ProgramData\\uname2\\test.txt");
//test.txt is the name of the file to be created
//create file in the same folder called test.txt
//if existed overwrite it
}
catch(FileNotFoundException e){
System.err.println("Error creating file"+e.getMessage());
}
call createFile() in the main
CreateTextFile file = new CreateTextFile();
file.createFile();
Check your file name. It should not contain any colon.