获取内部文件路径

发布于 2024-12-20 11:55:21 字数 1646 浏览 1 评论 0原文

我打开一个内部文件并像这样写信给他:

final String NEWLINE = new String(newLine+"\n");     
FileOutputStream fOut = openFileOutput(fileName+".txt",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 
osw.write(NEWLINE);

我需要一种方法来检查内部文件(文件名+“.txt”)是否存在。

如果没有,则将 newLine 写入文件,否则追加 newLine 到文件。

我尝试过:

File file = getBaseContext().getFileStreamPath(fileName+".txt");
if(file.exists())...

但我不知道内部文件路径,并且它不起作用。

有什么建议吗?

更多细节:

private void writeStringToFile(String fileName, String newLine) {
    final String NEWLINE = new String(newLine + "\n");
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    try { // catches IOException below
        File file = getFileStreamPath(fileName + ".txt");
        if (!file.exists()) {
            Log.d("DAVID", "File not exist now create him");
            fOut = openFileOutput(fileName + ".txt", MODE_WORLD_READABLE);
            osw = new OutputStreamWriter(fOut);
        } else {
            Log.d("DAVID", "File already exist");
            osw = new OutputStreamWriter(fOut);
        }
        // Write the string to the file
        osw.append(NEWLINE);
        // osw.write(NEWLINE);
        // ensure that everything is really written out and close
        osw.flush();
        osw.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

第一次运行时没问题 - 我收到:“文件不存在,现在创建他”到日志 第二次我遇到由空指针引起的致命错误。 我认为这是因为 osw get null fOut

osw  = new OutputStreamWriter(fOut);

但我该如何防止它呢?

I open an internal file and write to him like this:

final String NEWLINE = new String(newLine+"\n");     
FileOutputStream fOut = openFileOutput(fileName+".txt",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut); 
osw.write(NEWLINE);

I need a way to check if internal file (fileName+".txt") exist.

If not, write newLine to file, else append newLine to file.

I tried:

File file = getBaseContext().getFileStreamPath(fileName+".txt");
if(file.exists())...

But I don't know the internal file path, and it's not working.

Any suggestions?

More details:

private void writeStringToFile(String fileName, String newLine) {
    final String NEWLINE = new String(newLine + "\n");
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    try { // catches IOException below
        File file = getFileStreamPath(fileName + ".txt");
        if (!file.exists()) {
            Log.d("DAVID", "File not exist now create him");
            fOut = openFileOutput(fileName + ".txt", MODE_WORLD_READABLE);
            osw = new OutputStreamWriter(fOut);
        } else {
            Log.d("DAVID", "File already exist");
            osw = new OutputStreamWriter(fOut);
        }
        // Write the string to the file
        osw.append(NEWLINE);
        // osw.write(NEWLINE);
        // ensure that everything is really written out and close
        osw.flush();
        osw.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

In the first time it runs it's ok - I'm getting: "File not exist now create him" to Log
In the second time I'm getting FATAL ERROR cause by null pointer.
I think it's because osw get null fOut

osw  = new OutputStreamWriter(fOut);

But how can I prevent it?

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

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

发布评论

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

评论(1

ゞ花落谁相伴 2024-12-27 11:55:21

上面的代码是如此错误,我不得不重写它:

private void apendStringToInternalFile(String fileName, String newLine)
        throws FileNotFoundException, UnsupportedEncodingException,
        IOException {
    newLine = new String(newLine + "\n");
    FileOutputStream fOut = openFileOutput(fileName + ".txt",
            Context.MODE_APPEND | Context.MODE_WORLD_READABLE);
    // MODE_WORLD_READABLE is _deprecated_
    try {
        // ALWAYS SPECIFY ENCODING -
        OutputStreamWriter osw = new OutputStreamWriter(fOut, "UTF-8");
        BufferedWriter buffer = new BufferedWriter(osw);
        buffer.write(newLine);
        buffer.flush();
    } finally {
        try {
            fOut.close();
        } catch (IOException e) {
            // Log this to be on the safe side -
            e.printStackTrace();
        }
    }
}

显然整个问题不存在。如果文件不存在,openFileOutput 将创建该文件,并添加标志 Context.MODE_APPEND 如果文件存在,它将追加。自由复制粘贴

The code above was so wrong I just had to rewrite it :

private void apendStringToInternalFile(String fileName, String newLine)
        throws FileNotFoundException, UnsupportedEncodingException,
        IOException {
    newLine = new String(newLine + "\n");
    FileOutputStream fOut = openFileOutput(fileName + ".txt",
            Context.MODE_APPEND | Context.MODE_WORLD_READABLE);
    // MODE_WORLD_READABLE is _deprecated_
    try {
        // ALWAYS SPECIFY ENCODING -
        OutputStreamWriter osw = new OutputStreamWriter(fOut, "UTF-8");
        BufferedWriter buffer = new BufferedWriter(osw);
        buffer.write(newLine);
        buffer.flush();
    } finally {
        try {
            fOut.close();
        } catch (IOException e) {
            // Log this to be on the safe side -
            e.printStackTrace();
        }
    }
}

Obviously the whole issue is not existent. openFileOutput will create the file if it doesn't exist and adding the flag Context.MODE_APPEND it will append if the file does exist. Copy paste freely

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