[Android]流文件
代码部分
1. MainAcitivity
package home.lee.iostreamdemo1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
private static final String FILE_PATH= "/data/data/home.lee.iostreamdemo1/";
private static final String FILE_NAME = "iosfile.txt";
private static final String TEXT_ENCODING = "UTF-8";
private static final String TAG = "ERROR";
private static final String FILE_NAME_OS = "openIOSfile.txt";
private TextView tv;
private String textContent;
private EditText ev;
private String writeFromEditText;
private FileOutputStream os;
private FileInputStream is;
private byte[] isByte;
private int isLength;
private Button saveBtn;
private Button readBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ev = (EditText)findViewById(R.id.edittext1);
tv = (TextView)findViewById(R.id.textview1);
saveBtn = (Button)findViewById(R.id.save);
saveBtn.setOnClickListener(this);
readBtn = (Button)findViewById(R.id.read);
readBtn.setOnClickListener(this);
/*
try {
//创建文件
File file = new File(FILE_PATH, FILE_NAME);
file.createNewFile();
//写出操作
FileOutputStream fileOS = new FileOutputStream(file);
String writeContent = "你好,初次写入,请核实!";
//把字符串内容转换成字节数组形式,然后写出
fileOS.write(writeContent.getBytes());
fileOS.close();
//读入操作
FileInputStream fileIS = new FileInputStream(file);
//文件内容长度
int length = (int)file.length();
//根据文件内容长度,创建字节数组
byte[] content = new byte[length];
//读数据
fileIS.read(content, 0, length);
textContent = EncodingUtils.getString(content, TEXT_ENCODING);
fileIS.close();
} catch (IOException e) {
Log.e(TAG, e.toString());
//e.printStackTrace();
}
*/
//tv.setText(textContent);
//setContentView(tv);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.save:
fileIOSwrite();
break;
case R.id.read:
fileIOSread();
break;
default:
break;
}
}
private void fileIOSread() {
//读文件
try {
//InputStream is = this.getResources().openRawResource(R.raw.res_raw);
is = openFileInput(FILE_NAME_OS);
isByte = new byte[1024];
//把读取到的内容,放到
isLength = is.read(isByte);
this.setTitle("文件长度为" + isLength);
//把字节内容转换成字符串形式显示
String readContent = new String(isByte);
tv.setText(readContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void fileIOSwrite() {
//写文件-如果不存在,就会创建-路径为:/data/data/package_name/files/里
try {
os = openFileOutput(FILE_NAME_OS, MODE_PRIVATE);
writeFromEditText = ev.getText().toString();
os.write(writeFromEditText.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:gravity="center"
android:text="文本存取"
android:textColor="#00F"
android:textSize="21sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="122dp"
android:layout_margin="10dp"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.72"
android:fadingEdge="vertical"
android:scrollbars="vertical" >
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:layout_weight="0.69" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="horizontal" >
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="写出"
android:textSize="17sp" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="读入"
android:textSize="17sp" />
</LinearLayout>
</LinearLayout>
3. 结果
下载代码资源:代码