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. python3全栈开发-补充UDP的套接字、操作系统、并发的理论基础

    一.基于UDP的套接字 udp套接字简单示例 import socket ip_port=('1.1.1.1',8181) BUFSIZE=1024 udp_server_client=socket. ...

  2. jquery easyui datagrid设置行样式 不可删除某行

    rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSu ...

  3. 81. Search in Rotated Sorted Array II (中等)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  4. vue项目开发中遇到的问题总结--内部分享

     1.路由变化页面数据不刷新问题 这种情况一般出现在vue-router的history模式下,初次进入会执行钩子函数,再次进入时则不会. 解决方案: 监听路由变化 watch : { "$ ...

  5. REACT相关资料合集

    ===实例=== https://github.com/jesseskinner/react-webpack-demo ===UI组件库=== https://github.com/amazeui/a ...

  6. 重置winsock目录解决不能上网的问题

    摘自:http://www.52microsoft.com/netsh-winsock-reset/ 有时候,我们会遇到能成功连接网络但是却无法上网的问题.屏幕右下角系统托盘中的网络连接图标显示正常, ...

  7. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  8. CRM客户关系管理系统(四)

    kingadmin设计开发 4.4.根据list_display配置生成数据列表 (1)kingadmin/views.py (2)kingadmin/templates/kingadmin/tabl ...

  9. Vasya the Hipster

    One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red so ...

  10. VirtualBox: Resize a Fedora, CentOS, or Windows Dynamic Guest Virtual Disk (VDI) in VirtualBox

    Here's the scenario: you've set up Dynamically Allocated Storage for the hard drive on your Guest VM ...