[英]Create file in specified directory
尝试在特定目录中创建文件,但它显示错误FileNotFound。 为什么? 我用不可能的路径吗? 我真的不知道,但似乎代码应该工作。
String day=/1;
String zn="/zn";
File_name=zn
String root= Environment.getExternalStorageDirectory().toString();
File_path=root+day;
File file1 = new File(File_path,File_name);
file1.mkdirs();
if(!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
OutputStream fos= new FileOutputStream(file1);
String l,d,p;
l = lessnum.getText().toString();
d = desc.getText().toString();
p = place.getText().toString();
fos.write(l.getBytes());
fos.write(d.getBytes());
fos.write(p.getBytes());
fos.close();
更改您的代码,就像在SD卡上创建文件一样
String root= Environment.getExternalStorageDirectory().getAbsolutePath();
String File_name = "File_name.Any_file_Extension(like txt,png etc)";
File file1 = new File(root+ File.separator + File_name);
if(!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
在当前您,您还缺少文件扩展名文件名,因此将字符串zn
更改为zn="/zn.txt";
并确保您已在AndroidManifest.xml
添加了Sd卡权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
首先,你创建一个目录
String root= Environment.getExternalStorageDirectory().toString();
String dirName =
root+ "abc/123/xy";
File newFile = new File(dirName);
newFile.mkdirs();
然后在该目录中创建一个文件
String testFile = "test.txt"; File file1 = new File(dirName,testFile); if(!file1.exists()){ try { file1.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
然后进行文件编写操作
try { OutputStream fos= new FileOutputStream(file1);
字符串l,d,p; l = lessnum.getText()。toString(); d = desc.getText()。toString(); p = place.getText()。toString(); os.write(l.getBytes()); fos.write(d.getBytes()); fos.write(p.getBytes()); fos.close(); } catch(Exception e){// TODO自动生成的catch块e.printStackTrace(); }
我想这会对你有所帮助......
谢谢...
您需要通过将以下行添加到您的Manifest,为您的应用程序提供写入SD卡的正确权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并查看http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29
String root= Environment.getExternalStorageDirectory().toString();
String dirName =
root+ "abc/123/xy";
File newFile = new File(dirName);
newFile.mkdirs();
String testFile = "test.txt";
File file1 = new File(dirName,testFile);
if(!file1.exists()){
try {
file1.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
并且在清单文件上<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
谢谢...
这是您最近的尝试:
File_path = root + File.separator + day;
File f_dir = new File(File_path);
f_dir.mkdirs();
File file1 = new File(f_dir, File_name);
if (!file1.exists()) {
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
OutputStream fos= new FileOutputStream(file1);
如果你向我们展示了完整的堆栈跟踪和错误消息,那么就更容易弄清楚出了什么问题,但我可以想到几种可能性:
f_dir.mkdirs()
返回的值,并且它可能返回false
以指示未创建目录路径。 这可能意味着:
file1.exists()
调用将返回true
。 事物存在的事实并不一定意味着你可以打开它来写作:
如果我写这篇文章,我会写这样的:
File dir = new File(new File(root), day);
if (!dir.exists()) {
if (!dir.mkdirs()) {
System.err.println("Cannot create directories");
return;
}
}
File file1 = new File(dir, fileName);
try (OutputStream fos= new FileOutputStream(file1)) {
...
} catch (FileNotFoundException ex) {
System.err.println("Cannot open file: " + ex.getMessage());
}
我只在需要时尝试创建目录...并检查创建是否成功。 然后我只是尝试打开文件写入它。 如果该文件不存在,则将创建该文件。 如果无法创建,则FileNotFoundException消息应解释原因。
请注意,我还更正了您在变量名称选择中所犯的样式错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.