2017-08-02 27 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

あなたのmanifest.xmlに内部ストレージの書き込み操作をしていることを確認してください。どうやってそれを見つけようとしているのですか? –

+0

私のアンドロイドデバイスやエミュレータでファイルを見つけることができません。 istは/ data/user/0 /ディレクトリを指定しますが、このディレクトリは存在しません。 –

+0

API ver 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; 
     }}} 

、あなたは、読み取りのためのユーザーの権限を与え、あなたはあなたが見つけることができない何を意味するの

関連する問題