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

发布于 2025-01-05 06:17:53 字数 521 浏览 0 评论 0原文

我已经厌倦了胡闹。 我开始相信这实际上是不可能的。 我在哪里可以找到一个显示如何编写文件“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:是一个目录”

I'm tired messing around.
And i'm beginning to believe that it actually isn't possible.
Where do i find a simple example showing how to write a file "myPath/myFile.txt"?
And then reading it back again?

This an example of a code block that i can't get to work:

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

This is the error that i get:
"java.io.IOException: Is a directory"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷→糖丶微凉 2025-01-12 06:17:53

以下代码片段会将应用程序空间中保存的文件复制到您所需的路径。

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

This following snippet will copy you files which are saved in the application space to your desired path.

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();
}
残月升风 2025-01-12 06:17:53

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

You could not write to Android internal storage (not unless your device is rooted) aside from the private file space assigned to your own application by the android system.

我偏爱纯白色 2025-01-12 06:17:53
String strfile1 = getApplicationContext().getFilesDir().getAbsolutePath() + "/serstatus.txt" ;

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

File f1 = new File(strfile1);
紫罗兰の梦幻 2025-01-12 06:17:53

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

以下代码是将文本保存在名为 raksi.txt 的文件中。

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

以下代码是按钮获取的点击监听器。它从文件中读取数据并显示为 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
    }

you have to use serialization methods as in java.
to save an text as file you should be using FileOutputStream and to read the file you should be using FileInputStream. You can check the following code it has a simple edittext and two buttons one to save and one to read data saved in that file.

The following code is to save text in the file named 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
        }
    }
});

The following code is the click listener for the button get. it reads the data from the file and displays as 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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文