2014-03-12 4 views
0

나는이 코드를 사용하여 새 xml 파일을 만듭니다. 하지만 이미 존재하는지 확인한 후에 다른 파일에 넣고 싶습니다.android를 사용하여 새 파일 만들기

 private void XmlSms() { 

      File newxmlfile = new File(Environment.getExternalStorageDirectory()+ "/SmsFile.xml"); 
      try 
     { 
     Log.v(BackupFragment.this.getClass().getName(), "create file:"+ newxmlfile.createNewFile()); 
     }  
     catch (IOException e) 
      { 
    Log.e("IOException", "exception in createNewFile() method"); 
    } 
      FileOutputStream fileos = null; 
      try 
      { 
    fileos = new FileOutputStream(newxmlfile); 
    } 
      catch (FileNotFoundException e) 
      { 
    Log.e("FileNotFoundException", "can't create FileOutputStream"); 


} 
+3

질문은 어디에 있습니까? 당신은 이미 무엇을 시도 했습니까 (그리고 무엇이 효과가 없습니다)? –

+0

해당 코드를 사용하면 xml 파일이 루트에 직접 만들어집니다. 새 파일 (예 : newfile)을 만들고 xml 파일 (SmsFile)을 넣고 싶습니다. 경로가/newfile/SmsFile/ – Marya

+1

이니 디렉토리를 만들고 파일을 넣으시겠습니까? –

답변

1

당신은 그것을 당신의 XML 파일 작성 전에 "이를 새"디렉토리, 를 만들 mkdirs()를 사용해야합니다, 지금

File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"newfile"); 
directory.mkdirs(); 

와이 새 디렉토리에 XML 파일을 생성 할 수 있습니다 :

File newxmlfile= new File(directory , "SmsFile.xml); 
newxmlfile.createNewFile() 
+0

작동한다. thkx :-) – Marya

관련 문제