2013-02-12 6 views
13

を使用してテキストファイルを読む:擬似コード(私が考えたもの私はアンドロイドのアプリのようにテキストファイル読むことができる方法のInputStream

"something written\nin this file\nis to be read by\nthe InputStream" 

さ:

"1.something written 
2.in this file 
3.is to be read by 
4.the InputStream 
..." 

をので、私は次のように文字列を返すことができます):

make an inputstream 
is = getAssest().open("textfile.txt"); //in try and catch 
for loop{ 
string = is.read() and if it equals "." (i.e. from 1., 2., 3. etc) add "/n" ... 
} 

答えて

23

この

を試してみてください
9

BufferedReaderを使用して入力ストリームを読み取ります。 BufferedReaderは文字入力ストリームからテキストを読み込み、文字、配列、行の効率的な読み込みを提供するように文字をバッファリングします。 InputStreamは、バイトの入力ストリームを表します。 reader.readLine()はファイルを1行ずつ読み込みます。

BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
StringBuilder out = new StringBuilder(); 
String line; 
while ((line = reader.readLine()) != null) { 
    out.append(line); // add everything to StringBuilder 
    // here you can have your logic of comparison. 
    if(line.toString().equals(".")) { 
     // do something 
    } 

} 
0
   File fe=new File(abc.txt); 
       FileInputStream fis=new FileInputStream(fe); 
       byte data[]=new byte[fis.available()]; 
       fis.read(data); 
       fis.close(); 
       String str=new String(data); 
       System.out.println(str); 
+0

それはあなたが任意のtxtファイルを持っている、uはのFileInputStreamを使用して、そのテキストファイルを読みたい場合は、あなたがそうすることができる方法と場所あなたのコード – Adonis

+0

を使用するようにコンテキストを提供するのに役立つと考え..... .....あなたのファイルに=新しいファイル(abc.txt);これはあなたのコンソールに表示されます。data String str = new String(data); System.out.println(文字列); – Ziyad

+0

.read(data)の結果は無視されます。 –

関連する問題