每次执行应用程序时,XML文件只会创建一次

时间:2013-05-11 08:33:34

标签: android xml file

我正在尝试在SD卡上生成XML文件。我在Manifest文件中添加了用户权限,但是当我通过usb插入手机并在eclipse中运行应用程序时,第一次创建了xml文件,但是当我再次通过手机或通过eclipse运行应用程序时,它就没有获得创建。要创建文件,我将通过USB重新连接手机,并且只创建一次。请帮我。

public class MainActivity extends Activity {
    TextView  myTextView;
     EditText E1;
     EditText E2;
     EditText E3;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b1= (Button) findViewById(R.id.button1);
    Button b3= (Button) findViewById(R.id.button3);
    E1 = (EditText) findViewById(R.id.editText1);
    E2 = (EditText) findViewById(R.id.editText2);
    E3 = (EditText) findViewById(R.id.editText3);
    b1.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) { 

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    try {
         docBuilder = docFactory.newDocumentBuilder();
         Document doc = docBuilder.newDocument();
         Element rootElement = doc.createElement("Class");
         doc.appendChild(rootElement);
         Element student = doc.createElement("Student");
         rootElement.appendChild(student);
         Element firstname = doc.createElement("firstname");                                   firstname.appendChild(doc.createTextNode(E1.getText().toString()));
            student.appendChild(firstname);

         Element Email = doc.createElement("Email");
         Email.appendChild(doc.createTextNode(E2.getText().toString()));
         student.appendChild(Email);

         Element Roll = do c.createElement("Roll_No");
         Roll.appendChild(doc.createTextNode(E3.getText().toString()));
         student.appendChild(Roll);
         TransformerFactory transformerFactory = TransformerFactory.newInstance();
         Transformer transformer = transformerFactory.newTransformer();
         DOMSource source = new DOMSource(doc);

         File FF=new File(Environment.getExternalStorageDirectory()+"//new.xml");
         try {
        FF.createNewFile();
           } catch (IOException e) {
           e.printStackTrace();
           }

         StreamResult result = new StreamResult(FF);
         transformer.transform(source, result);  
          }
         catch (ParserConfigurationException e) {   
          e.printStackTrace();
          }
         catch (TransformerException e) {               
          e.printStackTrace();}  
          Toast.makeText(getApplicationContext(),       Environment.getExternalStorageDirectory().toString(), Toast.LENGTH_LONG).show();                                }
    });

   b3.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
           // Close the application
           finish(); }});
   }
}

2 个答案:

答案 0 :(得分:1)

你正在捕捉异常并忽略它。这不是好习惯,这就是为什么你不知道错误是什么:

        File FF=new File(Environment.getExternalStorageDirectory()+"//new.xml");
        try {
            FF.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

FF.createNewFile();

正在抛出异常,因为如果新文件已存在,则无法创建新文件。首先删除它或打开它以覆盖它。

http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileAlreadyExistsException.html

请改用此代码:

File FF=new File(Environment.getExternalStorageDirectory()+"//new.xml");
try {
    if (FF.exists());
        FF.delete();
    FF.createNewFile();
} catch (IOException e) {
    // Handle the error here! don't ignore it. Either throw the exception all the way, or log it, or something.
    throw e;
}

答案 1 :(得分:0)

我认为问题出在FF.createNewFile(); 。如果文件已经存在,则此函数返回false,这就是为什么它只创建一个。

点击此处:http://www.tutorialspoint.com/java/io/file_createnewfile.htm