2013-05-11 49 views
0

我试图在SD卡上生成XML文件。我已经在Manifest文件中添加了用户权限,但是当我通过usb插入手机并在eclipse中运行应用程序时,第一次创建了xml文件,但是当我再次通过手机或通过eclipse运行应用程序时,它不能获取创建。为了创建文件,我必须通过USB重新连接手机,并且只能创建一次。请帮帮我。XML文件只创建一次而不是每次执行应用程序时

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(); }}); 
    } 
} 

回答

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; 
} 
+0

我试图通过你提供的代码,现在该文件是不是所有得到建立。 – user2044296 2013-05-11 10:13:41

+0

是否引发异常?请提供logcat。 – tbkn23 2013-05-11 10:59:50

相关问题