-5

我想在android中创建一个新的xml文件。我的代码如下所示。它不起作用

File newxmlfile = new File("C:/Users/yunus.oksuz/Desktop/xmlFile.xml");
try
{
newxmlfile.createNewFile();
Toast msg = Toast.makeText(MainActivity.this, "file was created", Toast.LENGTH_LONG);
msg.show();
}
catch(IOException e)
{
Log.e("IOException", "exception in createNewFile() method");
}
4

2 回答 2

0

以下是创建 xml 文件的方法。

File f = new File(Environment.getExternalStorageDirectory() + "/yourxmlfilename.xml");

// If file does not exist, clear log count
if(!f.exists())
    f.createNewFile();
于 2012-10-12T12:19:49.533 回答
0

First thing to know, you cannot even think about having a file in your PC and trying to refer it to your Android application project. They both are totally different.

let me explain the reason.

Consider you have the app installed in a phone, how do you think you can simply access your PC to which you are no more connected. This is not a regular java project you are working on. Android is totally different.

I will tell how it works. Open a emulator and wait for it to load. now,

Go to DDMs-> File Explorer

You will be able to see a folder by name "mnt" or "sdcard". Click on it. And now on the top right corner you will have three icons , one to push a file to sdcard and one to delete from sdcard and also one to pull a file from sdcard.

So click on the push a file icon and select your file and copy it to sdcard.

Now you have your file in sdcard. here you have to learn how to read a file from sdcard.

And I am sure there are so many examples available on internet on how you can read a file from sdcard.

Hope this will help you to get it started.

于 2012-10-12T12:15:01.517 回答