ENOENT (No such file or directory) on database file - java

java.io.IOException: open failed: ENOENT (No such file or directory)
This happens on this line of code:
File db = getDatabasePath("questions");
db.createNewFile();//HERE
I am trying to create a database file with the name questions.db , which will be empty. I will then pull the file from the server and insert it there. I then want to be able to access the database from sqlite.

You did not show the stack trace, it can help to pinpoint it. But since the File#createNewFile() is not doing much, the only possible reason for this to fail is, that the (relative) path includes a non-existing parent directory. createNewFile() will not create those.
You typically write
File db = ...
db.getParentFile().mkdirs();
db.createNewFile();
to fix this. (Or use a different - aka existing - location).

Related

How can I connect to read only H2 database file which is inside runnable jar?

I've requirement to create database once with create/insert statements and then make it available inside javafx runnable jar with read only DB access.
I just gone through H2 database document many time and tried to achieve this using H2 Driver version 1.4.196 and 1.4.192 but I think I'm missing something.
I can connect without including it into runnable jar and read only things works as well. But I need to access it from jar itself to prevent DB access from outsiders.
Can I've connection string or logic to connect read only DB file from jar itself ?
Ofcourse I need more clear practical example from this, this and this.
I've implemented zipping logic as this and it works fine from given path.
But when I've added zip file in runnable jar and accessing it through following code:
DriverManager.getConnection("jdbc:h2:file:split:zip:./test.zip!/test");
Then it throws exception with:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.FileNotFoundException: .\jar\res\buzdirectory.zip (The system cannot find the path specified)"; "listFiles zip:./jar/res/buzdirectory.zip!/" [90031-196]
If I put fully qualified path then it works well:
DriverManager.getConnection("jdbc:h2:file:split:zip:C:\\CoreJava\\src\\main\\java\\res\\test.zip!/test");
What I'm missing here to access it from jar file.
I think we cannot open the database file directly from the runnable JAR file. To access it should be a "real" file, so we will need to extract it from the JAR and then open that copy.
For example, to extract the database from the JAR into a temporary file:
File dbFile = File.createTempFile("temp", ".zip");
dbFile.deleteOnExit();
java.nio.file.Files.copy(this.getClass().getResourceAsStream("/db/temp.zip"),
dbFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
StringBuffer connStr = new StringBuffer("jdbc:h2:file:split:zip:")
.append(dbFile.getAbsolutePath()).append("!/temp");
Connection conn = DriverManager.getConnection(connStr.toString());
conn.close();
System.out.println("closed");
By the way this is not preferable approach yet all. But we can use this kind of approach in some special cases.

Create a CSV file with Java is impossible (Access Denied)

I'm trying to create a CSV file in a Java program. No matter where the file is located, I've this error:
Exception in thread "main" java.io.FileNotFoundException: <location> (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
I have the same error for the .PDF and .DOC files.
I have no error with .TXT, .JSON ......
I have the administator rights.
I think it's Microsoft Office which protect .DOC and .CSV files.
When you call new File your are supposed to give the file name, not only the directory you want to put your file in.
Try with something like
File file = new File("D:/Data/" + item.getFileName());
Try printing the fileObj.getAbsolutePath(); to see if the file object is referring to the correct location/file.
If it is correct, then the problem is with access.
Remember, you may see the Access Denied error even if the file already exists and is being used by another program.
You should consider posting some code so that we can review the code blocks to identify a solution.

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)

read a file from within app folder in android

I want to read a file that is saved in my java folder (same where my activities are) in package com.example so it's java-->com.example--> data.ser. However I'm getting java.io.FileNotFoundException: /java/com/example/data.ser: open failed: ENOENT (No such file or directory) when I call
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("java/com/example/data.ser")));. I tried removing the java part or just leaving the file name on its own but nothing works. I am not loading it from external/internal storage but just from within the app. How to do it? I'm using android studio. Thanks for help
Files has to be stored on 'assets' or 'raw' folder, depending on what kind of file you are going to store. On 'java' you should set code class only. You can find an easy example here:
raw file example

context.fileOpenInput(...) - file does not exist...but it does

I am probably doing something stupid here but my app cannot read data from the external storage. But i can write files just fine. In fact, i pull the files from an FTP server and write them to external storage which works fine. But as soon as I try to read them with:
getApplicationContext().openFileInput(files[i]);
I get java.io.FileNotFoundException: /data/data/co.za.infestation.opendesign.app/files/event1.txt: open failed: ENOENT (No such file or directory)
Which is weird because I know the files are there. And i know files[i] has the correct file name because I printed them before after:
String[] files = getExternalFilesDir(null).list();
Yes I did add the permissions in the manifest file... And yes, i did hardcode the file names without success..Maybe i need more coffee.
Thanks!
openFileInput()
is for opening files which reside in private internal memory. You can see the error path:
/data/data/co.za.infestation.............
Try to open with a
FileInputStream()
instead.

Categories

Resources