获取文件内容时出错?不知道为什么

时间:2017-04-14 03:55:13

标签: android android-file

我在我的android设备中创建了一个输出文件,但是当我第二次打开它时,我得到了这个错误,当得到文件内容错误时,我可以在调试环境下看到这个文件内容,任何人都知道我发生了什么事输出文件?任何回复都有义务

    private ForeignObjectParser parser;
    private List<ForeignObjectModel> foreignObjects;
    @Override
            public void onClick(View v) {
                try {
                    InputStream is = getAssets().open("ForeignObjects.xml");
                    parser = new ForeignObjectParserImp();
                    foreignObjects = parser.parse(is);
                    String xml = parser.serialize(foreignObjects);  //
                    FileOutputStream fos = new FileOutputStream(new File(getExternalFilesDir(null), "foreign.html"));
                    fos.write(xml.getBytes("UTF-8"));
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
            }

public class ForeignObjectParserImp implements ForeignObjectParser  {
@Override
public List<ForeignObjectModel> parse(InputStream is) throws Exception {
    List<ForeignObjectModel> foreignObjects = null;
    ForeignObjectModel foreignObject = null;


    XmlPullParser parser = Xml.newPullParser(); 
    parser.setInput(is, "UTF-8");               

    int eventType = parser.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT) {
        switch (eventType) {
            case XmlPullParser.START_DOCUMENT:
                foreignObjects = new ArrayList<ForeignObjectModel>();
                break;
            case XmlPullParser.START_TAG:
                if (parser.getName().equals("foreignObject")) {
                    foreignObject = new ForeignObjectModel();
                } else if (parser.getName().equals("id")) {
                    eventType = parser.next();
                    foreignObject.setId(parser.getText());
                } else if (parser.getName().equals("value")) {
                    eventType = parser.next();
                    foreignObject.setValue(parser.getText());
                } else if (parser.getName().equals("x")) {
                    eventType = parser.next();
                    foreignObject.setX(parser.getText());
                } else if (parser.getName().equals("y")) {
                    eventType = parser.next();
                    foreignObject.setY(parser.getText());
                } else if (parser.getName().equals("width")) {
                    eventType = parser.next();
                    foreignObject.setWidth(parser.getText());
                } else if (parser.getName().equals("height")) {
                    eventType = parser.next();
                    foreignObject.setHeight(parser.getText());
                } else if (parser.getName().equals("font-size")) {
                    eventType = parser.next();
                    foreignObject.setFontsize(parser.getText());
                } else if (parser.getName().equals("font")) {
                    eventType = parser.next();
                    foreignObject.setFont(parser.getText());
                } else if (parser.getName().equals("alignment")) {
                    eventType = parser.next();
                    foreignObject.setAlignment(parser.getText());
                } else if (parser.getName().equals("origzoom")) {
                    eventType = parser.next();
                    foreignObject.setOrigzoom(parser.getText());
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getName().equals("foreignObject")) {
                    foreignObjects.add(foreignObject);
                    foreignObject = null;
                }
                break;
        }
        eventType = parser.next();
    }
    return foreignObjects;
}

@Override
public String serialize(List<ForeignObjectModel> foreignObjects) throws Exception {
    XmlSerializer serializer = Xml.newSerializer(); 
    StringWriter writer = new StringWriter();
    serializer.setOutput(writer);   
    serializer.startDocument("UTF-8", true);
    serializer.startTag("", "foreignObjects");
    for (ForeignObjectModel foreignObject : foreignObjects) {
        serializer.startTag("", "foreignObject");
        serializer.attribute("", "id", foreignObject.getId() + "");
        serializer.attribute("", "x", foreignObject.getX() + "");
        serializer.attribute("", "y", foreignObject.getY() + "");
        serializer.attribute("", "width", foreignObject.getWidth() + "");
        serializer.attribute("", "height", foreignObject.getHeight() + "");

        serializer.startTag("","div");
        serializer.attribute("","id",foreignObject.getId() + "Div" + "");
        serializer.attribute("", "style", foreignObject.getWidth() + "");


        serializer.startTag("","span");
        serializer.attribute("","id",foreignObject.getId() + "Span");
        serializer.attribute("","style",foreignObject.getFont() + "");
        serializer.text(foreignObject.getValue());
        serializer.endTag("","span");

        serializer.endTag("","div");

        serializer.endTag("", "foreignObject");
    }
    serializer.endTag("", "foreignObjects");
    serializer.endDocument();

    return writer.toString();
}

}

public interface ForeignObjectParser {
    /**
     * @param is
     * @return
     * @throws Exception
     */
    public List<ForeignObjectModel> parse(InputStream is) throws Exception;

    /**
     * @param books
     * @return
     * @throws Exception
     */
    public String serialize(List<ForeignObjectModel> books) throws Exception;
}

因为我打开了一次,所以我认为代码没有问题,但我可能错了。

1 个答案:

答案 0 :(得分:0)

我认为它的文件太大,内存不足。发布您的日志或邮寄代码。