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进阶(六)文件读操作的更多相关文章

  1. Java进阶(二)文件读操作

    本文以实际的读取文件为例子,介绍流的概念,以及输入流的基本使用. 按照前面介绍的知识,将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流--InputStream或Reader.而由 ...

  2. python 基础之文件读操作

    创建一个名为‘尘曦’的文件内容如下 Hadoop是一个由Apache基金会所开发的分布式系统基础架构. 用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. ...

  3. go语言之进阶篇文件常用操作接口介绍和使用

    一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...

  4. Android中的文件权限操作

    默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...

  5. 文件读操作(IO编程)

    将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流——InputStream或Reader.而由于读取的是特定的数据源——文件,则可以使用输入对应的子类FileInputStream ...

  6. Android 数据存储-文件读写操作

    本来已经写了一部分,后来发现这篇博客写的比我的好,就直接引用一下: https://www.cnblogs.com/LiHuiGe8/p/5604725.html

  7. Android数据存储-文件操作

    一.预备知识 1.Android中的MVC设计模式 MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...

  8. python文件读取操作、序列化

    1.对文件进行读写操作时,先建立文件句柄 f = open("test.txt","r",encoding="UTF-8") 其中,r为文件 ...

  9. 一篇文章快速搞懂Qt文件读写操作

    导读:Qt当中使用QFile类对文件进行读写操作,对文本文件也可以与QTextStream一起使用,这样读写操作会更加简便.QFileInfo可以用来获取文件的信息.QDir可以用于对文件夹进行操作. ...

随机推荐

  1. vrn:基于直接体积回归的单幅图像大姿态三维人脸重建

    3D面部重建是一个非常困难的基本计算机视觉问题.目前的系统通常假设多个面部图像(有时来自同一主题)作为输入的可用性,并且必须解决许多方法学挑战,例如在大的面部姿势,表情和不均匀照明之间建立密集的对应. ...

  2. linux系统下安装jdk、tomcat、mysql、redis

    一,安装jdk 1.rpm -qa | grep jdk 检测是否安装jdk 2.rpm -e --nodeps 要卸载的已有jdk名称 3.tar -zxvf jdk-7u71-linux-i586 ...

  3. Linux安装JProfiler监控tomcat

    下载JProfiler包wget http://download-keycdn.ej-technologies.com/jprofiler/jprofiler_linux_9_2.rpm 安装JPro ...

  4. sublime安装配置

    http://www.sublimetext.com.cn/ 打华东师范大学校赛的时候,学长谈论到这个编辑器.自定义背景多行多光标同时编辑酷炫爆了.感觉这是一个万能的文本编辑器.通过配置可以写多种语言 ...

  5. Linux服务器搭建相关教程链接整理

    Linux: Linux 教程 | 菜鸟教程 linux下如何添加一个用户并且让用户获得root权限 - !canfly - 博客园 Git: 在 Linux 下搭建 Git 服务器 - 黄棣-dee ...

  6. Spring Security安全框架入门篇

    一.Spring Security相关概念 1.1..Spring Security介绍: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安 ...

  7. Linux测量kernel子模块加载时间的方法

    1. 在文件kernel/init/main.c里面,在接口do_one_initcall( )中,将initcall_debug设置为true,然后编译boot.img 2. 使用adb shell ...

  8. Dynamics CRM2011 导入解决方案报根组件插入错误的解决方法

    今天在还原一个老版本的解决方案,在导入时报根组件插入问题"Cannot add a Root Component 38974590-9322-e311-b365-00155d810a00 o ...

  9. 递归dict

    一个看起来非常酷的定义 class Example(dict): def __getitem__(self, item): try: return dict.__getitem__(self, ite ...

  10. For oracle databases, if the top showing the oracle database, then oracle process is using the top c

    Note 805586.1   Troubleshooting Session Administration (Doc ID 805586.1)Note 822527.1   How To Find ...