4
\$\begingroup\$

I am currently writing a small configuration API for myself, i want to use it to easily read/write to a file, however, i want to make sure it's efficient and get some other options into the field of my programming. How can i make my classes even more efficient?

This is the class for the options for comments, headers and such

package config.options;

public class ConfigOptions {

/**
 * This is pretty much it for now.
 */

private boolean appendable = false;
private boolean encryptable = false;
private char separator = '=';
private String header = "[%header%]";
private String ignoreLine = "#";


public boolean isEncryptable() {
    return encryptable;
}


public void setEncryptable(boolean encryptable) {
    this.encryptable = encryptable;
}


public char getSeparator() {
    return separator;
}


public void setSeparator(char separator) {
    this.separator = separator;
}


public boolean isAppendable() {
    return appendable;
}


public void setAppendable(boolean appendable) {
    this.appendable = appendable;
}


public String getHeader() {
    return header;
}


public void setHeader(String header) {
    this.header = header;
}


public String getIgnoreLine() {
    return ignoreLine;
}


public void setIgnoreLine(String ignoreLine) {
    this.ignoreLine = ignoreLine;
}

}

This is my class for writing to the file

package config.write;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import config.options.ConfigOptions;

public class ConfigWriter {

File file;
BufferedWriter writer;
private String URL;
private ConfigOptions options;


public ConfigWriter(String URL, ConfigOptions options, String filename) {
    this.URL = URL;
    this.options = options;

    File dir = new File(URL);
    if(!dir.exists()) {
        dir.mkdir();
    }

    file = new File(dir, filename);
    if(!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    try(BufferedWriter writer = new BufferedWriter(new FileWriter(file, options.isAppendable()))) {
        this.writer = writer;
    } catch (IOException e) {

        e.printStackTrace();
    }

}


public void writeAll(String[] configobjects, String[] values) {
    if(file.exists() && file.canWrite()) {
        try {
            for(int i = 0; i < configobjects.length; i++) {
                writer.write(configobjects[i] + options.getSeparator() + values[i]);
            }
            writer.newLine();
            writer.flush();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}

public void writeType(String configobject, Object value)  {

    if(file.exists() && file.canWrite()) {
        try {
            writer.write(configobject + options.getSeparator() + String.valueOf(value));
            writer.newLine();
            writer.flush();
        } catch (NumberFormatException | IOException e) {
            System.out.print(e.getCause());
            e.printStackTrace();
        }

    } else {
        System.out.println("Cannot write to the file, either it does not exist, or you cannot write to it.");
    }
}


public boolean setType(String stringobject, Object newvalue) {

    return false;
}

public boolean remove(String stringobject) {

    return false;
}   



public String getURL() {
    return URL;
}

public void setURL(String uRL) {
    URL = uRL;
}   

}

This is my class for reading from the file.

package config.read;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import config.options.ConfigOptions;

public class ConfigReader implements ConfigReadingRequirements {

File file;
BufferedReader reader;
private boolean readable;
private ConfigOptions options;

public ConfigReader(String URL, ConfigOptions options) {
    file = new File(URL);

    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        this.reader = reader;
        if (!file.canRead()) {
            this.readable = false;
            System.out.println("Cannot read the file, are you sure you have the correct URL?");
        } else {
            this.readable = true;
            this.options = options;
        }

        if (options.getSeparator() == -1) {
            options.setSeparator('=');
        }

    } catch (IOException e) {
        System.out.println(e.getCause());
        e.printStackTrace();
    }

}

public String[] readAll() {
    ArrayList<String> data = new ArrayList<String>();
    try {
        while (reader.readLine() != null) {
            if (!reader.readLine().startsWith("#")) {
                data.add(reader.readLine());
            }
        }
        if (!data.isEmpty()) {
            String[] dat = new String[data.size()];
            for (int i = 0; i < data.size(); i++) {
                dat[i] = data.get(i);
            }
            return dat;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public Object readType(String configobject) {
    if (isReadable()) {
        String str;
        try {
            while ((str = reader.readLine()) != null) {
                if (!str.startsWith(options.getIgnoreLine())) {
                    if (str.split(String.valueOf(options.getSeparator()))[0].equalsIgnoreCase(configobject)) {
                        str = splitToMatch(str);
                        return String.valueOf(str);
                    }
                }
            }
        } catch (NumberFormatException | IOException e) {
            System.out.print(e.getCause());
            e.printStackTrace();
        }
    } else {

        System.out.println("Cannot read the file, are you sure you have the correct URL?");
    }
    return null;
}

private String splitToMatch(String splittable) {
    return String.valueOf(splittable.split(String.valueOf(options.getSeparator()))[1]);
}

public boolean isReadable() {
    return readable;
}

public BufferedReader getReader() {
    return reader;
}

public void setReader(BufferedReader reader) {
    this.reader = reader;
}

}

The interfaces they implement are simply methods that every class needs to have, like the writer needs a writeType method which has an object as argument, a way to remove text from the file and a way to change the value of a key.

For the reader it's simply a readType that returns an object.

How can i improve my code? What would you suggest?

\$\endgroup\$
1
  • \$\begingroup\$ If you don't do this for educational purpose (and are willing to skip the header), just use java.util.Properties \$\endgroup\$ Dec 14 '16 at 12:30
1
\$\begingroup\$

You should probably make your ConfigOptions class serializeable, just in case.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your input, i'll be sure to read about it and implement it \$\endgroup\$
    – pandaJuan
    Dec 15 '16 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.