2

いじり疲れた。そして、私はそれが実際には不可能であると信じ始めています. ファイル「myPath/myFile.txt」の書き込み方法を示す簡単な例はどこにありますか? そして、もう一度読み返しますか?

これは、動作しないコード ブロックの例です。

if(pathExists(path, ctx))  
{  
File file = new File(ctx.getFilesDir().getAbsolutePath() +"/" + path, fileName);  
FileInputStream fIn = new FileInputStream(file);  

InputStreamReader isr = new InputStreamReader(fIn);  

StringWriter writer = new StringWriter();  
IOUtils.copy(fIn, writer, "UTF-8");  
fileStr = writer.toString();  
}

これは私が得るエラーです:
「java.io.IOException:ディレクトリです」

4

4 に答える 4

3

この次のスニペットは、アプリケーション スペースに保存されているファイルを目的のパスにコピーします。

File mfile=new File("/data/data/src.com.file.example/files");
File[] list=mfile.listFiles();
    // The path where you like to save your files
String extStorageDirectory = "/mnt/sdcard/backup/";


    File file23 = null;
    File fr = null;
    for(int i =0;i<list.length;i++){
File myNewFolder = new File(extStorageDirectory +list[i].getName().substring(0,5));

if(myNewFolder.exists()){

   String selectedFilePathq = "data/data/src.com.file.example/file/"+list[i].getName();
file23 = new File(selectedFilePathq);
fr = new File(myNewFolder+"/"+list[i].getName());
}else{
  myNewFolder.mkdir();
String selectedFilePathq = "data/data/src.com.file.example/files  /"+list[i].getName();
file23 = new File(selectedFilePathq);
fr = new File(myNewFolder+"/"+list[i].getName());
}



try {
 copy(file23,fr);
} catch (IOException e) {
    e.printStackTrace();
}
}








public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}
于 2012-02-15T05:22:52.687 に答える
0

Java のようにシリアル化メソッドを使用する必要があります。テキストをファイルとして保存するには FileOutputStream を使用し、ファイルを読み取るには FileInputStream を使用する必要があります。次のコードを確認すると、単純な編集テキストと、そのファイルに保存されたデータを保存するためのボタンと読み取るための 2 つのボタンがあります。

次のコードは、テキストを raksi という名前のファイルに保存します。

Button savebutton = (Button)findViewById(R.id.but);
savebutton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
        e= (EditText)findViewById(R.id.edit);
        StringBuffer sb = new StringBuffer();
        sb.append(e.getText().toString());
        String s = sb.toString();
        try { 
            final String TESTSTRING = new String(s);
            FileOutputStream fOut = openFileOutput("raksi.txt",MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut);
            osw.write(TESTSTRING);
            ll = TESTSTRING.length();
            osw.flush();
            osw.close();
        }catch (Exception e) {
        // TODO: handle exception
        }
    }
});

次のコードは、ボタン get のクリック リスナーです。ファイルからデータを読み取り、トーストとして表示します。

Button b1 = (Button)findViewById(R.id.but1);
b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    try{
        FileInputStream fIn = openFileInput("name.txt");
        InputStreamReader isr = new InputStreamReader(fIn);
        char[] inputBuffer = new char[ll];
        isr.read(inputBuffer);
        String readString = new String(inputBuffer);
        Toast.makeText(getApplicationContext(), readString, Toast.LENGTH_LONG).show();
    }
    catch(IOException e){
    // TODO: handle exception
    }
于 2012-02-14T10:36:37.460 に答える
0
String strfile1 = getApplicationContext().getFilesDir().getAbsolutePath() + "/serstatus.txt" ;

File f1 = new File(strfile1);
于 2012-02-14T07:07:35.157 に答える
0

Androidシステムによって独自のアプリケーションに割り当てられたプライベートファイルスペースを除いて、Android内部ストレージに書き込むことはできませんでした(デバイスがルート化されていない限り)。

于 2012-02-14T06:38:22.183 に答える