2016-04-15 15 views
0

こんにちは皆私はjavaを初めて使用しています。なぜファイルが見つからないのかという問題が発生しています。私は、単にjfilechooserを使ってファイルを開き、ONE LINEテキストをオプションペインメッセージに出力するようにしています。何が間違っているか、何を修正する必要があるかに関するアイデア?ありがとうございますFileNotFound例外エラー

package synchro; 

import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import javax.swing.JFileChooser; 
import javax.swing.JOptionPane; 

public class SynchroTest { 
    public static void main(String[] args){ 
    JFileChooser fileChooser = new JFileChooser(); 
     int returnValue = fileChooser.showOpenDialog(null); 
     if (returnValue == JFileChooser.APPROVE_OPTION) { 
      File selectedFile = fileChooser.getSelectedFile(); 
      System.out.println(selectedFile.getName()); 

     try{ 
     Scanner input = new Scanner(new File(selectedFile.getName())); 
     while(input.hasNext()){ 
     String line = input.nextLine(); 
     JOptionPane.showMessageDialog(null, "Input sentence:\n" + line); 
     } 

     }catch(FileNotFoundException e){ 
      System.out.println("File Not Found"); 
     } 
    } 
} 
} 
+0

なぜあなたはちょうど使用いけない - 'スキャナ入力は=新しいスキャナ(selectedFile);' –

+0

私はエラーを取得します。 –

+0

そのエラーは何ですか? –

答えて

1

私はなぜあなたがループを回っているのか分かりません。

File selectedFile = fileChooser.getSelectedFile();は、クリックされたファイルに対してFileオブジェクトを提供します。

単にScanner input = new Scanner(selectedFile);を使用すると、ファイルを解析することができます。

while(input.hasNext()) { 
    String line = input.nextLine(); 
    System.out.println(line); 
} 
関連する問題