2012-01-23 65 views
0

嗨我一直在嘗試很多工作來檢索文件的內容。我的目的是將一些內容寫入文件,並在單擊按鈕時將它們作爲文本視圖返回。我已經寫了一些代碼,但不幸的是,它不是working.Can在進階寫入文件和檢索文件的內容

public class Writing_to_fileActivity extends Activity { 
    /** Called when the activity is first created. */ 
    public EditText edit; 
    public Button btn, read; 
    public TextView tv; 
    public String str; 
    public FileOutputStream fOut; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     edit = (EditText) findViewById(R.id.edit); 
     btn = (Button) findViewById(R.id.btn); 
     btn.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       str = edit.getText().toString(); 
       try { 
        File sdcard = Environment.getExternalStorageDirectory(); 
        fOut= openFileOutput("Bharath.txt", 
          MODE_WORLD_WRITEABLE);      
        OutputStreamWriter osw = new OutputStreamWriter(fOut);      
        osw.write(str);      
        osw.flush();       
        osw.close();            
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      }  
     }); 
     read =(Button)findViewById(R.id.read); 
     read.setOnClickListener(new OnClickListener() {  
      public void onClick(View v) { 
       StringBuilder text = new StringBuilder(); 
       try { 
        BufferedReader br = new BufferedReader(new FileReader(fOut)); 
        String line; 
        while ((line = br.readLine()) != null) { 
         text.append(line); 
         text.append('\n'); 
        } 
       } catch (IOException e) { 
       } 
       TextView tv = (TextView) findViewById(R.id.tet); 
       tv.setText(text); 
      } 
     }); 
    } 
} 
+1

你究竟是什麼意思,不工作?你是否得到一個例外,或者它只是沒有做你期望的? – Kingamajick

+0

是什麼問題,具體是...添加一些日誌。或在輸出結果.. – AAnkit

+0

我沒有得到文本view.ie,整個代碼運行正常,但文本視圖不會出現後,我點擊按鈕 –

回答

1

任意一個幫助me.thanks重新定義你的文件

FileInputStream fIn; 

和文件輸入流設置到文件

fIn=openFileInput("Bharath.txt"); 

然後設置緩衝器讀取器從所述流中讀取:

BufferedReader br = new BufferedReader(new FileReader(fIn)); 
0

這是代碼在我的應用程序希望這有助於

 File path = new File(Environment.getExternalStorageDirectory(),"/text"); 
     if (!path.exists()){path.mkdirs();} 

     try{ 
     File text = new File(path,"text.txt"); 
     FileWriter writer = new FileWriter(text); 
     writer.append(edit.getText()); 
     writer.flush(); 
     writer.close(); 
        } 
       catch//// 

閱讀

  File txt = new File(Environment.getExternalStorageDirectory(),"/text"); 
    File file = new File(txt,"text.txt"); 
    StringBuilder text = new StringBuilder(); 
     if(file.exists()) { 
     try { 
      BufferedReader br = new BufferedReader(new FileReader(file)); 
      String line; 
      while ((line = br.readLine()) != null) { 
       text.append(line); 
       text.append('\n'); 
          tv.setText(Text); 
      } 
      //catch 
+0

Dude解析器不在命令行後移動 FileWriter writer = new FileWriter(text); 並在讀取後行 File text = new File(path,「text.txt」); –

0

更換你下面的行...

BufferedReader br = new BufferedReader(new FileReader(fOut)); 

BufferedReader br = new BufferedReader(new FileReader("Bharath.txt")); 

然後嘗試...

+0

我已經做了,但徒勞的夥計,Textview沒有出現 –

+0

在你的while循環內,你正在從文件中讀取.....嘗試在控制檯上打印「行」....這將幫助你在調試... –

+0

在這段時間的條件會閱讀和檢查它。解析器沒有從BufferedReader行檢查br = new BufferedReader(new FileReader(fOut)); –