2013-01-24 2 views
2

에서 그것을 보여주고 내 텍스트 파일의.txt 파일의 내용을 읽고 내가 .txt 파일의 내용을 읽으려고하고 여러 텍스트 영역

내용은 내 GUI에 JTextAreas의 숫자를 표시 할 쉼표로 8 개 임의의 숫자

2001403003030707020

(다음과 같이) 서로를 분리 내 GUI 8 JTextArea에 있고 나는 다른 JTextArea에 각 번호를 표시 할 .

그래서 어떻게 쉼표 (,)를 텍스트 파일에서 구분 기호로 사용할 수 있습니까?

다음 코드는 파일을 완벽하게 열지 만 선택한 .txt 파일의 내용을 하나의 텍스트 영역에만 표시하고 있습니다. 목표를 달성하기 위해 코드를 편집하려면 어떻게해야합니까?

b2.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent arg0) { 

      JFileChooser fc = new JFileChooser(); 
      int returnVal = fc.showOpenDialog(fc); 
      if (returnVal == JFileChooser.APPROVE_OPTION) 
      { 
        File file = fc.getSelectedFile(); 
        try 
        { 
        FileReader fr = new FileReader(file); 
        o = new BufferedReader(fr); 
        while((s=o.readLine())!=null) 
         t1.setText(s); 
        } 
        catch (FileNotFoundException e) 
        { 
        e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
        e.printStackTrace(); 
        }     
      } 
     } 
    }); 
+2

의''finally'에서 fr.close()'를 호출하는 것을 잊지 마세요 -블록. – MrSmith42

+0

@ MrSmith42 상기시켜 주셔서 감사합니다! –

답변

4

","을 구분 기호로 지정하여 텍스트 파일의 내용을 토큰 화해야합니다.

String content = "200, 140, 300, 30, 30, 70, 70, 20; 
String[] tokens = content.split(", "); 

그런 다음 토큰 배열의 각 번호에 액세스 할 수 있습니다.

+0

일했습니다! 감사 –

2

당신은

는 시도 (",") s.split를 사용하여이 숫자를 나눌 수있는이

 FileReader fr = new FileReader(file); 
     BufferedReader o = new BufferedReader(fr); 
     String s; 
     while ((s = o.readLine()) != null) { 
      String Values[] = s.split(","); 
      for (int i = 0; i < Values.length; i++) { 
       System.out.println(Values[i]);//////////here You can set JTextArea by using Values[i] 

      } 
     } 
관련 문제