2013-02-12 15 views
13

Come posso leggere un file di testo come in Android:La lettura di un file di testo utilizzando InputStream

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

così posso essere restituito una stringa del tipo:

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

quello che ho pensato di IS (Pseudocodice):

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" ... 
} 

risposta

23

provare questo

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Toast; 
import java.io.*; 

public class FileDemo1 extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     try { 
      playWithRawFiles(); 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), "Problems: " + e.getMessage(), 1).show(); 
     } 
    } 

    public void playWithRawFiles() throws IOException {  
     String str = ""; 
     StringBuffer buf = new StringBuffer();    
     InputStream is = this.getResources().openRawResource(R.drawable.my_base_data); 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      if (is != null) {        
       while ((str = reader.readLine()) != null) {  
        buf.append(str + "\n"); 
       }     
      } 
     } finally { 
      try { is.close(); } catch (Throwable ignore) {} 
     } 
     Toast.makeText(getBaseContext(), buf.toString(), Toast.LENGTH_LONG).show(); 
    } 
} 
9

Utilizzare BufferedReader per leggere il flusso di input. Mentre BufferedReader leggerà il testo da un flusso di input di caratteri, bufferizzando i caratteri in modo da fornire una lettura efficiente di caratteri, matrici e linee. InputStream rappresenta un flusso di input di byte. reader.readLine() leggerà il file riga per riga.

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

Sarebbe utile per fornire un contesto per come e dove utilizzare il codice – Adonis

+0

se si dispone di un file txt e u desidera leggere il file di testo utilizzando FileInputStream è possibile farlo e ..... ..... nel tuo file fe = new File (abc.txt); e questo sarà visualizzato nella tua console read data String str = new String (data); System.out.println (str); – Ziyad

+0

Il risultato di .read (dati) viene ignorato –

Problemi correlati