我需要一个例子来展示如何读写文件,包括Android内部存储的路径



我太累了。我开始相信这实际上是不可能的。在哪里可以找到一个简单的例子来展示如何编写文件"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:是一个目录"

下面的片段将把保存在应用程序空间中的文件复制到您想要的路径中。

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();
}

除了安卓系统分配给您自己的应用程序的私人文件空间外,您无法写入安卓内部存储(除非您的设备是root设备)。

String strfile1 = getApplicationContext().getFilesDir().getAbsolutePath() + "/serstatus.txt" ;
File f1 = new File(strfile1);

您必须像在java中一样使用序列化方法。要将文本保存为文件,您应该使用FileOutputStream;要读取文件,则应该使用FileInputStream。您可以检查以下代码—它有一个简单的edittext和两个按钮—一个用于保存,另一个用于读取保存在该文件中的数据。

以下代码用于将文本保存在名为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的点击监听器。它从文件中读取数据,并显示为toast、

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
    }

最新更新