1

私は多元的な配列に未加工のリソーステキストファイルを読む方法を教えようとしています。生のテキストファイルは次のとおりです。FileReaderでraw txtファイルを参照するには?

ken todd 
stuart brian 
bob stewart 
orange apple 

green red 

コードは以下のとおりです。

public void LoadFromFile(){ 

     // int fileId = (getResources().getIdentifier("text_file", 
     //  "raw", getPackageName())); 
     Scanner scan = new Scanner(new FileReader(R.raw.text_file); 
     // initialises the scanner to read the text_file 

     String[][] entries = new String[6][2]; 
     // creates a 2d array with 6 rows and 2 columns. 

     int i = 0; 
     while (scan.hasNextLine()) { 
      entries[i] = scan.nextLine().split("\\s+"); 
      i++; 
     } 
     //loops through the file and splits on a space 

     for (int row = 0; row < entries.length; row++) { 
      for (int col = 0; col < entries[0].length; col++) { 
       if (entries[row][col] != null) { 
        System.out.print(entries[row][col] + " "); 
       } 
      } 
      if (entries[row][0] != null) { 
       System.out.print("\n"); 
      } 
     } 
     //prints the contents of the array that are not "null" 
    } 

スキャナのラインにエラーが発生しました。具体的にFileReaderの(R.raw.text_file)私は明らかにFileReaderのがintを探していると私は、ファイルIDを入れていることを実行しようとしました

'FileReaderの(int型)'

がコンストラクタを解決できません取得しますintをint型変数に変換してから、FileReaderでint型変数を使用しても、どちらも機能しませんでした。

int fileId = (getResources().getIdentifier("text_file", 
     "raw", getPackageName())); 
Scanner scan = new Scanner(new FileReader(fileId); 

それは私に

を同じエラーを与えたコンストラクタ 'FileReaderの(int型)' を解決できません

私はここで間違って何をしているのですか?誰もがファイルを配列に入れる方法のヒントを得ましたか?

乾杯

エアフィックス

答えて

1

Doug Stevensonの指導のお陰で、ファイルを配列に読み込むためのコードを正しく取得できました。私は、配列をハード・コーディングではなくテキスト・ファイルの正しいサイズにし、テキスト・ファイルの空白行を扱うなど、整理する必要がある他のいくつかの詳細を持っています。ここでの平均時間で

が配列にテキストファイルをロードするための作業コードです:

public void TextFileReader(){ 

     String[][] textFileData = new String[5][2]; 

     InputStreamReader InputSR = null; 
     BufferedReader BufferedRdr = null; 
     String thisLine = null; 

     InputSR = new InputStreamReader(getResources().openRawResource(R.raw.text_file)); 
     BufferedRdr = new BufferedReader(InputSR); 

     try { 
      // open input stream text_file for reading purpose. 
      int i = 0; 
      while ((thisLine = BufferedRdr.readLine()) != null) { 
       String[] parts = thisLine.split(" "); 
       textFileData[i][0] = parts[0]; 
       textFileData[i][1] = parts[1]; 
       Log.v("String Array"+i+"0", String.valueOf(textFileData[i][0])); 
       Log.v("String Array"+i+"1", String.valueOf(textFileData[i][1])); 
       i = i +1; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
    } 
0

リソースが開発マシン上のファイルです。 Androidデバイスのファイルではありません。 FileReaderを使用してアクセスすることはできません。 getResources().openRawResource(R.raw.text_file)Contextで呼び出されます)を使用して、リソース上にInputStreamを取得してください。

2

生のリソースはopenRawResource(int)で読み取ることができます。それはInputStreamReaderでラップすることができるInputStreamを与え、次にBufferedReaderでラップします。そのBufferedReader上でreadLine()を使用して行を取得することができます。

+0

ダグ、私はあなたの助けを用いて溶液を得るために近づいています。ここの私の要点を見てください。 https://gist.github.com/Airfixed/799e784696b0a60c5423d347bf33a341しかし、txtファイルに空白行がある場合は動作しません。空白行はどうやって説明できますか? – Airfix

関連する問題