Android进阶(六)文件读操作
Android中文件的读写操作与Java中文件的读写操作是有区别的。在Java中,读文件操作如以下代码所示:
public class FileRead {
private static final String filePath = "E:/SHQ/workspace/TT/中国火车查询字段对应表.txt";
public static void main(String [] args) throws IOException{
String from_station = "济南";
String to_station = "北京";
readFile(filePath,from_station,to_station);
}
private static String [] readFile(String filepath, String from_station, String to_station) throws IOException{
Map<String, String> map = new HashMap<String, String>();
String s;
String [] data = null;
FileReader fileReader = null;
try {
File inputFile = new File(filepath);
fileReader = new FileReader(inputFile);
BufferedReader bf = new BufferedReader(fileReader);
while ((s = bf.readLine()) != null){
data = s.split(":");
map.put(data[0], data[1]);
}
data[0] = map.get(from_station);
data[1] = map.get(to_station);
System.out.println(data[0] + ">>>>>>>>>>>>>" + data[1]);
return data;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}finally{
fileReader.close();
}
}
}
而在Android开发中则不然!在Android开发中,读文件操作代码如下图所示:
public void search(View source){
// 获取输入的数值时,一定要将获取内容的语句放在按键触发式的方法内
from_station = from_station_name.getText().toString().trim();
to_station = to_station_name.getText().toString().trim();
String res = null;
byte[] buffer = null;
try {
InputStream in = getResources().getAssets().open(fileName);
//返回读取的大概字节数
int length = in.available();
buffer = new byte[length];
in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
res = EncodingUtils.getString(buffer, "GBK");
Map<String, String> map = new HashMap<String, String>();
String[] trainsInfo = null;
String[] medium = null;
//读取火车查询字段对应表(根据调试信息得出)
trainsInfo = res.split("\r\n");
for ( String str: trainsInfo){
medium = str.split(":");
map.put(medium[0], medium[1]);
}
from_station = map.get(from_station);
to_station = map.get(to_station);
url = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate="
+ queryDate
+ "&from_station="
+ from_station
+ "&to_station="
+ to_station;
Spider spider = new Spider(this);
spider.execute(url);
}
Android进阶(六)文件读操作的更多相关文章
- Java进阶(二)文件读操作
本文以实际的读取文件为例子,介绍流的概念,以及输入流的基本使用. 按照前面介绍的知识,将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流--InputStream或Reader.而由 ...
- python 基础之文件读操作
创建一个名为‘尘曦’的文件内容如下 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. ...
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- Android中的文件权限操作
默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...
- 文件读操作(IO编程)
将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流——InputStream或Reader.而由于读取的是特定的数据源——文件,则可以使用输入对应的子类FileInputStream ...
- Android 数据存储-文件读写操作
本来已经写了一部分,后来发现这篇博客写的比我的好,就直接引用一下: https://www.cnblogs.com/LiHuiGe8/p/5604725.html
- Android数据存储-文件操作
一.预备知识 1.Android中的MVC设计模式 MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...
- python文件读取操作、序列化
1.对文件进行读写操作时,先建立文件句柄 f = open("test.txt","r",encoding="UTF-8") 其中,r为文件 ...
- 一篇文章快速搞懂Qt文件读写操作
导读:Qt当中使用QFile类对文件进行读写操作,对文本文件也可以与QTextStream一起使用,这样读写操作会更加简便.QFileInfo可以用来获取文件的信息.QDir可以用于对文件夹进行操作. ...
随机推荐
- java的迭代器详解
迭代器的引出 在jdk1.5版本之前是没有 foreach的,然而1.5版本就加上了foreach,而引入的新的foreach功能并不是在jvm上进行改进的因为代价太高,甲骨文工程师想到了一个比较好的 ...
- Java 反射(二)
作者:郑剑锋链接:https://www.zhihu.com/question/24304289/answer/147529485来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Linux 在线模拟器
最近在学习Linux的一些命令的使用,但是很久之前装的Linux虚拟机被删掉了,又不想为了练习几个命令折腾一遍虚拟机.所以,就尝试地搜了一下,看看有没有在线的Linux模拟器可以使用,只要可以练习一下 ...
- Django笔记--视图
URLconf 在settings.py文件中通过ROOT_URLCONF指定根级url的配置 urlpatterns是一个url()实例的列表 一个url()对象包括: 正则表达式 视图函数 名称n ...
- ACM Ignatius and the Princess II
Problem Description Now our hero finds the door to the BEelzebub feng5166. He opens the door and fin ...
- mysql5.7在centos上安装的完整教程以及相关的“坑”
安装前的准备 Step1: 如果你系统已经有mysql,如一般centos自带mysql5.1系列,那么你需要删除它,先检查一下系统是否自带mysql yum list installed | gre ...
- Linux测量kernel子模块加载时间的方法
1. 在文件kernel/init/main.c里面,在接口do_one_initcall( )中,将initcall_debug设置为true,然后编译boot.img 2. 使用adb shell ...
- Most Common Solutions to FRM-41839 and .tmp Files Not Being Deleted
In this Document Symptoms Changes Cause Solution References APPLIES TO: Oracle Application ...
- 优化Javascript数据遍历
问题 M是一个对象的集合,没个对象拥有唯一的字符串类型的Id N是Id的集合. 从M中过滤掉Id不在N中的对象. 假如M有50w个数据,N中可能是0~50w任意的数据. 方案1 使用数组保存Id的集合 ...
- FORM界面批量处理-全选框实现
全选框实现方法多种多样,这里只介绍两种 方法一:触发器式,优点程序简单,缺点颜色单调不突出 1. 在数据块和控制块上分别创建check box 2. 设置check box选中与为 ...