第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Android 读取手机SD卡根目录下某个txt文件的文件内容

Android 读取手机SD卡根目录下某个txt文件的文件内容

时间:2024-01-13 19:05:47

相关推荐

Android 读取手机SD卡根目录下某个txt文件的文件内容

1.先看activity_main.xml文件:

<LinearLayout xmlns:android="/apk/res/android"xmlns:android1="/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content" ><TextView android:textSize="18dip"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文件名: /sdcard/" /><EditText android:hint="xname.txt"android:id="@+id/ET_Folder" android:layout_width="180dip" android:layout_height="wrap_content" /></LinearLayout><LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content" ><Button android:text="打开" android:id="@+id/But_Open" android:layout_width="wrap_content"android:layout_height="wrap_content" /><Button android:text="清除" android:id="@+id/But_Clear" android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout><ScrollView android:id="@+id/ScrollView01" android:layout_width="fill_parent" android:layout_height="wrap_content"><EditText android:editable="false"android:id="@+id/ET_FileContent" android:layout_width="fill_parent" android:layout_height="wrap_content" /></ScrollView></LinearLayout>

2.MainActivity.java文件

/*读取手机SD卡根目录下某个txt文件的文件内容 * */import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_folder;//输入的文件夹名private Button bt_open;//打开按钮private Button bt_clear;//清除按钮private EditText et_filecontent;//用于显示txt文件内容protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_folder = (EditText) findViewById(R.id.ET_Folder);et_filecontent = (EditText) findViewById(R.id.ET_FileContent);bt_open = (Button) findViewById(R.id.But_Open); bt_open.setOnClickListener(new OnClickListener(){//打开按钮监听public void onClick(View arg0) {//若输入的文件夹名为空if(et_folder.getText().toString().trim().equals("")){Toast.makeText(getApplicationContext(), "输入为空",Toast.LENGTH_SHORT).show();}else{//获得SD卡根目录路径 "/sdcard"File sdDir = Environment.getExternalStorageDirectory();//根目录下某个txt文件名File path = new File(sdDir+File.separator+et_folder.getText().toString().trim());// 判断SD卡是否存在,并且是否具有读写权限if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { et_filecontent.setText("");et_filecontent.setText(getFileContent(path));}}}});bt_clear = (Button) findViewById(R.id.But_Clear); bt_clear.setOnClickListener(new OnClickListener(){//清除按钮监听public void onClick(View arg0) {et_folder.setText("");et_filecontent.setText("");}});}//读取指定目录下的所有TXT文件的文件内容protected String getFileContent(File file) {String content = "";if (file.isDirectory() ) {//检查此路径名的文件是否是一个目录(文件夹)Log.i("zeng", "The File doesn't not exist "+file.getName().toString()+file.getPath().toString()); } else {if (file.getName().endsWith(".txt")) {//文件格式为txt文件try {InputStream instream = new FileInputStream(file); if (instream != null) {InputStreamReader inputreader=new InputStreamReader(instream, "GBK");BufferedReader buffreader = new BufferedReader(inputreader);String line="";//分行读取while (( line = buffreader.readLine()) != null) {content += line + "\n";}instream.close();//关闭输入流}}catch (java.io.FileNotFoundException e) {Log.d("TestFile", "The File doesn't not exist.");} catch (IOException e) {Log.d("TestFile", e.getMessage());}}}return content ;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。