2017-08-02 51 views
-2

我有一個應用程序在(text.txt)文件的內部存儲器 上保存登錄信息,但我在內部存儲器上找不到該文件。 你可以請幫忙。 這裏是我的代碼:在內部存儲器上找不到文件

public void Save(View view) throws IOException { 

    String text1=userName.getText().toString(); 
    String text2=password.getText().toString(); 
    File file=null; 
    text1=text1+" "; 
    FileOutputStream fileOutputStream = null; 
    try{ 
     file=getFilesDir(); 
     fileOutputStream=openFileOutput("text.txt", Context.MODE_PRIVATE); 
     fileOutputStream.write(text1.getBytes()); 
     fileOutputStream.write(text2.getBytes()); 

    }catch(FileNotFoundException e){ 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    finally{ 
     fileOutputStream.close(); 
    } 
    Toast.makeText(this,"Saved successfully "+file,Toast.LENGTH_SHORT).show(); 
} 

} 


Thanks! 
+1

你是什麼意思,你找不到。你是如何找到它的? –

+0

我的意思是我無法在我的android設備或我的模擬器上找到該文件, ist給我目錄/ data/user/0/...但此目錄不存在 –

+0

從API版本24開始,您無法在Android Monitor中看到您的內部目錄。有一些錯誤來訪問數據目錄。如果你寫代碼來檢查你應該能夠得到它。 –

回答

0

嘗試類似你這樣的代碼只是改變按要求。在一個文件,該數據保存在我的內部存儲

在內部創建一個文件存儲

package com.exampl.sdcardexample; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 

    EditText file,data; 
    Button save,view_data; 
    TextView result; 
    FileOutputStream fout; // used to write data in the file 
    OutputStreamWriter myWriter; // used to write data in the file 
    FileInputStream fin; // used to read the data in the file 
    BufferedReader myReader;// used to read the data in the file 

    String fileName; 
    String fileData; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     result=(TextView)findViewById(R.id.textView1); 
     file=(EditText)findViewById(R.id.editText1); 
     data=(EditText)findViewById(R.id.editText2); 
     save=(Button)findViewById(R.id.button1); 
     view_data=(Button)findViewById(R.id.button2); 
     save.setOnClickListener(this); 
     view_data.setOnClickListener(this); 
     fileName=file.getText().toString(); 
     fileData=data.getText().toString(); 

    } 
    @Override 
    public void onClick(View v) 
    { 
     switch (v.getId()) 
     { 
     case R.id.button1: 
      // creating a new file and saving the data inside the file 

      Toast.makeText(getApplicationContext(), "working", Toast.LENGTH_SHORT).show(); 

      try { 
       fileName=file.getText().toString(); 
       fileData=data.getText().toString(); 
       //File myFile=new File("/storage/emulated/0/"+fileName);// path of internal memory   
       File myFile=new File("sdcard/"+fileName); 
       myFile.createNewFile(); 
       fout=new FileOutputStream(myFile); 
       myWriter=new OutputStreamWriter(fout); 
       myWriter.append(fileData); 
       myWriter.close(); 
       fout.close(); 

       Toast.makeText(getApplicationContext(), "Data saved", Toast.LENGTH_LONG).show(); 

      } catch (FileNotFoundException e) {e.printStackTrace();} 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }   

      break; 

     case R.id.button2: 
      // Reading the data from the file we created 

      fileName=file.getText().toString(); 
      fileData=data.getText().toString(); 
      String datarow=""; 
      String result_data=""; 
      //StringBuffer stringBuffer=new StringBuffer(); 
      try 
      { 
       File myFile=new File("sdcard/"+fileName); 

       fin=new FileInputStream(myFile); 
       myReader=new BufferedReader(new InputStreamReader(fin)); 

       try { 
        while((datarow=myReader.readLine())!=null) 
        { 
         result_data+=datarow+"\n";// getting all the data 
        } 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       try { 
        myReader.close();// closing the file 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      result.setText(result_data);   

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }   
      break; 
     }}} 

並確保您在manifest.xml中爲內部存儲的讀寫操作提供了用戶權限

相關問題