2017-01-30 8 views
1

.txtファイルを読み込んで単語を検索しようとしていますが、プログラムはただちにProcess finished with exit code 0で終了します。Javaがテキストファイルを読み取れない

import java.io.*; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class LogParser { 
    static Scanner file; 
    static ArrayList text = new ArrayList(); 
    static String path = new String(); 
    static String check = new String(); 
    private static int a = 0; 
    static Scanner inpunt = new Scanner(System.in); 


    public static void main (String[] args) { 

     System.out.println("Input path to file"); 
     path = inpunt.nextLine(); 
     File texts = new File(path); 

     try { 
      file = new Scanner(new File(path)); 
     } catch (Exception e) { 
      System.out.println("Can't open file"); 
     } 

     try { 
      while (file.hasNext()) { 
       text.add(a, file.nextLine()); 
       check = text.get(a).toString(); 
       if (check.contains("cap")) 
        System.out.println("Allert!!!!!!!!!!!!!!!!!!!!!!!!!!!" + text.get(a)); 
       a = a + 1; 

      } 
     } catch (Exception e) { 
      // System.out.println("Can't open file"); 
      if (file.toString().contains("cap")) 
       System.out.println("cap" + "Path to file: " + path); 
      System.out.println(text.size()); 
     } 

    } 

} 

.txtファイル内のテキストは次のとおりです。

let's try read this cap 

私はxmlファイルを開こうとすると、すべてがOKです。私の問題はtxtファイルのみです。

+1

あなたは自分自身について少し説明してください。 – Chisko

+1

'path'がどこに設定されているかは不明です。問題を再現しやすい方法でコードを修正してください。あなたが経験しているエラー/例外は何ですか? – alfasin

+1

パスが設定されていません。 –

答えて

2

コメントに記載されているとおり、パス変数は設定されていません。新しいファイルを作成し、インスタンス化されていないパスを渡そうとしています。

関連する問題