Android之SD卡上的文件读取
2015-09-14 17:30
471 查看
public class ReadSDCard extends Activity
{
String FILE_NAME = "/test1.txt" ;//SD卡的文件的路径
Button writeBtn, readBtn ;
// 一个写的按钮,一个读取的按钮
EditText text1, text2 ;
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_readsdcard);
writeBtn =
(Button) findViewById(R.id.btn_write);
readBtn =
(Button) findViewById(R.id.btn_read);
text1 =
(EditText) findViewById(R.id.edit_t1);
text2 =
(EditText) findViewById(R.id.edit_t2);
writeBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View
v) {
String msg = text1.getText().toString();
writeMSG(msg);
text1.setText("" );
}
});
readBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View
v) {
try {
//
如果手机插入了SD卡,而且应用程序具有访问SD的权限(要在配置文件中注册)
if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED))
{
//
获取SD卡对应的存储目录
File sdCardDir = Environment
. getExternalStorageDirectory();
//
获取指定文件对应的输入流
FileInputStream fis = new FileInputStream(sdCardDir
.getCanonicalFile() + FILE_NAME);
//
将制定的流包装秤BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
StringBuilder sb = new StringBuilder("" );
String line = null;
//
循环读取文件的内容
while ((line
= br.readLine()) != null) {
sb.append(line);
}
br.close();
text2.setText(sb);
}
} catch (FileNotFoundException
e) {
e.printStackTrace();
} catch (IOException
e) {
e.printStackTrace();
}
}
});
}
private void writeMSG(String
content) {
try {
if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED))
{
//
获取SD卡目录
File sdCard = Environment.getExternalStorageDirectory();
File targeFile = new File(sdCard.getCanonicalFile()
+ FILE_NAME );
//
以指定文件创建RandomAccessFile对象
RandomAccessFile raf = new RandomAccessFile(targeFile, "rw" );
//
将文件记住的指针移动到最后(移到最后才能追加、不然只能覆盖原有的数据)
raf.seek(targeFile.length());
raf.write(content.getBytes());
System. out.println("写入成功" +
raf);
raf.close();
}
} catch (Exception
e) {
}
}
}
{
String FILE_NAME = "/test1.txt" ;//SD卡的文件的路径
Button writeBtn, readBtn ;
// 一个写的按钮,一个读取的按钮
EditText text1, text2 ;
@Override
protected void onCreate(Bundle
savedInstanceState) {
// TODO Auto-generated
method stub
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_readsdcard);
writeBtn =
(Button) findViewById(R.id.btn_write);
readBtn =
(Button) findViewById(R.id.btn_read);
text1 =
(EditText) findViewById(R.id.edit_t1);
text2 =
(EditText) findViewById(R.id.edit_t2);
writeBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View
v) {
String msg = text1.getText().toString();
writeMSG(msg);
text1.setText("" );
}
});
readBtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View
v) {
try {
//
如果手机插入了SD卡,而且应用程序具有访问SD的权限(要在配置文件中注册)
if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED))
{
//
获取SD卡对应的存储目录
File sdCardDir = Environment
. getExternalStorageDirectory();
//
获取指定文件对应的输入流
FileInputStream fis = new FileInputStream(sdCardDir
.getCanonicalFile() + FILE_NAME);
//
将制定的流包装秤BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
StringBuilder sb = new StringBuilder("" );
String line = null;
//
循环读取文件的内容
while ((line
= br.readLine()) != null) {
sb.append(line);
}
br.close();
text2.setText(sb);
}
} catch (FileNotFoundException
e) {
e.printStackTrace();
} catch (IOException
e) {
e.printStackTrace();
}
}
});
}
private void writeMSG(String
content) {
try {
if (Environment.getExternalStorageState().equals(Environment. MEDIA_MOUNTED))
{
//
获取SD卡目录
File sdCard = Environment.getExternalStorageDirectory();
File targeFile = new File(sdCard.getCanonicalFile()
+ FILE_NAME );
//
以指定文件创建RandomAccessFile对象
RandomAccessFile raf = new RandomAccessFile(targeFile, "rw" );
//
将文件记住的指针移动到最后(移到最后才能追加、不然只能覆盖原有的数据)
raf.seek(targeFile.length());
raf.write(content.getBytes());
System. out.println("写入成功" +
raf);
raf.close();
}
} catch (Exception
e) {
}
}
}
相关文章推荐
- Android JNI NDK 全程配置和运行demo,总结csdn所有博客自己提炼!!!!最易懂
- 如何在Android上使用OpenGL ES 2.0绘制点。
- Android AsyncTask完全解析,带你从源码的角度彻底理解
- 去掉android二维码的边框
- android 开发第五弹---水波纹与自定义view实现绚丽粒子
- Android数据库安全解决方案,使用SQLCipher进行加解密
- 去除AndroidStudio中libpng关于iCCP的警告
- android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
- Android5.0新控件CardView的介绍和使用
- 深入理解Android之Gradle
- Android SDK 在线更新镜像服务器资源
- Android 让图片等比例缩放的三种方法
- android studio 代理配置
- android之Handler用法
- Android SDK开发包国内下载地址
- Android TextView自动换行文字排版参差不齐的原因
- Android studio -- Throwable: Unable to locate adb within SDK
- Android - 序
- Android项目规范
- Android Studio六--Gradle多渠道打包