2012-07-17 57 views
-1

我想從我的.txt文件中讀取我的數據到android中的ReaderClass中,字段由「;」分隔。如何從.txt文件(android)獲取輸入流?

----這裏是我的我的最後一個職位的解決方案:

public void cut() 
    { 
    try{  


    InputStream input =context.getResources().openRawResource(R.raw.textfile); 
    BufferedReader br = null; 
    br=new BufferedReader(new InputStreamReader(input,"iso-8859-1")); 
    String line = null; 


    while ((line = br.readLine()) != null) {   

     String[] decoupage= line.split(";"); 


     String titre=decoupage[0]; 
     String description=decoupage[1]; 
     String reponse=decoupage[2]; 
     String explication=decoupage[3]; 
     String categorie=decoupage[4]; 
     String etat=decoupage[5];  

        //test Logcat 

      Log.d("information ", " buffer"); 
     Log.i("titre : ",titre); 
     Log.i("description : ",description); 
     Log.i("reponse : ",reponse); 
     Log.i("explication : ",explication); 
     Log.i("categorie : ",categorie); 
     Log.i("etat : ",etat); 



     } 
    in.close(); 

    }catch (Exception e){ 
    System.err.println("Error: " + e.getMessage()); 
    System.err.println("\n File not found"); 
    } 
    //end 
    } 
+1

ATLEAST嘗試** **的東西,你問問題之前! – 2012-07-17 11:52:51

+0

我對你的另一個問題發表了評論:** http://stackoverflow.com/questions/11483992/how-to-find-my-db-file-in-sqlite **你能回答這個問題嗎?可以幫助我增加我的知識? – 2012-07-17 12:09:06

回答

1
FileInputStream fis; 
fis = openFileInput("sample.txt"); 
StringBuffer Content = new StringBuffer(""); 

byte[] buffer = new byte[1024]; 
int length; 
while ((length = fis.read(buffer)) != -1) { 
    Content.append(new String(buffer)); 
} 

you will get entire content in a string buffer ,convert it into string, then you can apply yourString.split(";") to get all values which you can keep in some array. 
+0

我在哪裏提到字段之間的分隔「;」? – Angelika 2012-07-17 11:49:25

+1

您將得到一個字符串緩衝區將其轉換爲字符串,然後應用<您的字符串變量名稱> .split(「;」) – 2012-07-17 11:52:49

+0

好的,謝謝,StringTokenizer類呢? – Angelika 2012-07-17 11:55:38