当前位置: 首页 > 知识库问答 >
问题:

JAVA伊奥。Android上的FileNotFoundException,打开失败:eNote(没有这样的文件或目录)

龚浩宕
2023-03-14

我正试图为我的应用程序保存和编写一个包含一些数据的XML文件,但当它试图打开该文件时,它给出了一个java。伊奥。FileNotFoundException。

我用来保存XML文件的代码是:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-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);

它给出的逻辑是:

    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.

有人知道如何解决这个问题吗?提前谢谢

编辑:整个写代码是:(抱歉,有些名字和注释是荷兰语)

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/xslt}indent-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};

共有2个答案

黎阳冰
2023-03-14

多亏了绿色应用,我找到了我的解决方案。正如他所说,我的流结果什么也没做。

我把它换了

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));
公冶光亮
2023-03-14

如果使用模拟器,可能没有为模拟器设备设置内存。转到AVD管理器,选择您的设备,单击“编辑”,填写“内部存储”。

你还得加上

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in manifest.xml
 类似资料: