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);
}
+
orconcat
in a loop to build up a string. Use StringBuilder instead. \$\endgroup\$StringBuilder
to perform string concatenation, so it does not really matter \$\endgroup\$StringBuffer
but it is converted back toString
after each line. When usingStringBuffer
dedicated you only have one conversion at the end of all operations. ButStringBuilder
has the avantage that it is not synchronied. \$\endgroup\$