Android 读取手机SD卡根目录下某个txt文件的文件内容
1.先看activity_main.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android: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> <LinearLayout
android: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 ;
}
}
Android 读取手机SD卡根目录下某个txt文件的文件内容的更多相关文章
- MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示
初学Rxjava,目前只能遍历加载指定目录下的所有文件夹中的照片,文件夹中如果还嵌套有文件夹目前还没找到实现方法. 先看mvp目录结构: 很抱歉,没有model. 接下来是view层的接口代码和pre ...
- android setDestinationInExternalPublicDir 下载到SD卡根目录
一:setDestinationInExternalPublicDir(“Trinea”, “MeiLiShuo.apk”);表示设置下载地址为sd卡的Trinea文件夹,文件名为MeiLiShuo. ...
- Android之——清理手机SD卡缓存
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47375595 眼下,市场上非常多Android手机软件都具有清理SD卡缓存的功能, ...
- android 获取sd卡根目录
dir:/storage/emulated/0 也就是 sdcard目录 ====== android 获取sd卡根目录 public String getSDPath(){ File ...
- Windows mobile 下读取手机SIM卡信息(转)
Windows mobile 下读取手机SIM卡信息 c#改善 Windows mobile 下读取手机SIM卡信息
- Android BaseAdapter ListView (SD卡中文件目录显示出来)
首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...
- Android 读取手机某个文件夹目录及子文件夹中所有的txt文件
1. activity_main.xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- FileUtils【获取SD卡根目录、读写文件、移动、复制、删除文件、获取文件名、后缀名操作类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 封装了获取SD卡根目录路径.以及对文件读写.获取文件名等相关操作. 因为需要用到android.permission.READ_EX ...
- Android模拟器使用SD卡
在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一. 准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...
随机推荐
- c++应用程序文件的编译过程
这里讲下C++文件的编译过程及其中模板的编译过程: 一:一般的C++应用程序的编译过程. 一般说来,C++应用程序的编译过程分为三个阶段.模板也是一样的. 在cpp文件中展开include文件 ...
- centos虚拟机NAT静态IP设置
宿主机为Centos6.3 64位,三台虚拟机为为Centos6.3 64位.虚拟机的网络连接方式为默认的NAT方式.虚拟机默认为DHCP方式动态获取IP.为了在三台虚拟机上搭建hadoop,需要将这 ...
- jQuery插件开发 格式与解析3之$.extend()用途
前叙:$.extend()——用途:扩展和继承 1.Object extend() 用一个或多个对象扩展另一个对象,并返回已修改的原始对象.这对于简单继承是一个非常有用的实用工具. (1)扩展:(Do ...
- monkeyrunner总结
device=MonkeyRunner.waitForConnection() //手机连接 result = device.takeSnapshot() //截图 result.write ...
- 如何解决svn图标不显示呢?
svn图标不显示解决 确保设置正确: 右键->TortoiseSVN->setting->Icon Overlays->Status cache->default/She ...
- linux命令帮助
Linux命令格式:command [options] [arguments]command:命令options: 参数 [] 表示是可选的;<> 表示是可变化的; x|y|z 表示只能选 ...
- 判断http 请求来自于手机还是PC
首先收集了部分客户端请求头部信息如下 iPhone微信: User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) App ...
- HTML基础总结<头部>
重点摘录:HTML head 元素 标签 描述 <head> 定义了文档的信息 <title> 定义了文档的标题 <base> 定义了页面链接标签的默认链接地址 & ...
- Ajax (jquery)实现智能提示搜索框(in Django)
搜索框输入搜索名字,从数据库中筛选名字, 如果有包含输入的字母的名字则以json格式返回并且显示在搜索框下: html文件: <!DOCTYPE html> <ht ...
- ORACLE基本SQL语句-查询篇
一.普通查询 /*查询表数据*/select * from STU /*取出前3行数据*/select * from stu where ROWNUM<=3 /*模糊查询*/select * f ...