FileWriter的不是写在Android中,以文件? [英] FileWriter not writing to file in Android?

查看:314
本文介绍了FileWriter的不是写在Android中,以文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个名为FILENAME.TXT的新文件,并写入了喜当我打开一个应用程序。我怎么去呢?我在Eclipse中运行这个code,preSS F11,打开AVD,然后点击应用。我得到的Hello World,APPNAME!消息,但没有文件被创建。

I am trying to write to a new file named "filename.txt" and write out "hi" when I open an app. How do I go about this? I run this code in Eclipse, press F11, open up the AVD, and click on the app. I get the "Hello World, appname!" message, but no file is created.

package com.paad.comparison;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Toast;

public class ComparisonOfControlCreationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
        String string1 = "Hey you";
        FileWriter fWriter;
        try{
            fWriter = new FileWriter("filename.txt", true);
            fWriter.write("hi");
            fWriter.flush();
            fWriter.close();
        } catch(Exception e) {
            e.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

编辑:对于任何人,恰好碰上将来出现此问题,请确保您的Andr​​oidManifest.xml文件的行

For anyone that happens to run into this problem in the future, make sure your AndroidManifest.xml file has the line

<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
之间没有与LT的空间;和u

< uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> without the space between < and u

推荐答案

您需要调用的 同步() 关闭文件,但是你需要之前的FileDescriptor 对于这一点,我不相信这是直接从的FileWriter 。但是,您可以创建使用的FileWriter 对象的的FileDescriptor 的FileOutputStream 可以提供一个用于此目的。

You need to call sync() before closing the file, but you need a FileDescriptor for that, and I don't believe it's available directly from FileWriter. However, you can create a FileWriter object using a FileDescriptor, and FileOutputStream can provide one for this purpose.

根据您的初始code以下,但我选择存储在我的例子中,SD卡文件,所以运行下面的code的正如的你应用程序清单将需要之前添加正确的权限&lt;应用方式&gt; 标签

The following is based on your initial code, but I've opted to store the file on the sdcard in my example, so to run the code below as is your app manifest will need the correct permission added prior to the <application> tag.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这是说,结局也不会,如果你想直接在设备上存储文件的影响。

That said, the outcome won't be affected if you want store the file on the device directly.

code(上Desire HD的测试):

Code (tested on a Desire HD):

package com.paad.comparison;

import java.io.FileOutputStream;
import java.io.FileWriter;

import android.app.Activity;
import android.os.Bundle;

public class ComparisonOfControlCreationActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        String string1 = "Hey you";

        FileOutputStream fos ;

        try {
            fos = new FileOutputStream("/sdcard/filename.txt", true);

            FileWriter fWriter;

            try {
                fWriter = new FileWriter(fos.getFD());
                fWriter.write("hi");
                fWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                fos.getFD().sync();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

最后,刷新()所谓的close(),要求刷新()前夕的close()是多余的,但不会对人体有害。

Finally, as flush() is called by close(), calling flush() immediately prior to close() is superfluous, though not harmful.

这篇关于FileWriter的不是写在Android中,以文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆