读取和写入 Samsung Galaxy S SD 卡上的文本文件

发布于 2024-12-09 04:17:34 字数 1506 浏览 0 评论 0原文

我认为这一定不是一个很难实现的任务,我已经使用 HTC Desire 完成了它,但由于某种原因,我无法在 Android 应用程序中读取 Samsung Galaxy S SD 卡。

我使用 :

public String writeFile1(String text) {

    File sdDir = Environment.getExternalStorageDirectory(); 

    File myFile = new File(sdDir+"/TextFiles/patientDetails.txt");
    try{
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.write(text);
        myOutWriter.close();
        fOut.close();
        return "success";
    }catch (IOException e){
        e.printStackTrace();
        return "fail";
    }
}

这工作正常!文件内容被保存,我很高兴。但是,当我使用...执行相反操作时,

//
               File f = new File(Environment.getExternalStorageDirectory()+fileName);

           FileInputStream fileIS = new FileInputStream(f);

           BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));

           String readString = new String(); 

           //just reading each line and pass it on the debugger
           String s = "";
           while((readString = buf.readLine())!= null){
               s+=readString;   
           }
           return s;

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        } catch (IOException e){

           e.printStackTrace();
        }

我收到文件未找到异常!我刚刚写了它,当我安装SD卡时可以看到我写的内容。

有人知道这个问题的解决办法吗?谢谢

I am thinking that this must not be a very difficult task to achieve and i have managed it with the HTC Desire but for some reason I cannot read from the Samsung Galaxy S SD card in my android application.

I use :

public String writeFile1(String text) {

    File sdDir = Environment.getExternalStorageDirectory(); 

    File myFile = new File(sdDir+"/TextFiles/patientDetails.txt");
    try{
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.write(text);
        myOutWriter.close();
        fOut.close();
        return "success";
    }catch (IOException e){
        e.printStackTrace();
        return "fail";
    }
}

and this works fine! The file content gets saved and i am very happy. However, when I do the reverse using...

//
               File f = new File(Environment.getExternalStorageDirectory()+fileName);

           FileInputStream fileIS = new FileInputStream(f);

           BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));

           String readString = new String(); 

           //just reading each line and pass it on the debugger
           String s = "";
           while((readString = buf.readLine())!= null){
               s+=readString;   
           }
           return s;

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        } catch (IOException e){

           e.printStackTrace();
        }

I receive a file not found exception! I just wrote to it and can see what I wrote when I mount the SD Card.

Does someone know the solution to this? Thanks

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

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

发布评论

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

评论(2

何必那么矫情 2024-12-16 04:17:34

您使用了错误的构造函数,您应该使用

File f = new File(Environment.getExternalStorageDirectory(), "filename");

而不是

 File f = new File(Environment.getExternalStorageDirectory()+fileName);

现在您的代码将正常工作。

You are using wrong constructor you should use

File f = new File(Environment.getExternalStorageDirectory(), "filename");

instead of

 File f = new File(Environment.getExternalStorageDirectory()+fileName);

Now your code will work fine.

執念 2024-12-16 04:17:34

当您像这样初始化文件对象时会发生什么

Environment.getExternalStorageDirectory()+fileName

首先会发生什么您以这种方式获取路径

/sdcard

并与文件名连接然后您以这种方式获得结果

filename = "test.txt";

path > /sdcardtext.txt

现在检查是否找不到该文件所以要小心检查完整路径对于文件对象。

接下来你可以像这样使用

File f = new File(Environment.getExternalStorageDirectory(), "filename");

What happen when you initializing your file object like this

Environment.getExternalStorageDirectory()+fileName

in this what happen first you getting the path like this way

/sdcard

and concatenate with the file name then you getting the result this way

filename = "test.txt";

path > /sdcardtext.txt

now check that this file didn't found so beware check the complete path for file object.

next thing you can use like this way

File f = new File(Environment.getExternalStorageDirectory(), "filename");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文