2012-02-29 12 views
0
private static int count = 0; 

    public static String[] play()throws Exception{ 
     File file=new File("E:/proj/"+count+".bin"); 
     FileInputStream fin=new FileInputStream("E:/proj/"+count+".bin"); 

     //reading the byte content of the bin files 
     byte fileContent[] = new byte[(int)file.length()]; 
     fin.read(fileContent); 

     //storing the deserialized object that is returned to an object. 
     Object obj=serializer.toObject(fileContent); 

     //converting the obtained object to string 
     String word=obj.toString(); 
     String[] args=new String[]{word}; 
     count++; 
     return args ;   
    } 

このコードは、実際にその指定されたパスに存在するすべてのbinファイルを読み込み、最終的にはそれを文字列に変換し、string[]リターンですべてのbyte[]文字列に変換など、さまざまな文字列の要素を格納することになりました。 string[]。カウンタのためにすべてのbinファイルを読み込んでいますが、何とかして、読み込んだ最初のバイナリファイルの文字列だけを返します。読書と返す複数のbinファイル

この変更されたバージョンの用量でも動作するようです。私はそれがすべてのbinファイルを読むと思うが、読み込まれた最後のbinファイルの文字列だけを返す。私が試していたのは、すべての文字列要素をstring[]に格納し、別の呼び出し関数にstring[]を返すことでした。

public static String[] play(){ 
    int i = 1; 
    String[] args=null;; 
    String result = null; 
    while (true) { 
     try { 
      result += processFile(i++); 
      args=new String[]{result}; 
     } 
      catch (Exception e) { 
      System.out.println("No more files"); 
      break; 
     } 
    } 
    return args; 
}  

private static String processFile(int fileNumber) throws Exception { 
    File file=new File("E:/proj/"+fileNumber+".bin"); 
    FileInputStream fin=new FileInputStream("E:/proj/"+fileNumber+".bin"); 

    //reading the byte content of the bin files 
    byte fileContent[] = new byte[(int)file.length()]; 
    fin.read(fileContent); 

    //storing the deserialized object that is returned, to an object. 
    Object obj=serializer.toObject(fileContent); 

    //converting the obtained object to string 
    String word=obj.toString(); 
    return word; 
} 
+0

ループ内からplay()メソッドが呼び出されていますか?残りのコードを表示できますか? – assylias

+0

hii Assylias...再生メソッドはループ内から呼び出されません。それは単に別のクラスで呼ばれているだけです... – kuki

答えて

0

私ははっきりとあなたの条件を理解していれば、あなたはあなたのコードこのよう変更してみてください可能性があります

List<String> args = new ArrayList<String>(); 
    while (true) { 
     try { 
      args.add(processFile(i++)); 
     } 
     catch (Exception e) { 
      // your code 
     } 
    } 
    String[] array = args.toArray(new String[0]); 
+0

Hii kuldeep ...ありがとうトン...それは働いた! :) – kuki

+0

@kuki、あなたの受け入れ率が下がるなら、あなたが問題を解決するのを助けたら "答えを受け入れる"ことを忘れないでください。 –

0

あなただけの投稿をコードでいくつかの問題があります を - 結果はそうnullに初期化されるあなたのコードは最初のループにNPEをスローします。 - 適切に初期化した場合( "")、各ループの新しい配列にargsが再割り当てされ、前のループから得た情報が失われます。

play()メソッドで配列を返すには、配列内の各項目が1つのファイルの内容である必要があります。あなたが何か違うものを望むなら、あなたの要件をより明確に説明する必要があります。

public static String[] play() { 
    int i = 1; 
    List<String> files = new ArrayList<String>(); 
    while (true) { 
     try { 
      files.add(processFile(i++)); 
     } catch (Exception e) { 
      System.out.println("No more files"); 
      break; 
     } 
    } 
    return files.toArray(new String[0]); 
} 
関連する問題