Java 在SD卡上创建新文件
我试图在android studio中创建一个程序,该程序在sd卡上的.txt文件上写入/如果还没有文件,则创建一个新文件,但它从未创建该文件,我经常遇到这样的异常: java.io.IOException:没有这样的文件或目录 这是我的密码:Java 在SD卡上创建新文件,java,android,Java,Android,我试图在android studio中创建一个程序,该程序在sd卡上的.txt文件上写入/如果还没有文件,则创建一个新文件,但它从未创建该文件,我经常遇到这样的异常: java.io.IOException:没有这样的文件或目录 这是我的密码: public void write(String filename) throws IOException { EditText contentTxt = (EditText)findViewById(R.id.fileTxtField);
public void write(String filename) throws IOException {
EditText contentTxt = (EditText)findViewById(R.id.fileTxtField);
String path = Environment.getExternalStorageDirectory().getPath()+"/Demo/"+filename;
File file = new File(path);
if(!file.exists())
{
file.getParentFile().mkdirs();
file.createNewFile();
new AlertDialog.Builder(this).setTitle("File Created").setMessage("File successfully created!").show();
}
PrintWriter out = new PrintWriter(new FileWriter(file, true));
out.println(contentTxt.getText().toString());
out.flush();
out.close();
}
编辑:我测试了你的代码,它运行正常。您需要读写权限 将这些添加到清单中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
}
接下来覆盖onRequestPermissionsResult
函数/方法。在onCreate之外执行此操作(显然)
至于你的代码,它就像我说的那样工作,我只是想改变一些关于它的东西
如果目录../Demo/不存在,则无法在那里创建文件,因此请确保先创建目录。你做这件事的方式很好,但做的工作比需要的多
//Lets make a new DIR
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
if(!file.exists())
{
file.createNewFile();
new AlertDialog.Builder(this).setTitle("File Created").setMessage("File successfully created!").show();
}
希望能奏效!:) 您是否请求在清单中写入外部存储?您是否也在运行时使用
requestPermissions()
请求它?另外,请使用File File=new文件(Environment.getExternalStorageDirectory(),“Demo/”+文件名)代码>。请记住,这些都不能在安卓Q或更高版本上使用。使用Context
上的getExternalFilesDir()
将文件写入其中,或者使用存储访问框架允许用户选择要使用的内容位置。感谢您的帮助!不幸的是,我现在有一个不同的问题,每当我试图请求许可时,我都没有收到任何请求,请求时我是否做错了什么<代码>如果(ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!=PackageManager.permission(已授予){ActivityCompat.requestPermissions(this,新字符串[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);返回false;}
您已经授予应用权限了吗?如果是这样,那么就不需要请求权限,因为应用程序已经有了权限。这是一次性的事情。要测试这一点,只需卸载应用程序或取消选中应用程序设置中的存储权限。我假设你在做布尔函数?这就是为什么返回false的原因?我应该澄清一下,如果应用程序具有存储权限,那么您将不会看到请求消息。另外,在我个人看来,我会在onRequestPermissionsResult中做所有事情
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1: { //CODE 1
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// WRITE_EXTERNAL_STORAGE-related task you need to do.
//Continue and write the file
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
//Maybe display a message or something
}
return;
}
// other 'case' lines to check for other
// permissions this app might request.
}
}
//Lets make a new DIR
if (!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
if(!file.exists())
{
file.createNewFile();
new AlertDialog.Builder(this).setTitle("File Created").setMessage("File successfully created!").show();
}