Java 忽略File.createNewFile()的结果
请你帮我找出我做错了什么。我在使用android studio创建文件时遇到了一个问题。 没有错误,但未创建文件“textstring.txt”。 W/System.err:java.io.IOException:open失败:enoint(没有这样的文件或目录)和 W/System.err:at java.io.File.createNewFile(File.java:944)是我在myFile.createNewFile()中遇到的警告 这是我的代码行Java 忽略File.createNewFile()的结果,java,android,Java,Android,请你帮我找出我做错了什么。我在使用android studio创建文件时遇到了一个问题。 没有错误,但未创建文件“textstring.txt”。 W/System.err:java.io.IOException:open失败:enoint(没有这样的文件或目录)和 W/System.err:at java.io.File.createNewFile(File.java:944)是我在myFile.createNewFile()中遇到的警告 这是我的代码行 private String INPU
private String INPUT_FILE = "textstring.txt";
private String inputString = "thisIsTheTextToWrite";
private File myFile = null;
private Button myWrite = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create a file
myFile = new File(Environment.getExternalStorageDirectory().getPath() + "/Android/Data/" + getPackageName() + "/files/" + INPUT_FILE);
mWrite = (Button)findViewById(R.id.b_write);
mWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
FileOutputStream output = openFileOutput(INPUT_FILE, MODE_PRIVATE);
output.write(inputString.getBytes());
if(output != null)
output.close();
//
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState()))
{
myFile.createNewFile();
output = new FileOutputStream(myFile);
output.write(inputString.getBytes());
if(output != null)
output.close();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
问题似乎发生在//myFile.createNewFile()处;
为什么?
提前谢谢你这只是一个警告,对吗 您应该始终检查myFile.createNewFile()的结果。:
就这样你能详细说明你面临的问题吗?因为看你的标题,它似乎只是一个警告。i、 e您没有使用方法myFile.createNewFile()的返回值;你也可以忽略这一点。你是正确的,没有错误,但是“textstring.txt”文件没有创建。W/System.err:java.io.IOException:open failed:enoint(没有这样的文件或目录)和W/System.err:at java.io.file.createNewFile(file.java:944)是我收到的警告,发生在myFile.createNewFile()上
if(!myFile.createNewFile()) {
Log.e("mDebug", "Couldn't create the file");
}