android - 创建文件夹而不是文件

标签 android file-io

我想在定义的目录中创建一个文件,我尝试了这两个代码,但第一个仅创建文件夹,另一个输出异常:没有这样的文件或目录: 第一个代码:

File file = new File(Environment.getExternalStorageDirectory()
                                 +File.separator
                                 +"carbu" 
                                 +File.separator
                                 +"install"); 
                            file.mkdir();

然后我添加了此代码希望创建文件:

File file2 = new File("/carbu/install/","voitu");
file2.createNewFile();

有人可以帮我吗? 非常感谢您:)。

最佳答案

在您的 Activity 中尝试此操作:

FileOutputStream fos = openFileOutput(YOUR_FILE_NAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(5);
oos.flush();

如果文件不存在,这将创建文件。当然,您应该关闭 oosfos

关于android - 创建文件夹而不是文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6861167/

相关文章:

android - java.security.cert.CertPathValidatorException : Could not validate certificate

android - '在包 'textcolor' 中找不到属性 'android' 的资源标识符'

C++文件I/O问题

c - 在 C 中附加失败,只是覆盖

C++文件操作: Reading specific line in a file.

android - 拦截 Google map 内置“我的位置”按钮的点击

java - 可以在不启用计费的情况下使用 Google Places API 吗?

带有 camera2 库的 Android 应用程序在 SDK19 启动时崩溃

C++ FileIO 奇怪的行为

file-io - R 中 apply + rbind 的问题