java读取sd卡txt文件代码_安卓读写文件txt(SD卡)

在前一篇文章中说道了手机读写内存文件的操作,这次主要说的是手机读写SD卡文件的操作

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:id="@+id/read"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/txt1"

android:layout_alignParentBottom="true"

android:layout_marginBottom="46dp"

android:text="@string/read" />

android:id="@+id/write"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/ed1"

android:layout_below="@+id/ed1"

android:layout_marginTop="84dp"

android:text="@string/write" />

android:id="@+id/txt1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/write"

android:layout_below="@+id/write"

android:layout_marginTop="72dp" />

android:id="@+id/ed1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="41dp"

android:ems="10"

android:inputType="textMultiLine" >

在AndroidManifest.xml文件中加入:

android:minSdkVersion="8"

android:targetSdkVersion="18" />

Java代码:

package com.example.sdcard;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private Button write;

private Button read;

private EditText ed1;

private TextView txt1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

write = (Button) findViewById(R.id.write);

read = (Button) findViewById(R.id.read);

ed1 = (EditText) findViewById(R.id.ed1);

txt1 = (TextView) findViewById(R.id.txt1);

write.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

writeSDcard(ed1.getText().toString());

}

});

read.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

txt1.setText(readSDcard());

}

});

}

// 把数据写入SD卡

private void writeSDcard(String str) {

try {

// 判断是否存在SD卡

if (Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

// 获取SD卡的目录

File file = Environment.getExternalStorageDirectory();

FileOutputStream fileW = new FileOutputStream(file.getCanonicalPath() + "/test.txt");

fileW.write(str.getBytes());

fileW.close();

}else{

showMessage("SD卡不存在!!");

}

} catch (Exception e) {

e.printStackTrace();

}

}

// 从SD卡中读取数据

private String readSDcard() {

StringBuffer str = new StringBuffer();

try {

// 判断是否存在SD

if (Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

File file = new File(Environment.getExternalStorageDirectory()

.getCanonicalPath() + "/test.txt");

// 判断是否存在该文件

if (file.exists()) {

// 打开文件输入流

FileInputStream fileR = new FileInputStream(file);

BufferedReader reads = new BufferedReader(

new InputStreamReader(fileR));

String st = null;

while ((st =reads.readLine())!=null ) {

str.append(st);

}

fileR.close();

} else {

txt1.setText("该目录下文件不存在");

}

}else{

showMessage("SD卡不存在!!");

}

} catch (Exception e) {

e.printStackTrace();

}

return str.toString();

}

// 显示信息

public void showMessage(String msg) {

Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值