FileOutputStream在保存时添加额外信息

时间:2014-01-06 15:16:14

标签: java android

在内部存储器上创建文件时遇到问题。我将我的信息存储在SQLite中,然后当我去提取数据时,我正在提取它然后使用XMLSerializer编写它。好吧,当我调试应用程序时,模拟器上的信息是正确和准确的,但是一旦我将应用程序放在我的手机上运行,​​该文件就会附加额外的信息。

这是我正在使用的XMLSerializer文件:

public class WriteRecordsToXML {


public static String createRecord()
{
    StringWriter writer = new StringWriter();
    try 
    {   
        XmlSerializer xmlSerializer = Xml.newSerializer();
        //StringWriter writer = new StringWriter();

        xmlSerializer.setOutput(writer);
        //xmlSerializer.setOutput(fileOS, "UTF-8");

        xmlSerializer.startDocument("UTF-8", true);

        xmlSerializer.startTag("", "Boats");

        Boats boats = new Boats();

        for(Boats boat : boats.getBoatList())
        {
            xmlSerializer.startTag("", "boat");
            xmlSerializer.attribute("", "boatId", boat.getBoatID());

            xmlSerializer.startTag("", "showId");
            xmlSerializer.text(boat.getBoatShowID());
            xmlSerializer.endTag("", "showId");

            xmlSerializer.startTag("", "showDate");
            xmlSerializer.text(boat.getShowDate());
            xmlSerializer.endTag("", "showDate");

            xmlSerializer.startTag("", "showLocation");
            xmlSerializer.text(boat.getShowLocation());
            xmlSerializer.endTag("", "showLocation");

            xmlSerializer.startTag("", "personName");
            xmlSerializer.text(boat.getPersonName());
            xmlSerializer.endTag("", "personName");

            xmlSerializer.startTag("", "boatName");
            xmlSerializer.text(boat.getBoatName());
            xmlSerializer.endTag("", "boatName");

            xmlSerializer.startTag("", "boatLength");
            xmlSerializer.text(boat.getBoatLength());
            xmlSerializer.endTag("", "boatLength");

            for(BoatOptions boatOption : BoatOptions.boatOptionList)
            {
                if(boatOption.getBoatID().equals(boat.getBoatID()))
                {
                    xmlSerializer.startTag("", "boatOption");
                    xmlSerializer.text(boatOption.getBoatOption());
                    xmlSerializer.endTag("", "boatOption");
                }
            }

            xmlSerializer.endTag("", "boat");               
        }

        xmlSerializer.endTag("", "Boats");

        xmlSerializer.endDocument();
        xmlSerializer.flush();
        //return writer.toString().getBytes();
    } 
    catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return writer.toString();
}

}

就像我上面所说,当我检查编写器对象时,它正确地保存了我的信息。

这是我保存提取的信息的地方,我尝试了很多不同的东西,但没有什么能解决它:

public void extractData(View view)
    {
        File file = new File(folder, File.separator + "BoatShow_" + boatShowID + ".xml");
        String fileName = "BoatShow_" + boatShowID + ".xml";

    try {
        if(file.exists())
        {
            file.delete();
        }
        file.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    boolean isExtracted = false;

    FileOutputStream fileOs = null;
    String content = "";

    try {

        Boats.clearBoatList();
        BoatOptions.boatOptionList.clear();
        db.getBoatRecords(boatShowID);
        fileOs = new FileOutputStream(file);
        String xmlFile = WriteRecordsToXML.createRecord();
        fileOs.write(xmlFile.getBytes());
        fileOs.close();

        FileInputStream fileIs = openFileInput(fileName);
        byte[] input = new byte[fileIs.available()];
        while(fileIs.read(input) != -1)
        {
            content += new String(input);
        }

        if(content.equals(xmlFile))
        {
            isExtracted = true;
        }
        else
        {
            isExtracted = false;
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.activity_custom_layout);
    dialog.setTitle("Data Extraction");

    Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
    TextView dialogText = (TextView)dialog.findViewById(R.id.customTextView);
    if(isExtracted)
    {
        dialogText.setText("Data successfully extracted for Boat Show: " + boatShowID);
    }
    else
    {
        dialogText.setText("Data did not extract for Boat Show: " + boatShowID);
        file.delete();
    }


    dialogButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0) {
            dialog.dismiss();
        }               
    });

    dialog.show();
}

例如,如果我的数据库中有2条记录:

1, 2, 1/3/2014, Missouri, Matt, Flat Boat, < 16 Feet, Other
2, 2, 1/3/2014, Missouri, Matt, Yacht, > 60 Feet, Johnson

在我的文件BoatShow_2.xml中提取的数据应如下所示:

1 2 1/3/2014 Missouri Matt Flat Boat
< 16 Feet
Other
    2 2 1/3/2014 Missouri Matt Yacht
    `> 60 Feet Johnson `

当我打开文件时,第一个boatOption也会被添加到第二艘船上(仅当我直接在手机上使用该应用程序时)

2 2 1/3/2014 Missouri Matt Yacht
Other `> 60 Feet Johnson `

0 个答案:

没有答案