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可以用于对文件夹进行操作. ...
随机推荐
- css3中-moz、-ms、-webkit各什么意思
1.-moz代表firefox浏览器私有属性 2.-ms代表ie浏览器私有属性 3.-webkit代表safari.chrome私有属性 这些是为了兼容老版本的写法,比较新版本的浏览器都支持直接写:b ...
- Ubuntu14.04安装 HP DeskJet GT 5820 打印机的方法
原创内容发表在 http://www.linuxdiyf.com/linux/25331.html 步骤就是: 这里(https://sourceforge.net/projects/hplip/fi ...
- 原生js去掉所有的html标签,最终得到HTML标签中的所有内容
替换掉所有的 html标签,最终得到Html标签中的内容 <script> //替换掉所有的 html标签,最终得到Html标签中的内容 var req="<div sty ...
- Centos 7安装MYSQL
1.下载RPM源 直接使用yum命令下载mysql来进行安装是不能成功的,安装过程会有问题,这里需要使用rpm命令来先进下载.下载路径为: http://dev.mysql.com/get/mysql ...
- 导出数据子集:带where条件的exp导出
举个例子:用select * from all_objects创建了一张表T.想要导出object_id小于5000的所有行.(1)windows下: exp userid=cms0929/cms09 ...
- 深入理解Oracle中的随机函数
--Oracle中取随机值的函数 .dbms_random包 dbms_random包提供了一系列的随机值获取函数以及相关存储过程.下面详细讲解常用的函数和过程. ()dbms_random.rand ...
- AutoMagic-开源自动化平台构建思路
最近在github上看到AutoMagic自动化平台开源了,一时手痒,就试着搭了一套环境,现在把思路和大家说一说. AutoMagic从其工作分工分两部分: 1:Web端管理平台 管理平台基于Pyth ...
- Go 语言函数闭包
Go 语言支持匿名函数,可作为闭包.匿名函数是一个"内联"语句或表达式.匿名函数的优越性在于可以直接使用函数内的变量,不必申明. 以下实例中,我们创建了函数 getSequence ...
- Zookeeper命令行操作(常用命令;客户端连接;查看znode路径;创建节点;获取znode数据,查看节点内容,设置节点内容,删除节点;监听znode事件;telnet连接zookeeper)
8.1.常用命令 启动ZK服务 bin/zkServer.sh start 查看ZK服务状态 bin/zkServer.sh status 停止ZK服务 bin/zkServer.sh stop 重启 ...
- Android 5.0 调色 Palette调色功能
Palette非常好用,也非常好玩. Palette的作用是从图像中提取突出的颜色,这样我们可以根据提取到的色值把它赋给Toolbar,标题,状态栏等,可以使我们的整个界面色调统一,效果非常好看. P ...