Java read files from folder

Here is Java Class for reading files from specific folder on the server. This class is part of my file manager library which will be finished very soon. When I finished file manager library I will upload it on the bitbucket and library will be opensource.

Some features of file manager library:

  • read files
  • read files and folder
  • read files and folder from sub folders(recursively)
  • upload file
  • upload multiple files
  • progress listener for upload
  • html5 interface
  • file manager for spring framework
  • file manager for Java EE6 & 7
  • implementation with all popular editors

How to use a class:


//create new instance
 FileManager fm = new FileManager();

 //set file filter, which indicate what kind of files we want read,
 //if set empty array new String[]{} that means that class will read all types of files
 fm.setExtensionFilter(new String[]{"jpg", "png"});
 //set path of your root folder
 fm.setPath("C:\\Users\\PC\\Pictures\\Google Talk Received Images");
 //read sub directories true, if you dont want read sub directories set false
 fm.setReadSubDirectory(false);
 //init object to read files
 fm.read();
 //return all files and all files properties into json format
 //this is very useful for web and JQuery
 System.out.println(fm.getGson());

 

Here is class. I must point out that class is in early development, when I completed all class will be further optimized.


package com.i_do2.file;

import com.google.gson.Gson;
import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class FileManager implements FilenameFilter {

 private String path;
 private String apsolutePath;
 private int filesSize;
 private String name;
 private long size;
 private boolean directory;
 private String parentPath;
 private String parentApsolutePath;
 private boolean readSubDirectory;
 private String formatDate;
 private String date;
 private List<FileManager> list;

 private String[] ext;

 public FileManager() {

 filesSize = 0;
 directory = false;
 name = new String();
 size = 0;
 filesSize = 0;
 path = new String();
 apsolutePath = new String();
 parentPath = new String();
 parentApsolutePath = new String();
 list = new ArrayList<>();
 readSubDirectory = false;
 ext = new String[]{};
 date = new String();
 formatDate = "dd.mmmm.yyyy";
 }

 public void read() {

 File file = new File(path);
 size = file.length();
 parentPath = file.getParent();
 apsolutePath = file.getAbsolutePath();
 name = file.getName();
 directory = file.isDirectory();
 date = new SimpleDateFormat("dd.mmmm.yyyy").format(file.lastModified());

 if(file.isDirectory() && readSubDirectory) {

 File[] files = file.listFiles();
 for(File f : files) {

 try {


 FileManager fm = new FileManager();
 fm.setFormatDate(formatDate);
 fm.setExtensionFilter(ext);
 fm.setPath(f.getPath());
 fm.setReadSubDirectory(readSubDirectory);
 fm.read();
 list.add(fm);
 fm = null;
 f = null;

 }catch(NullPointerException ex) {

 //
 }
 }

 files = null;
 }
 else {

 File[] files = file.listFiles();
 for(File f : files) {

 try {


 if(!this.accept(f, f.getName())) continue;
 FileManager fm = new FileManager();
 fm.setFormatDate(formatDate);
 fm.parseFile(f);
 list.add(fm);

 fm = null;
 f = null;

 }catch(NullPointerException ex) {
 //System.out.println(ex.getMessage());
 }
 }

 files = null;
 }

 file = null;
 }

 public void parseFile(File file) {

 size = file.length();
 parentPath = file.getParent();
 apsolutePath = file.getAbsolutePath();
 name = file.getName();
 directory = file.isDirectory();
 file = null;
 }

public String getPath() {
 return path;
 }

public void setPath(String path) {
 this.path = path;
 }

public String getApsolutePath() {
 return apsolutePath;
 }

 public int getFilesSize() {
 return filesSize;
 }

public String getName() {
 return name;
 }

public long getSize() {
 return size;
 }

public boolean isDirectory() {
 return directory;
 }

public String getParentPath() {
 return parentPath;
 }

public String getParentApsolutePath() {
 return parentApsolutePath;
 }

public void setReadSubDirectory(boolean readSubDirectory) {
 this.readSubDirectory = readSubDirectory;
 }

public List<FileManager> getList() {
 return list;
 }

 public String getGson() {

 Gson gson = new Gson();
 return gson.toJson(this);
 }

public String[] getExtensionFilter() {
 return ext;
 }

public void setExtensionFilter(String[] ext) {
 this.ext = ext;
 }

public String getFormatDate() {
 return formatDate;
 }

public void setFormatDate(String formatDate) {
 this.formatDate = formatDate;
 }

@Override
 public boolean accept(File dir, String fname) {

 if(dir.isDirectory()) return true;
 if(ext.length == 0) return true;
 for(String extension : ext) {

 if(fname.toLowerCase().endsWith(extension.toLowerCase())) {
 return true;
 }
 }
 return false;
 }

 public void clean() {

 ext = null;

 for(FileManager f : list) {

 f.clean();
 f = null;
 }

 list.clear();
 list = null;
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s