最近,我遇到了一个有趣的挑战,我显然无法解决。
我是一个Android初学者,我感兴趣的是:如何从文本文件中读取(在手机上,作为用户)并将解析后的文件存储到字符串中,处理字符串,输出它?
例如:
code to read a file
.
.
code to count characters
.
.
.
code to print the result on the phone screen.
感谢您的帮助,我很感激。
答案 0 :(得分:1)
后台异步任务加载和解析filename.csv
class LoadAllIcosSD extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SynchroIcoActivitySD.this);
pDialog.setMessage(getString(R.string.progdata));
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
try {
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "/myfolder/filename.csv";
File myFile = new File(baseDir + File.separator + fileName);
if(myFile.exists()){
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
//how many row
int ip=0;
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
ip = ip+1;
}
String kosikx = aBuffer;
myReader.close();
String delims = "[\n]+";
String delims2 = "[;]+";
String[] riadokxxx = kosikx.split(delims);
for (int i = 0; i < riadokxxx.length; i++) {
String riadok1 = riadokxxx[i];
String[] polozkyx = riadok1.split(delims2);
String icox = polozkyx[0];
String naix = polozkyx[3] + " " + polozkyx[6];
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_PID, icox);
map.put(TAG_NAME, naix);
map.put(TAG_PRICE, "!!");
// adding HashList to ArrayList
productsList.add(map);
}
}//iffileexist
} catch (Exception e) {
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
SynchroIcoActivitySD.this, productsList,
R.layout.list_item_icosd, new String[] { TAG_PID, TAG_NAME, TAG_PRICE},
new int[] { R.id.pid, R.id.name, R.id.price });
// updating listview
setListAdapter(adapter);
}
});
}
}