文件读写没什么好说的,但是关于SD卡的文件读写,也是第一次。
1、把所有的进行实际操作的封装成一个类;
2、在MainActivity中实例化对象然后去用。
- layout布局文件中,一个输入框,两个按钮,一个文本显示
- 记得在配置文件中加入读写文件的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
SDFile.java
package com.sdfile;
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 android.content.Context;
import android.os.Environment;
public class SDFile {
/**
* 记得在配置文件中加入SD卡的文件读写权限
*/
private Context context;
public SDFile(Context context) {
this.context = context;
}
public SDFile() {
}
String sdRootPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
//新建文件夹,传入的字符串是文件名
public File crateFodler(String dir) {
File dirFile = new File(sdRootPath + File.separator
+ dir + File.separator);
dirFile.mkdirs();
return dirFile;
}
public String getFile(String fileName) {
FileInputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory()+"/.jiangjiwei",
fileName);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
try {
inputStream = new FileInputStream(file);
int len = -1;
byte[] data = new byte[1024];
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(outputStream.toByteArray());
}
public boolean saveContent(String fileName, String content) {
boolean flag = false;
FileOutputStream fileOutputStream = null;
File file = new File(Environment.getExternalStorageDirectory()+"/.jiangjiwei",
fileName);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
flag = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return flag;
}
}
MainActivity.java
package com.sdfile;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText editText;
private Button saveButton, readButton;
private TextView textView;
private String fileNameString = "text.txt";
private String contextString;
private SDFile sdFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edittext);
saveButton = (Button) findViewById(R.id.save);
readButton = (Button) findViewById(R.id.read);
textView = (TextView) findViewById(R.id.textview);
sdFile = new SDFile(MainActivity.this);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
contextString = editText.getText().toString();
sdFile.crateFodler(".jiangjiwei");
sdFile.saveContent(fileNameString, contextString);
makeText("save Is Done");
makeText(Environment.getExternalStorageDirectory() + "");
}
});
readButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
contextString = sdFile.getFile(fileNameString);
textView.setText(contextString);
makeText("read Is Done");
}
});
}
void makeText(String sss) {
// Toast.makeText(this, sss, 0).show();
Log.e("" + "SDFile", sss);
}
}