<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ouling.ex_file" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ex_file" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="file_ex" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/editText_in"
android:text=""></EditText>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/In_save" android:text="保存文本"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/In_save_sd" android:text="保存到SD卡"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/editText_out"
android:text=""></EditText>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:id="@+id/out_put" android:text="读取文本"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:id="@+id/out_put_sd" android:text="读取SD卡文本"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="选择复制的文件:" />
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button android:id="@+id/save_to_sd" android:text="确定复制"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
package com.ouling.ex_file;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class ex_file extends Activity {
/** Called when the activity is first created. */
private EditText et_input, et_output;
private Button btn_save, btn_save_sd, btn_out, btn_out_sd, btn_save_tosd;
private Spinner m_spinner;
private String FILENAME = "ex_file.txt";
private Context context;
private static ArrayList<String> files;
private static ArrayList<String> filenames;
private ArrayAdapter<String> madpter;
private String copy_path = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = (Context) this;
et_input = (EditText) findViewById(R.id.editText_in);
et_output = (EditText) findViewById(R.id.editText_out);
btn_save = (Button) findViewById(R.id.In_save);
btn_save_sd = (Button) findViewById(R.id.In_save_sd);
btn_out = (Button) findViewById(R.id.out_put);
btn_out_sd = (Button) findViewById(R.id.out_put_sd);
// 保存文件
OnClickListener save_listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (et_input.getText().toString().equals("")) {
Toast.makeText(context, "请输入文本!", 1000).show();
return;
}
switch (v.getId()) {
case R.id.In_save:
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);// 以私有模式创建文件
String text = et_input.getText().toString();
fos.write(text.getBytes());// 写入数据
fos.flush(); // 将缓冲区剩余数据写入文件
fos.close(); // 关闭FileOutputStream
Toast.makeText(context, "保存文件成功", 1000).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
break;
case R.id.In_save_sd:
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
FileOutputStream ostream = null;
try {
File myfile = new File(Environment
.getExternalStorageDirectory().getPath(),
FILENAME);
if (!myfile.exists()) {
myfile.createNewFile();
}
ostream = new FileOutputStream(myfile, true);
String text = et_input.getText().toString();
ostream.write(text.getBytes());
Toast.makeText(context, "保存文件到sd卡成功", 1000).show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ostream != null) {
try {
ostream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
break;
default:
break;
}
}
};
btn_save.setOnClickListener(save_listener);
btn_save_sd.setOnClickListener(save_listener);
// 读取
OnClickListener output_listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FileInputStream inStream = null;
ByteArrayOutputStream stream = null;
try {
switch (v.getId()) {
case R.id.out_put:
inStream = openFileInput(FILENAME);
break;
case R.id.out_put_sd:
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File sd_file = new File(Environment
.getExternalStorageDirectory().getPath(),
FILENAME);
if (!sd_file.exists()) {
Toast.makeText(context, "未找到该文件", 1000).show();
return;
}
inStream = new FileInputStream(sd_file);
}
default:
break;
}
stream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = -1;
while ((length = inStream.read(buffer)) != -1) {
stream.write(buffer, 0, length);
}
stream.close();
inStream.close();
et_output.setText(stream.toString());
Toast.makeText(context, "获取文本成功!", 1000).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, "文件未找到", 1000).show();
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
return;
} finally {
try {
if (stream != null)
stream.close();
if (inStream != null)
inStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
btn_out.setOnClickListener(output_listener);
btn_out_sd.setOnClickListener(output_listener);
m_spinner = (Spinner) findViewById(R.id.spinner1);
// 获得程序目录中的所有文件路径
files = new ArrayList<String>();
filenames=new ArrayList<String>();
String path = getApplication().getFilesDir().getParentFile().getPath();
getfiles(path);
madpter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, filenames);
madpter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_spinner.setAdapter(madpter);
// 选择监听
m_spinner
.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
// 获得需复制文件的全路径
copy_path = files.get(position);
// 设置显示选择项
parent.setVisibility(View.VISIBLE);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
btn_save_tosd = (Button) findViewById(R.id.save_to_sd);
btn_save_tosd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.save_to_sd) {
if (Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
if (!copy_path.equals("")) {
String sd_filename = copy_path.substring(copy_path
.lastIndexOf('/'));
save_to_sd(copy_path, Environment
.getExternalStorageDirectory().getPath()
+ "/" + sd_filename);
}
}
}
}
});
}
private void getfiles(String path) {
File file = new File(path);
if (file.list() == null) {
return;
}
int file_num = file.list().length;
for (int i = 0; i < file_num; i++) {
File child_file = new File(path, file.list()[i]);
if (child_file.isDirectory()) {
String child_path = child_file.getPath();
getfiles(child_path);
} else if (child_file.isFile()) {
System.out.println(child_file.getName());
files.add(child_file.getPath());
filenames.add(child_file.getName());
}
}
}
// 将数据保存到sd卡
private void save_to_sd(String from_path, String sd_path) {
try {
File fromFile = new File(from_path);
if (!fromFile.exists()) {
System.out.println("程序中不存在" + from_path);
return;
}
File sd_File = new File(sd_path);
InputStream in = new FileInputStream(fromFile);
OutputStream out = new FileOutputStream(sd_File);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
Toast.makeText(context, "完成文件复制", 1000).show();
} catch (Exception e) {
// TODO: handle exception
System.out.println("save to sd " + e.toString());
}
}
}