Android:如何指定写入文件的位置

发布于 2024-11-08 19:56:33 字数 397 浏览 3 评论 0原文

我想将此代码创建的文件存储在我的 sdcard 中的某个位置(即 sdcard/myfiles/file/)

    final String TESTSTRING = new String("Hello Android");

    FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(TESTSTRING);

    osw.flush();
    osw.close();

我是 java 和 android 开发新手,非常感谢您的帮助! :)

I want to store the created file of this code somewhere in my sdcard (i.e. sdcard/myfiles/file/)

    final String TESTSTRING = new String("Hello Android");

    FileOutputStream fOut = openFileOutput("samplefile.txt", MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(TESTSTRING);

    osw.flush();
    osw.close();

I'm new to java and android dev, many thanks for any help! :)

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

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

发布评论

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

评论(2

苄①跕圉湢 2024-11-15 19:56:33

使用以下代码在SDCard中写入文件

try {
    File root = Environment.getExternalStorageDirectory();
    if (root.canWrite()){
        File gpxfile = new File(root, "samplefile.txt");
        FileWriter gpxwriter = new FileWriter(gpxfile);
        BufferedWriter out = new BufferedWriter(gpxwriter);
        out.write("Hello world");
        out.close();
    }
} catch (IOException e) {
    Log.e(TAG, "Could not write file " + e.getMessage());
}

Environment.getExternalStorageDirectory() 类返回您的sdcard的路径。

Use following code to write file in SDCard

try {
    File root = Environment.getExternalStorageDirectory();
    if (root.canWrite()){
        File gpxfile = new File(root, "samplefile.txt");
        FileWriter gpxwriter = new FileWriter(gpxfile);
        BufferedWriter out = new BufferedWriter(gpxwriter);
        out.write("Hello world");
        out.close();
    }
} catch (IOException e) {
    Log.e(TAG, "Could not write file " + e.getMessage());
}

Environment.getExternalStorageDirectory() class returns the path of your sdcard.

梦旅人picnic 2024-11-15 19:56:33

使用以下代码...

try {
File root = Environment.getExternalStorageDirectory()+"/myfiles/file/";
if (root.canWrite()){
    File gpxfile = new File(root, "gpxfile.gpx");
    FileWriter gpxwriter = new FileWriter(gpxfile);
    BufferedWriter out = new BufferedWriter(gpxwriter);
    out.write("Hello world");
    out.close();
}
} catch (IOException e) {
    Log.e(TAG, "Could not write file " + e.getMessage());
}                   

Use the following code ...

try {
File root = Environment.getExternalStorageDirectory()+"/myfiles/file/";
if (root.canWrite()){
    File gpxfile = new File(root, "gpxfile.gpx");
    FileWriter gpxwriter = new FileWriter(gpxfile);
    BufferedWriter out = new BufferedWriter(gpxwriter);
    out.write("Hello world");
    out.close();
}
} catch (IOException e) {
    Log.e(TAG, "Could not write file " + e.getMessage());
}                   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文