【问题标题】:how to read a value in CSV with the value left to it如何读取 CSV 中的值并保留其值
【发布时间】:2016-12-15 07:34:18
【问题描述】:

我在堆栈和网络中搜索了很多,用于解析 CSV 文件。
但我不会得到我想要的。

我有这张 CSV 格式的表格,

我想要的是,如果我给出“ID”(即 0,1,2,3...),它应该返回给我正确的值,即,如果我给出

“2”它应该返回我“你好,你好吗?”。

“4”它应该返回我“你喜欢什么而不是茶?”

如何做到这一点?
现在我将 CSV 文件保存在 raw 文件夹中。

任何帮助将不胜感激

【问题讨论】:

  • '我在堆栈和网络中搜索了很多,用于解析 CSV 文件。'这听起来有点可疑,因为如果我用谷歌搜索“android parse csv file”,这是第一个结果。见stackoverflow.com/questions/5360628/…
  • 但我想从 id 获取值,即 0,1,2.. 我有 n 个字符串
  • 我不想遍历循环,
  • 您必须这样做,因为您已通读该文件。您需要通读文件,将每一行存储为键值对以将数字匹配到字符串中
  • 那么,如何在 csv 文件中存储键值对。

标签: java android csv


【解决方案1】:

您可以将 CSV 中的数据保存为:-

ArrayList<TableData> arrayList=new ArrayList<>();
        arrayList.add(new TableData(1,"Hello"));
        arrayList.add(new TableData(2,"How are u"));
        arrayList.add(new TableData(3,"I am fine"));
        arrayList.add(new TableData(4,"Thank You"));

        File file;
        File root   = Environment.getExternalStorageDirectory();
        if (root.canWrite()){
            File dir    =   new File (root.getAbsolutePath() + "/PersonData");
            dir.mkdirs();
            file   =   new File(dir, "Data.csv");
            FileOutputStream out   =   null;
            try {
                // write to byte array
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream dos = new DataOutputStream(baos);
                for (TableData element : arrayList) {
                    dos.writeUTF(String.valueOf(element.id));
                    dos.writeUTF(String.valueOf(element.name));
                }
                byte[] bytes = baos.toByteArray();
                out = new FileOutputStream(file);

                out.write(bytes);
                out.close();
            }catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

并将其检索为:-

File file;
        File root   = Environment.getExternalStorageDirectory();

            File dir    =   new File (root.getAbsolutePath() + "/PersonData");
            dir.mkdirs();
            file   =   new File(dir, "Data.csv");


        Uri u1  =   null;
        u1  =   Uri.fromFile(file);
        try {
            FileInputStream inputStream=new FileInputStream(file);
            String input="2";
            String previousValue="";
            ByteArrayInputStream bais = new ByteArrayInputStream(readFully(inputStream));
            DataInputStream in = new DataInputStream(bais);
            while (in.available() > 0) {
                String element = in.readUTF();
                if(previousValue.equalsIgnoreCase(input)){
                    textView.setText(element);
                }
                previousValue=element;

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

将inputStream转换为byte[]的方法:-

public static byte[] readFully(InputStream input) throws IOException
    {
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = input.read(buffer)) != -1)
        {
            output.write(buffer, 0, bytesRead);
        }
        return output.toByteArray();
    }

【讨论】:

  • 它正在工作,也许我已经对其进行了一些修改。
【解决方案2】:

创建一个单独的类来读取 CSV 文件:-

    public class CSVFile {
        InputStream inputStream;

        public CSVFile(InputStream inputStream){
            this.inputStream = inputStream;
        }

        public List read(){
            List resultList = new ArrayList();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            try {
                String csvLine;
                while ((csvLine = reader.readLine()) != null) {
                    String[] row = csvLine.split(",");
                    resultList.add(row);
                }
            }
            catch (IOException ex) {
                throw new RuntimeException("Error in reading CSV file: "+ex);
            }
            finally {
                try {
                    inputStream.close();
                }
                catch (IOException e) {
                    throw new RuntimeException("Error while closing input stream: "+e);
                }
            }
            return resultList;


   }
}

现在在你的活动中调用它:-

String input="2";
        InputStream inputStream = getResources().openRawResource(R.raw.sample);
        CSVFile csvFile = new CSVFile(inputStream);
        List scoreList = csvFile.read();
        for(int i=0;i<scoreList.size();i++){
            Object data_list=scoreList.get(i);
            String a[]= (String[]) data_list;
            String id=a[0];
            String value=a[1];
            if(input.equalsIgnoreCase(id)){
                Log.d("TAG", "Value Found "+value);
            }

        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 2021-07-28
    相关资源
    最近更新 更多