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);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No Silver Bullet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值