2011-06-18 37 views
1

私はアンドロイドニュービーですので、これは些細なことと長い投稿と思われます。私は、などをGoogleで検索しているが、私は見つけることができる唯一のアンドロイドの言及は、私は私のクラスではなくさえするjava.io.InputStreamをインポートした後、これを使用してみましたテキストファイルの読み込み方法

InputStream is = getAssets().open("read_asset.txt"); 
     int size = is.available(); 

     // Read the entire asset into a local byte buffer. 
     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 

を参照するように見えます。それはgetAssets()でエラーを起こしました。

私はリストビューのrowIdを使用しようとしています。 テキストファイルを開き、行ごとにファイルを読み込み、 最初の16文字などの文字列配列 を生成し、配列を使用して次のアクティビティのリストビューにデータを挿入します。

String[] sectID = null; //to be loaded in listview 
    switch (rowId){ 
     case 0://custom, go to section input screen 
     case 1:     
    readSectionFile s = new readSectionFile("Sect_US.dat"); 
    sectID=s.arrayShapesAll(); 

私のreadSectionFileクラス(抽出)は次のとおりです。

public readSectionFile(String FileName) { 
    //Count the number of section records in the data file 
    String line = null; // String that holds current file line 
    int recordcount = 0; // Line number of count 
    try{ 
     BufferedReader buf = new BufferedReader(new FileReader(FileName)); 
     // Read file to count records 
     while ((line = buf.readLine()) != null){ //null = EOF 
      line = buf.readLine(); 
      if (line.substring(0, 1).equals("*") || line.length() == 0) {  //== comment or blank line 
       //do nothing 
      }else{ 
       recordcount++; 
      } 
     }//while 
     buf.close(); 
    }catch (IOException x){ 
     x.printStackTrace(); 
    }//end try 
    // Now read file to load array 
    mSectionIDArray = new String[recordcount + 1]; 
    mSectionIdx = new int[recordcount + 1][2]; 
    mData = new double[recordcount + 1][15]; 
    int c=0; 
    String sectdata = null; // String that holds current file line 
    try { 
     BufferedReader buf = new BufferedReader(new FileReader(FileName)); 
     while ((sectdata = buf.readLine()) != null){   //null = EOF 
      sectdata = buf.readLine(); 

コードはreadSectionFile S =新しいreadSectionFile( "Sect_US.dat")で動作し、クラッシュしません。

readSectionFileコードでも、bufの2番目のインスタンスはtry、catchブロックを要求するEclipseエラーを生成しますが、最初のインスタンスは受け入れられます。

私の質問は、 このテキストファイル(/ assets)を正しく開くつもりですか? 2番目のbufの使用で何が問題になっていますか?

答えて

1

getAssets()はActivityのメソッドです。別のクラスからgetAssets()を呼び出そうとしている場合は、そのメソッドを呼び出すクラスにアクティビティのコンテキストを渡してから、context.getAssets()を呼び出します。

0

あなたは書きました:

私が正しく(中/資産)は、このテキストファイルを開くについて行くだろうか?

1つのオプションは、resrawという名前のディレクトリ内のテキストファイルを配置し、openRawResource(...)を使用してStringにそれらを読むことです。

String myText = readTextFileFromResource(context, R.raw.my_text_file); 

private String readTextFileFromResource(Context context, int resourceId){ 
    StringBuilder fileContents = new StringBuilder(); 
    try{ 
     InputStream inputStream = context.getResources().openRawResource(resourceId); 
     InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
     String nextLine; 
     while((nextLine = bufferedReader.readLine()) != null){ 
      fileContents.append(nextLine); 
      fileContents.append('\n'); 
     } 
    }catch(IOException e){ 
     //handle 
    }catch(Resources.NotFoundException nfe){ 
     //handle 
    } 
    return fileContents.toString(); 
} 
、あなたが rawディレクトリに​​と呼ばれるテキストファイルを持っていると仮定すると、
関連する問題