java andorid读取文件破坏文件

时间:2019-01-30 08:50:47

标签: java android android-file

我的内存中有一个文件。我必须从该文件读取数据。我不知道为什么我的文件损坏了。这是我读取文件的方法:

public synchronized static LocomotiveKeyDAO getLocomotive(File file, File file2) throws IOException, XmlPullParserException {
    LocomotiveKeyDAO locomotiveKeyDAO = new LocomotiveKeyDAO();
    InputStream input;
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

    if(file != null && file.exists()){
        input = new FileInputStream(file);
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new InputStreamReader(input));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if ((eventType == XmlPullParser.START_TAG)) {
                String tagName = xpp.getName();
                Log.e("tagName", tagName);
                if (tagName.equalsIgnoreCase("NumerStacji")) {
                    locomotiveKeyDAO.setLocomotive(xpp.nextText());
                }

            }
            eventType = xpp.next();
        }
        input.close();
    }
    if(file2 != null && file2.exists()){
        input = new FileInputStream(file2);
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp = factory.newPullParser();
        xpp.setInput(new InputStreamReader(input));
        int eventType = xpp.getEventType();
        eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if ((eventType == XmlPullParser.START_TAG)) {
                String tagName = xpp.getName();
                Log.e("tagName", tagName);
                if (tagName.equalsIgnoreCase("key")) {
                    locomotiveKeyDAO.setKey(xpp.nextText());
                }
            }
            eventType = xpp.next();
        }
        input.close();
    }
    return locomotiveKeyDAO;
}

此方法有时会损坏文件。我不知道为什么

0 个答案:

没有答案