Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取和解析文本文件_Java_Android - Fatal编程技术网

Java 读取和解析文本文件

Java 读取和解析文本文件,java,android,Java,Android,最近,我遇到了一个有趣的挑战,我显然无法解决 我是一名android初学者,我感兴趣的是:如何读取文本文件(在手机上,作为用户)并将解析后的文件存储到字符串中,处理字符串并输出它 例如: code to read a file . . code to count characters . . . code to print the result on the phone screen. 感谢您的帮助,非常感谢。加载和解析filename.csv的后台异步任务 class LoadAllIcos

最近,我遇到了一个有趣的挑战,我显然无法解决

我是一名android初学者,我感兴趣的是:如何读取文本文件(在手机上,作为用户)并将解析后的文件存储到字符串中,处理字符串并输出它

例如:

code to read a file
.
.
code to count characters
.
.
.
code to print the result on the phone screen.

感谢您的帮助,非常感谢。

加载和解析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);
            }
        });

    }

}
类LoadAllicosd扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(synchronicoActivitySD.this);
setMessage(getString(R.string.progdata));
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
受保护的字符串doInBackground(字符串…args){
试一试{
字符串baseDir=Environment.getExternalStorageDirectory().getAbsolutePath();
字符串fileName=“/myfolder/fileName.csv”;
File myFile=新文件(baseDir+File.separator+fileName);
如果(myFile.exists()){
FileInputStream fIn=新的FileInputStream(myFile);
BufferedReader myReader=新BufferedReader(
新的InputStreamReader(fIn));
字符串aDataRow=“”;
字符串aBuffer=“”;
//几排
int ip=0;
而((aDataRow=myReader.readLine())!=null){
aBuffer+=aDataRow+“\n”;
ip=ip+1;
}
字符串kosikx=aBuffer;
myReader.close();
字符串delims=“[\n]+”;
字符串delims2=“[;]+”;
字符串[]riadokxxx=kosikx.split(delims);
for(int i=0;ivalue
地图放置(标签PID,icox);
地图放置(标签名称,naix);
地图放置(标签价格,“!!”);
//将哈希列表添加到ArrayList
productsList.add(地图);
}
}//难以捉摸
}捕获(例外e){
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//获取所有产品后关闭对话框
pDialog.disclose();
//从后台线程更新UI
runOnUiThread(新的Runnable(){
公开募捐{
/**
*将解析的JSON数据更新到ListView中
* */
ListAdapter=新的SimpleAdapter(
SynchronicActivitySD.this,productsList,
R.layout.list_item_icosd,新字符串[]{TAG_PID,TAG_NAME,TAG_PRICE},
新的int[]{R.id.pid,R.id.name,R.id.price});
//更新列表视图
setListAdapter(适配器);
}
});
}
}

您遇到的具体部件是什么?你已经试过什么了?我一直坚持的第四个部分是如何作为用户从手机中选择一个文本文件,然后解析。谢谢你,这很有帮助。