ca.live.hk12.numera.server.database.FileService.java Source code

Java tutorial

Introduction

Here is the source code for ca.live.hk12.numera.server.database.FileService.java

Source

/**
 * You should have received a copy of the GNU General Public License version 3
 * along with this work; Please find the Copyright information and Terms and
 * Conditions in the ClientLauncher.java or ServerLauncher.java file.
 */

package ca.live.hk12.numera.server.database;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import ca.live.hk12.numera.server.service.ServerException;

enum FileService {

    Sectors("Sectors/", ".sector"), Users("Users/", ".user");

    private String directory;
    private String fileExtension;

    private static final String PARENT_DIR = "data/";

    FileService(String dir, String fileExtension) {
        this.directory = dir;
        this.fileExtension = fileExtension;

        makeDirectory(PARENT_DIR + dir);
    }

    public String getDirectory() {
        return PARENT_DIR + this.directory;
    }

    public String getFile(String fileName) {
        return getDirectory() + fileName + fileExtension;
    }

    public ArrayList<String> getFiles(boolean includeExtension) {
        ArrayList<String> files = new ArrayList<String>();

        File folder = new File(getDirectory());
        File[] listOfFiles = folder.listFiles();

        for (File file : listOfFiles) {
            if (file.isFile()) {
                String name = file.getName();
                if (name.substring(name.indexOf('.')).equals(fileExtension)) {
                    if (includeExtension)
                        // keep extension
                        files.add(name);
                    else {
                        // remove extension
                        files.add(name.substring(0, file.getName().indexOf('.')));
                    }
                }
            }
        }

        return files;
    }

    private static void makeDirectory(String dir) {
        File file = new File(dir);
        try {
            file.mkdirs();
        } catch (Exception e) {
            throw new ServerException("Unable to make directory: " + dir);
        }
    }

    public static boolean renameFile(File oldFile, File newFile) {
        try {
            Files.copy(oldFile.toPath(), newFile.toPath());
        } catch (IOException e) {
            new ServerException("Failed to renameFile: " + oldFile.getName());
            return false;
        }
        return true;
    }

    public static <T> T readData(String fileLocation, Class<T> dataClass) {
        T t = null;
        try (Reader reader = new InputStreamReader(new FileInputStream(fileLocation), "UTF-8")) {
            Gson gson = new GsonBuilder().create();
            t = gson.fromJson(reader, dataClass);
            reader.close();
        } catch (Exception e) {
            return t;
        }
        return t;
    }

    public static <T> boolean writeData(String fileLocation, T t) {
        try (Writer writer = new OutputStreamWriter(new FileOutputStream(fileLocation), "UTF-8")) {
            Gson gson = new GsonBuilder().create();
            gson.toJson(t, writer);
            writer.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServerException("Could not write to:<br/>" + fileLocation);
        }
    }

}