HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Java Save/Load Optimization

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
saveoptimizationjavaload

Problem

I have recently gotten into Java in a Computer Science class at my high school and I am trying to learn more than just the basics I have learned in school. Yesterday, I designed a very simple text editor I named Aqua that is written with Swing. For some reason, my computer drags a little bit when I run these methods. Is it because I have a crappy computer or did I write something wrong? Thanks!

private void save(String content, String name) throws IOException{
    System.out.println(dir.toString());
    if(firstRun<1){
        dirCreation();
        firstRun++;
    }
    try{
            String savedText = content;
            System.out.println(savedText);
            File newTextFile = new File(newDir.toString() + File + name + ".aqua");
            System.out.println(newDir.toString() + File.seperator + name + ".aqua");
            if (!newTextFile.exists()) {
                System.out.println("Created new File");
                newTextFile.createNewFile();
            }
        try (FileWriter fw = new FileWriter(newTextFile)) {
            fw.write(savedText);
        }

    }
    catch(IOException x){
                System.err.format("IOException: %s%n", x);
    }
}
private void load(String name) throws FileNotFoundException, IOException{
    if(firstRun<1){
        dirCreation();
        i++;
    }

    File loadingFile = new File(newDir + "\\" + name + ".aqua");
    Scanner scan = new Scanner(loadingFile);
    StringBuilder loadedText = new StringBuilder("");
    while (scan.hasNextLine()) {
            loadedText.append(scan.nextLine() + "\n");
            //Is this correct usage of StringBuilder?
    }
    jTextArea1.setText(out);        
    }

Solution

There are formatting and whitespace issues in your code, fix them! You're also switching between declaring variables and initializing them in the next line and initializing them in the same line, be consistent!

if(i<1){


That hurts! What is i? Where does it come from? Why do you check against less then one? Keep your variable names always clear, f.e. numberOfLinesInFile is way better then i in my opinion.

new File(newDir.toString() + "\\" + name + ".aqua");


This assumes that it is run on a Windows machine (or anything else that uses a backslash as path separator...which I hope no one else does). That will lead lead to problems on *nix machines, but I know that / works on any platform, you could also go full length and use File.separator.

Or you use the constructor-overload that is provided for exactly these means:

new File(newDir.toString(), name + ".aqua"):


String out = "";


That's not a great name for that variable, text or content would be better suited.

out+=line + "\n";


This has one problem, concatenating strings with the + is slow like whatever-place-of-ternal-pain-you-believe-in-if-any. Use a StringBuilder, that will speed things up considerable.

jTextArea1.setText(out);


Again, not a good name.

Code Snippets

new File(newDir.toString() + "\\" + name + ".aqua");
new File(newDir.toString(), name + ".aqua"):
String out = "";
out+=line + "\n";
jTextArea1.setText(out);

Context

StackExchange Code Review Q#27216, answer score: 6

Revisions (0)

No revisions yet.