我尝试保存文件时遇到问题。首先,当我在较旧的Eclipse上尝试它时,它正常工作,但我必须使用新的Eclipse,所以我从旧的Eclipse导出项目并将其导入新的Eclipse。现在它没有保存并给我这个例外:
$('.abc').click(function(e, elem){ // alert is shown once only
$(".custom-menu li").click(function(){ // alert is shown 3 times
我把我用来保存的代码(这个代码适用于较旧的eclipse,在new中给我异常):
java.io.FileNotFoundException: /storage/sdcard/MyApp/questions.txt: open failed: EACCES (Permission denied)
我的清单文件:
public void saveDates(boolean mainScreen)
{
String extr = Environment.getExternalStorageDirectory().toString();
File mFolder = new File(extr + "/MyApp");
if (!mFolder.exists())
{
mFolder.mkdir();
}
String strF = mFolder.getAbsolutePath();
File mSubFolder = new File(strF /*+ "/MyApp-SubFolder"*/);
if (!mSubFolder.exists())
{
mSubFolder.mkdir();
}
//Nombre del fichero
String s = "questions.txt";
BufferedWriter out;
try
{
FileWriter fileWriter = new FileWriter(mSubFolder.getAbsolutePath() + "/" + s, false);
out = new BufferedWriter(fileWriter);
out.write(indexAnswer + ";");
out.write("\r\n");
out.write(finished + ";");
out.write("\r\n");
out.write(directricesGenerales + ";");
out.write("\r\n");
out.write(politicaAmbiental + ";");
out.write("\r\n");
out.write(planificacion + ";");
out.write("\r\n");
out.write(implementacion + ";");
out.write("\r\n");
out.write(verificacion + ";");
out.write("\r\n");
out.write(revision + ";");
out.write("\r\n");
out.write(definicionProyecto + ";");
out.write("\r\n");
out.write(analisisAmbiental + ";");
out.write("\r\n");
out.write(desarrollo + ";");
out.write("\r\n");
out.write(disenyoDetalle + ";");
out.write("\r\n");
out.write(planAccion + ";");
out.write("\r\n");
out.write(evaluacionContinua + ";");
out.write("\r\n");
out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Datos guardados.", Toast.LENGTH_LONG).show();
if(mainScreen)
{
// Cerrar la ventana de test y volver a la de inicio
Intent intent = new Intent();
intent.setClass(questions.this, MainActivity.class);
startActivity(intent);
finish();
}
}
我的模拟器细节图片:
任何人都可以帮助我吗?我想保存文件就像我在旧版本中那样,我不知道为什么不起作用。
答案 0 :(得分:3)
首先,您需要在清单中使用此权限才能读取文件:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
其次,您的文件确实存在。如果没有,那么首先需要在向其写入数据之前创建文件。
如何创建文件:How to create a file in Android?
创建文件的示例代码:
// use "File.separator" instead of "/"
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
//create the file
file.createNewFile();
//text you want to write to your file
String text = "your_text";
//check if file exists
if(file.exists()){
OutputStream fo = new FileOutputStream(file);
//write the data
fo.write(text);
//close to avoid memory leaks
fo.close();
//give a log message that the file was created with "text"
System.out.println("file created: "+file);
}