2013-06-10 5 views
-1

최근에 고등학교에서 Computer Science 수업을 통해 Java에 입문했으며 학교에서 배운 기초 이상을 배우려고합니다. 어제 저는 Swing으로 작성된 Aqua라는 매우 간단한 텍스트 편집기를 디자인했습니다. 웬일인지, 나는이 방법을 실행할 때 컴퓨터가 조금씩 끌린다. 내가 진절머리 나는 컴퓨터를 가지고 있거나 내가 잘못한 것을 작성했기 때문입니까? 감사!자바 스윙 성능

private void save(String content, String name) throws IOException{ 
    System.out.println(dir.toString()); 
    if(i<1){ 
     dirCreation(); 
     i++; 
    } 
    try{ 
      String savedText; 
      savedText = content; 
    System.out.println(savedText); 
      File newTextFile; 
      newTextFile = new File(newDir.toString() + "\\" + name + ".aqua"); 
      System.out.println(newDir.toString() + "\\" + 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(i<1){ 
     dirCreation(); 
     i++; 
    } 

    File loadingFile; 
    loadingFile = new File(newDir + "\\" + name + ".aqua"); 
    Scanner scan = new Scanner(loadingFile); 
    String out = ""; 
    while (scan.hasNextLine()) { 
      String line = scan.nextLine(); 
      out+=line + "\n"; 
    } 
    jTextArea1.setText(out);   
    } 
+0

죄송합니다. 그러나 이것은 코드 리뷰 서비스가 아닙니다. 가지고 계신 분이라면 구체적인 질문을하십시오. – sashkello

+3

대신 http://codereview.stackexchange.com/에 게시하십시오. – mshsayem

+0

@ bigfoot675 지금 'code review' (으)로 이동하십시오! –

답변

2
out+=line + "\n"; 

일반적으로 좋은 생각이 String를 구축 할 문자열 연결을 사용합니다. 두 개 이상의 String을 연결하려는 경우 StringBuilder를 사용해야합니다.

이 경우에도이 작업을 수행 할 필요가 없습니다. 대신의 setText() 메소드를 하나의 큰 문자열을 생성하고 사용하는 당신은 사용할 수 있습니다

jTextArea1.append(...) 

당신이 텍스트 영역에 표시 할 각 문자열.

그러나, 심지어 더 좋은 방법은 사용하는 것입니다

JTextArea.read(...) 

이 API는 하나 개의 문장으로 당신을 위해 I/O를 수행합니다.

텍스트 저장시 동일합니다. JTextArea API에는 쓰기 (...) 메소드가 있습니다.

+0

루프에서 벗어난 경우 문제가되지 않습니다. 그런데 핫스팟 최적화 프로그램은 최적화 될 때 StringBuilders (또는 이와 비슷한 것)를 사용합니다. –