获取内部文件路径
我打开一个内部文件并像这样写信给他:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码是如此错误,我不得不重写它:
显然整个问题不存在。如果文件不存在,
openFileOutput
将创建该文件,并添加标志Context.MODE_APPEND
如果文件存在,它将追加。自由复制粘贴The code above was so wrong I just had to rewrite it :
Obviously the whole issue is not existent.
openFileOutput
will create the file if it doesn't exist and adding the flagContext.MODE_APPEND
it will append if the file does exist. Copy paste freely