android读取sdtxt文件
‘壹’ android 怎样在绝对路径下打开txt文件比如读取D:\F.TXT文件
android中只能访问android手机中的文件和SD卡中的文件,如果说你是想写入本地D:\F.TXT到你的应用中的话,只需要简单的操作一下io流就可以了,你的应用就可以从流中读取数据,要想从pc上复制文件到(模拟器)真机上可以用android:fileter视图里的工具,也可以用dos命令实现.比如将你d:\f.txt文件复制到机子上sd卡根目录下 adb -s emulator-5554 pull d:\f.txt/sdcard e.txt,你能把具体问题说一下么?问题太含糊了!
‘贰’ android读取txt文件
您好,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。
在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写a.txt了。
‘叁’ android怎样读文本文件的内容
1. 读取操作
String path = "/sdcard/foo.txt";
String content = ""; //文件内容字符串
//打开文件
File file = new File(path);
//如果path是传递过来的参数,可以做一个非目录的判断
if (file.isDirectory()){
Toast.makeText(EasyNote.this, "没有指定文本文件!", 1000).show();
}
else{
try {
InputStream instream = new FileInputStream(file);
if (instream != null) {
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
//分行读取
while (( line = buffreader.readLine()) != null) {
content += line + "\n";
}
instream.close();
} catch (java.io.FileNotFoundException e) {
Toast.makeText(EasyNote.this, "文件不存在", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 写入操作
String filePath = "/sdcard/foo2.txt";
String content = "这是将要写入到文本文件的内容";
//如果filePath是传递过来的参数,可以做一个后缀名称判断; 没有指定的文件名没有后缀,则自动保存为.txt格式
if(!filePath.endsWith(".txt") && !filePath.endsWith(".log"))
filePath += ".txt";
//保存文件
File file = new File(filePath);
try {
OutputStream outstream = new FileOutputStream(file);
OutputStreamWriter out = new OutputStreamWriter(outstream);
out.write(content);
out.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
‘肆’ android编程:怎样读取txt文件
android 能读取的文件都是系统里面的(这是系统不是开发坏境系统,而是你程序运行的环境系统,也就是avd或者真实的手机设备的sd卡),这就需要你把文件导入你的环境中,mnt目录底下,然后按到读取sd卡的路径读取即可。
‘伍’ android读取TXT文件数据
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String fileName = "/sdcard/y.txt";//文件路径
// 也可以用String fileName = "mnt/sdcard/Y.txt";
String res = "";
try {
FileInputStream fin = new FileInputStream(fileName);
// FileInputStream fin = openFileInput(fileName);
// 用这个就不行了,必须用FileInputStream
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");////依Y.txt的编码类型选择合适的编码,如果不调整会乱码
fin.close();//关闭资源
System.out.println("res--->"+res);
int a=Integer.parseInt(res.substring(3, 5));
int b=Integer.parseInt(res.substring(8, 10));
System.out.println(a+"res--->"+b);//获取的a.b
} catch (Exception e) {
e.printStackTrace();
}
}
‘陆’ android 我想读取一个txt文件,怎么获取路径
你的这个文件是不是存在手机的内存卡里面呢,如果是可以这样做
//首先获取到手机内存卡的根路径
String rootPath = Environment.getExternalStorageDirectory().getPath();
File file = new File(rootPath + "/a.txt"); //假设文件就在内存卡的根目录下
得到file对象之后就跟Java一样处理了
‘柒’ Android 根目录下读写.txt文件
//根目录权限不允许,放到/data/packeg_dir下或SD卡中
packagecom.example.demo;
Filedir=Environment.getDataDirectory();//获取data目录
//Environment.getExternalStorageDirectory();//获取SD卡目录
FileoutFile=newFile(dir,"/data/com.example.demo/text.txt");//只能在自己的程序包里建立文件,这是权限问题
‘捌’ 简述Android中如何利用文件存储来读写SD卡上的TXT文件。
确定这么做就必须要意识到一旦放进去就成死的了。而且你说的“改一下”更好的方法是“扩展一下” 扩展下你的电纸书包含一些默认的文件。