Chetan Kinger
Chetan Kinger

Reputation: 15212

How to insert platform specific paths into a database in Java

I am working on an application that processes a large number of files placed in a directory. I want to insert the fully qualified path of each file that is processed by my application into the database. If a file that is already processed by the application is re-submitted for re-processing by the user, then I want to delete all the data processed for the file from the database by specifying the fully qualified path of the file and then proceed with the file as if it were a new file.

The problem that I am facing is that when I run the application through eclipse, the path name inserted into the database is as follows:-

   D:\Trunk\App\Source

Afer I concatenate the file name with its path (pathString + File.seperator + fileNameString) i.e, the fully qualified path inserted into the database is as follows

   D:\Trunk\App\Source/file1.txt

But if I create a jar for my application and run the jar by double clicking on the jar, the path name inserted into the database is as follows:-

   D:/Trunk/App/Source

After I concatenate the file name with its path (pathString + File.seperator + fileNameString) i.e, the fully qualified path inserted into the database is as follows

  D:/Trunk/App/Source/file1.txt  

The application fails to delete the data for a file that is resubmitted for processing if the application is run by double clicking on the jar instead of running the application from eclipse. The query used for deleting is the same regardless of running the application from eclipse or by double clicking the jar

    delete from file_table where where file = 'D:\Trunk\App\Source/file1.txt'

Upvotes: 2

Views: 236

Answers (2)

Andrzej Jozwik
Andrzej Jozwik

Reputation: 14649

Convert path to URI and store in database. Then recreate file from URI.

String uriName = new File(path).toURI().toString()

String path = new File(new URI(uriName)).getAbsolutePath();

Upvotes: 1

Drew
Drew

Reputation: 426

Just convert all "\" to "/" or vice versa for all file paths that you process.

Upvotes: 2

Related Questions