读取外部存储的文件数据Environment.getExternalStorageDirectory()
2015-10-06 11:25
459 查看
MainActivity .java
package com.example.debug;
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.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String filename="test";
private EditText et;
private TextView show;
File sdcard=Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
show=(TextView) findViewById(R.id.show);
findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.txt");
if(myfile.exists()){
try {
FileInputStream fis=new FileInputStream(myfile);
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
char []input=new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String inString=new String(input);
show.setText(inString);
} catch (FileNotFoundException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
}
}
});
findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.txt");
if(!sdcard.exists()){
Toast.makeText(MainActivity.this, "当前设备不存在sd卡", Toast.LENGTH_LONG).show();
return;
}
try {
myfile.createNewFile();
Toast.makeText(MainActivity.this, "文件创建完成", Toast.LENGTH_LONG).show();
FileOutputStream fos=new FileOutputStream(myfile);
OutputStreamWriter osw=new OutputStreamWriter(fos);
osw.write(et.getText().toString());
osw.flush();
osw.close();
fos.close();
Toast.makeText(MainActivity.this, "文件已经写入完成", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要保存的数据"
/>
<Button
android:id="@+id/writeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="写入数据" />
<Button
android:id="@+id/readBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取数据" />
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="这里显示读到的内容"
/>
</LinearLayout>
添加权限:
package com.example.debug;
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.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
String filename="test";
private EditText et;
private TextView show;
File sdcard=Environment.getExternalStorageDirectory();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
show=(TextView) findViewById(R.id.show);
findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.txt");
if(myfile.exists()){
try {
FileInputStream fis=new FileInputStream(myfile);
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
char []input=new char[fis.available()];
isr.read(input);
isr.close();
fis.close();
String inString=new String(input);
show.setText(inString);
} catch (FileNotFoundException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
}
}
});
findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File myfile=new File(sdcard, "Thismyfile.txt");
if(!sdcard.exists()){
Toast.makeText(MainActivity.this, "当前设备不存在sd卡", Toast.LENGTH_LONG).show();
return;
}
try {
myfile.createNewFile();
Toast.makeText(MainActivity.this, "文件创建完成", Toast.LENGTH_LONG).show();
FileOutputStream fos=new FileOutputStream(myfile);
OutputStreamWriter osw=new OutputStreamWriter(fos);
osw.write(et.getText().toString());
osw.flush();
osw.close();
fos.close();
Toast.makeText(MainActivity.this, "文件已经写入完成", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要保存的数据"
/>
<Button
android:id="@+id/writeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="写入数据" />
<Button
android:id="@+id/readBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取数据" />
<TextView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="这里显示读到的内容"
/>
</LinearLayout>
添加权限:
相关文章推荐
- poj 3468 A Simple Problem with Integers (线段树区间更新 + 树状数组区间更新)
- 《程序员面试金典》--平衡二叉树的检测
- c++ for_each
- thinkphp学习笔记1
- ubuntu下C,C++,opencv cuda编程
- Android 在ScorllView中ListView和GridView冲突解决方案
- js中if判断表达式
- 安装了win10之后 再安装sql server 2008 r2提示不兼容
- C++ const关键字
- soket概览
- java拾遗之关键字static
- jpgraph绘图库的安装与配置
- java中的构造器
- Asp.net 解析json
- 你知道吗?win10十大最佳隐藏属性功能盘点
- HDU5493 Queue 线段树单点更新
- .NET/MVC-发布到IIS6.1提示未能加载程序集System.Web.Http.WebHost
- 按位非运算符“~”
- NSbundle
- win7 x64双机联调