package com.android.file;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AndroidtestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setText("点击");
setContentView(btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File directory = new File("/mnt/sdcard/mp3/");
File file = new File("/mnt/sdcard/mp3/hear.mp3");
if(!directory.exists()){
directory.mkdir();//没有目录先创建目录
}
try {
if(file.createNewFile()){
Toast.makeText(AndroidtestActivity.this, "创建文件成功", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AndroidtestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button btn = new Button(this);
btn.setText("点击");
setContentView(btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
File directory = new File("/mnt/sdcard/mp3/");
File file = new File("/mnt/sdcard/mp3/hear.mp3");
if(!directory.exists()){
directory.mkdir();//没有目录先创建目录
}
try {
if(file.createNewFile()){
Toast.makeText(AndroidtestActivity.this, "创建文件成功", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>