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的应用开发环境 ...
随机推荐
- uploadify不能正确显示中文的按钮文本的解决办法
uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...
- android SlidingTabLayout实现ViewPager页卡滑动效果
先来张效果图(能够滑动切换页卡) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVuZ2t2/font/5a6L5L2T/fontsize/400/fi ...
- QT中关于窗口全屏显示与退出全屏的实现
近期在学习QT时遇到了很多问题这也是其中一个,个人通过在各种书籍和网络上的查阅找到了一些关于这方面的答案,希望能给大家一些帮助. 首先,在QT中对于窗口显示常用的有这么几个方法可以调用: Qt全屏显示 ...
- .NET基础拾遗(7)多线程开发基础2
二..NET中的多线程编程 2.1 如何在.NET程序中手动控制多个线程? 最直接且灵活性最大的,莫过于主动创建.运行.结束所有线程. (1)第一个多线程程序 .NET提供了非常直接的控制线程类型的类 ...
- C# Interface显式实现和隐式实现
c#中对接口的实现方式有两种:隐式实现和显式实现,之前一直没仔细看过,今天查了些资料,在这里整理一下. 隐式实现的例子 interface IChinese { string Speak(); } p ...
- wed网页开发面试笔试必备小知识
HTML中行内元素与块级元素的区别: 在标准文档流里面,块级元素具有以下特点: ①总是在新行上开始,占据一整行: ②高度,行高以及外边距和内边距都可控制: ③宽带始终是与浏览器宽度一样,与内容无关: ...
- EasyUI 1.4.4 DataGrid(大数据量) bufferview滚动时不加载下一页数据解决方案
在使用Easyui DataGrid 过程中,发现若单页数据量超过300,IE浏览器加载速度很慢.也通过网上找寻了很多解决方案,最典型的就是去掉datagrid的自动列宽以及自动行高判断. 1.解决自 ...
- @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
1.带有Render的方法返回值是void,在方法内部进行输出: 不带的返回值类型为MvcHtmlString,所以只能这样使用: @Html.Partial 对应 @{Html.RenderPart ...
- 读取oracle页面或者进程卡住不动(死锁)
oracle最坑爹的地方:你insert update delete之后 或者kill死锁的时候记得一定要提交事务不然就是死锁卡在那里了 记住 kill死锁也是要提交事务的 select * ...
- 【nodejs学习】1.文件操作
1.小文件拷贝,使用nodejs内置模块 var fs = require('fs'); function copy(src, dst){ fs.writeFileSync(dst, fs.readF ...