Android上的java.io.FileNotFoundException,打开失败:ENOENT(没有这样的文件或目录)
Posted
技术标签:
【中文标题】Android上的java.io.FileNotFoundException,打开失败:ENOENT(没有这样的文件或目录)【英文标题】:java.io.FileNotFoundException on Android, open failed: ENOENT (No such file or directory) 【发布时间】:2015-04-04 16:49:11 【问题描述】:我正在尝试为我的应用程序保存和写入包含一些数据的 XML 文件, 但是当它尝试打开文件时,它会给出 java.io.FileNotFoundException。
我用来保存 XML 文件的代码是:
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("http://xml.apache.org/xsltindent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(getActivity().getFilesDir(), "rooster.xml"));
System.out.println("test");
我用来读取文件的代码是:
File XmlFile = new File(getActivity().getFilesDir(), "file.xml");
if (!XmlFile.exists())
System.out.println("No XML file!");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(XmlFile);
它给出的logcat是:
OpenGL ES Shader Compiler Version: E031.24.00.15
Build Date: 07/31/14 Thu
Local Branch:
Remote Branch: quic/LNX.LA.3.5.2.2_rb1
Local Patches: NONE
Reconstruct Branch: AU_LINUX_android_LNX.LA.3.5.2.2_RB1.04.04.04.087.028 + NOTHING
04-04 18:30:41.206 13423-13423/com.carmel.roosterapp D/OpenGLRenderer﹕ Enabling debug mode 0
04-04 18:30:41.256 13423-13423/com.carmel.roosterapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41b417c0 time:8749586
04-04 18:31:01.896 13423-13423/com.carmel.roosterapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41b417c0 time:8770221
04-04 18:31:07.666 13423-13423/com.carmel.roosterapp I/Timeline﹕ Timeline: Activity_launch_request id:com.carmel.roosterapp time:8775992
04-04 18:31:07.956 13423-13423/com.carmel.roosterapp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@41c18a58 time:8776280
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp I/dalvikvm﹕ Could not find method javax.naming.Reference.get, referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.initializeFrom
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp W/dalvikvm﹕ VFY: unable to resolve virtual method 21843: Ljavax/naming/Reference;.get (Ljava/lang/String;)Ljavax/naming/RefAddr;
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0004
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp W/dalvikvm﹕ VFY: unable to find class referenced in signature (Ljavax/naming/Reference;)
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp E/dalvikvm﹕ Could not find class 'javax.naming.StringRefAddr', referenced from method com.mysql.jdbc.ConnectionPropertiesImpl$ConnectionProperty.storeTo
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp W/dalvikvm﹕ VFY: unable to resolve new-instance 2099 (Ljavax/naming/StringRefAddr;) in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0006
04-04 18:31:08.376 13423-13482/com.carmel.roosterapp D/dalvikvm﹕ DexOpt: unable to opt direct call 0x5555 at 0x14 in Lcom/mysql/jdbc/ConnectionPropertiesImpl$ConnectionProperty;.storeTo
04-04 18:31:08.546 13423-13482/com.carmel.roosterapp I/dalvikvm﹕ Could not find method java.lang.management.ManagementFactory.getThreadMXBean, referenced from method com.mysql.jdbc.MysqlIO.appendDeadlockStatusInformation
04-04 18:31:08.546 13423-13482/com.carmel.roosterapp W/dalvikvm﹕ VFY: unable to resolve static method 21024: Ljava/lang/management/ManagementFactory;.getThreadMXBean ()Ljava/lang/management/ThreadMXBean;
04-04 18:31:08.546 13423-13482/com.carmel.roosterapp D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0079
04-04 18:31:08.966 13423-13482/com.carmel.roosterapp I/System.out﹕ Connectie met Database!
04-04 18:31:09.126 13423-13482/com.carmel.roosterapp I/System.out﹕ test
04-04 18:31:11.146 13423-13423/com.carmel.roosterapp I/System.out﹕ No XML file!
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ java.io.FileNotFoundException: /data/data/com.carmel.roosterapp/files/rooster.xml: open failed: ENOENT (No such file or directory)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at libcore.net.url.FileURLConnection.connect(FileURLConnection.java:82)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:117)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at com.carmel.roosterapp.RoosterPagina.getRooster(RoosterPagina.java:135)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at com.carmel.roosterapp.RoosterPagina$DoInBackground.onPostExecute(RoosterPagina.java:370)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at com.carmel.roosterapp.RoosterPagina$DoInBackground.onPostExecute(RoosterPagina.java:155)
04-04 18:31:11.156 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5146)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
04-04 18:31:11.166 13423-13423/com.carmel.roosterapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
04-04 18:31:11.176 13423-13423/com.carmel.roosterapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
04-04 18:31:11.176 13423-13423/com.carmel.roosterapp W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-04 18:31:11.186 13423-13423/com.carmel.roosterapp W/System.err﹕ at libcore.io.Posix.open(Native Method)
04-04 18:31:11.186 13423-13423/com.carmel.roosterapp W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-04 18:31:11.186 13423-13423/com.carmel.roosterapp W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
04-04 18:31:11.186 13423-13423/com.carmel.roosterapp W/System.err﹕ ... 18 more
04-04 18:31:11.186 13423-13423/com.carmel.roosterapp I/Choreographer﹕ Skipped 129 frames! The application may be doing too much work on its main thread.
有人知道如何解决这个问题吗? 提前致谢
编辑: 整个编写代码是:(对不起,有些名称和 cmets 是荷兰语)
public class DoInBackground extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener
private ProgressDialog dialog;
private static final String TAG = "LOL123";
private boolean isNetworkAvailable(Context context)
ConnectivityManager connectivityManager
= (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
public boolean hasInternetAccess(Context context)
if (isNetworkAvailable(context))
try
HttpURLConnection urlc = (HttpURLConnection)
(new URL("http://clients3.google.com/generate_204")
.openConnection());
urlc.setRequestProperty("User-Agent", "Android");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 204 &&
urlc.getContentLength() == 0);
catch (IOException e)
Log.e(TAG, "Error checking internet connection", e);
else
Log.d(TAG, "No network available!");
return false;
protected void onPreExecute()
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Laden...");
dialog.setTitle("Rooster Updaten");
dialog.setIndeterminate(false);
dialog.setCancelable(true);
dialog.show();
@Override
protected Void doInBackground(Void... unused)
if (hasInternetAccess(getActivity()))
//haal llnr/docentcode op
SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, 0);
String code = settings.getString("Code", null);
String[] dagen;
dagen = new String[5];
dagen[0] = "maandag";
dagen[1] = "dinsdag";
dagen[2] = "woensdag";
dagen[3] = "donderdag";
dagen[4] = "vrijdag";
int aantDagen = dagen.length;
// bepaal de dag van de week
//Calendar c = Calendar.getInstance();
//int dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-2;
String url = "jdbc:mysql://carmelrooster.nl:3306/";
String dbName = "systeemnieuw";
String tblName = "leerlingrooster";
String driver = "com.mysql.jdbc.Driver";
String userName = "app";
String password = "password";
String Llnr = code;
try
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connectie met Database!");
//maak een nieuw DOM Document
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = docFactory.newDocumentBuilder();
//root elements
Document doc = documentBuilder.newDocument();
Element rootElement = doc.createElement("rooster");
doc.appendChild(rootElement);
Statement stmt = conn.createStatement();
for (int i = 0; i < aantDagen; i++ )
ResultSet res = stmt.executeQuery("SELECT u1,u2,u3,u4,u5,u6,u7,u8 FROM " + tblName +" WHERE dag = '" + dagen[i] + "' and llnr = '" + Llnr + "'");
while (res.next())
String eerste = res.getString("u1");
eerste = eerste.replace("<br />", " ");
eerste = eerste.replace("<c>", " ");
eerste = eerste.replace("</c>", " ");
String tweede = res.getString("u2");
tweede = tweede.replace("<br />", " ");
tweede = tweede.replace("<c>", " ");
tweede = tweede.replace("</c>", " ");
String derde = res.getString("u3");
derde = derde.replace("<br />", " ");
derde = derde.replace("<c>", " ");
derde = derde.replace("</c>", " ");
String vierde = res.getString("u4");
vierde = vierde.replace("<br />", " ");
vierde = vierde.replace("<c>", " ");
vierde = vierde.replace("</c>", " ");
String vijfde = res.getString("u5");
vijfde = vijfde.replace("<br />", " ");
vijfde = vijfde.replace("<c>", " ");
vijfde = vijfde.replace("</c>", " ");
String zesde = res.getString("u6");
zesde = zesde.replace("<br />", " ");
zesde = zesde.replace("<c>", " ");
zesde = zesde.replace("</c>", " ");
String zevende = res.getString("u7");
zevende = zevende.replace("<br />", " ");
zevende = zevende.replace("<c>", " ");
zevende = zevende.replace("</c>", " ");
String achtste = res.getString("u8");
achtste = achtste.replace("<br />", " ");
achtste = achtste.replace("<c>", " ");
achtste = achtste.replace("</c>", " ");
Element dag = doc.createElement(dagen[i]);
rootElement.appendChild(dag);
//set attribute to dag element
dag.setAttribute("id", String.valueOf(i));
//1e uur elements
Element uur1 = doc.createElement("eerste");
uur1.appendChild(doc.createTextNode(eerste));
dag.appendChild(uur1);
//2e uur elements
Element uur2 = doc.createElement("tweede");
uur2.appendChild(doc.createTextNode(tweede));
dag.appendChild(uur2);
//3e uur elements
Element uur3 = doc.createElement("derde");
uur3.appendChild(doc.createTextNode(derde));
dag.appendChild(uur3);
//4e uur elements
Element uur4 = doc.createElement("vierde");
uur4.appendChild(doc.createTextNode(vierde));
dag.appendChild(uur4);
//5e uur elements
Element uur5 = doc.createElement("vijfde");
uur5.appendChild(doc.createTextNode(vijfde));
dag.appendChild(uur5);
//6e uur elements
Element uur6 = doc.createElement("zesde");
uur6.appendChild(doc.createTextNode(zesde));
dag.appendChild(uur6);
//7e uur elements
Element uur7 = doc.createElement("zevende");
uur7.appendChild(doc.createTextNode(zevende));
dag.appendChild(uur7);
//8e uur elements
Element uur8 = doc.createElement("achste");
uur8.appendChild(doc.createTextNode(achtste));
dag.appendChild(uur8);
conn.close();
//write contents into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("http://xml.apache.org/xsltindent-amount", "2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(getActivity().getFilesDir(), "file.xml"));
System.out.println("test");
//Output to console for testing
//StreamResult result = new StreamResult(System.out);
catch (ParserConfigurationException pce)
pce.printStackTrace();
catch (TransformerException tfe)
tfe.printStackTrace();
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
catch (java.lang.InstantiationException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();
return null;
整个读取的代码是:
public String[] getRooster() throws Exception
String[] dagen;
dagen = new String[5];
dagen[0] = "maandag";
dagen[1] = "dinsdag";
dagen[2] = "woensdag";
dagen[3] = "donderdag";
dagen[4] = "vrijdag";
int aantDagen = dagen.length;
// bepaal de dag van de week
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK)-2;
File XmlFile = new File(getActivity().getFilesDir(), "file.xml");
if (!XmlFile.exists())
System.out.println("No XML file!");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(XmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(dagen[dayOfWeek]);
Node nNode = nList.item(0);
String dag = nNode.getNodeName().substring(0,1).toUpperCase() + nNode.getNodeName().substring(1);
Element eElement = (Element) nNode;
String eerste = ("1e : " + eElement.getElementsByTagName("eerste").item(0).getTextContent());
String tweede = ("2e : " + eElement.getElementsByTagName("tweede").item(0).getTextContent());
String derde = ("3e : " + eElement.getElementsByTagName("derde").item(0).getTextContent());
String vierde = ("4e : " + eElement.getElementsByTagName("vierde").item(0).getTextContent());
String vijfde = ("5e : " + eElement.getElementsByTagName("vijfde").item(0).getTextContent());
String zesde = ("6e : " + eElement.getElementsByTagName("zesde").item(0).getTextContent());
String zevende = ("7e : " + eElement.getElementsByTagName("zevende").item(0).getTextContent());
String achtste = ("8e : " + eElement.getElementsByTagName("achste").item(0).getTextContent());
return new String[] dag, eerste, tweede, derde, vierde, vijfde, zesde, zevende, achtste;
【问题讨论】:
您保存到 rooster.xml 并从 file.xml 中读取。对吗? StreamResult 是做什么的?您的保存代码中没有任何内容可以创建文件。并且与源文档没有任何联系。 @peter_budo 抱歉,我刚刚更改了它,因为我的文件名等是荷兰语,但它们的名称相同 @greenapps 抱歉,这不是完整的代码,我已经用完整的读/写代码编辑了我的帖子 这没什么。你为什么发布所有不相关的代码?您没有对 rooster.xml 上的新 File 对象执行任何操作。除非 StreamResult 用它做点什么。因此,我问你 StreamResult 做了什么。我看不出与你的文件有任何联系。所以你什么也没有保存。 【参考方案1】:如果您使用模拟器,可能您没有为模拟器设备设置内存。 进入 AVD Manager,选择你的设备,点击“编辑”,填写“内部存储”。
还有你要加
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in manifest.xml
【讨论】:
我没有运行模拟器,我应该在清单中添加什么?【参考方案2】:感谢 greenapps,我找到了解决方案。 正如他所说,我的 streamresult 没有做任何事情。
我通过替换来修复它
File XmlFile = new File(getActivity().getFilesDir(), "file.xml");
与
FileInputStream XmlFile = getActivity().openFileInput("file.xml");
替换
StreamResult result = new StreamResult(new File(getActivity().getFilesDir(), "rooster.xml"));
与
StreamResult result = new StreamResult(getActivity().openFileOutput("rooster.xml", getActivity().MODE_PRIVATE));
【讨论】:
以上是关于Android上的java.io.FileNotFoundException,打开失败:ENOENT(没有这样的文件或目录)的主要内容,如果未能解决你的问题,请参考以下文章
android.Android() 上的 QPython 错误
ListView 上的 LongPress 与 Android 上的超链接
Android 9 上的 android.database.sqlite.SQLiteCantOpenDatabaseException