0

i am writing a java application (actually a Struts2 web application) and i need to store and retrieve images. i found out that it is not a good idea to store images in a database (Can I store images in MySQL) and that it is preferable to write them on disk, in a directory.

at some point i need to access all the images whose name contains a certain substring. if i used a database, i would write a query like this: select .. where name like %my_substring%. how can i achieve the same functionality using the java file system? certainly, i don't want to load all the images, and then iterate them to find those with proper name.

Community
  • 1
  • 1
L Petre
  • 1,027
  • 8
  • 12

3 Answers3

1

Just handle it with an Index File. Everytime you Store a image on the disc, its Name is also written in the Index File. When you search for a substring just search the Index and load the File with the Name in the Index

mormod
  • 15
  • 1
  • 5
  • could you please be more specific about what kind of Index File should i use? so far, i couldn't find any IndexFile class in the core java api, but i found one in the apache lucene library. is this the IndexFile you meant? – L Petre Jun 02 '15 at 08:16
  • Just create a .txt File on your disc and write the names of the imgs in it. If you search for a substring in a img title just seach the Index File – mormod Jun 02 '15 at 08:45
  • I don't think There are any librarys to handle Index files – mormod Jun 02 '15 at 08:46
1

As the marked answer in the question you linked states:

Do not store images in the database. Store images in directories and store references to the images in the database.

Thus, your query would become select path where name like %my_substring% (where path is the name of the column where the reference to the image is stored).

Once that the query is executed, your application will access the images as per the result of the query.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

i finally chose the solution without databases (although the solution provided by @npinti is really smart). so i did something like that

File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    if(listOfFiles[i].getName().contains(my_substring)){
     //add the file to a list of files to be displayed 

}}}

L Petre
  • 1,027
  • 8
  • 12