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);
}
+orconcatin a loop to build up a string. Use StringBuilder instead. \$\endgroup\$StringBuilderto perform string concatenation, so it does not really matter \$\endgroup\$StringBufferbut it is converted back toStringafter each line. When usingStringBufferdedicated you only have one conversion at the end of all operations. ButStringBuilderhas the avantage that it is not synchronied. \$\endgroup\$