2016-12-15 9 views
-2

CSVファイルを解析するために、スタックとウェブを多く検索しました。
しかし、私は私が望むものを得ることはできません。CSVで値を読み取る方法

私は私が欲しいもの

enter image description here

、CSV形式でこのテーブルを持っているが、私は(0,1,2,3です...) "ID" を与えれば、それは返す必要があり、あります私はそれに値する権利、すなわち、私が与える場合

"2"それは私に "こんにちはお元気ですか?"

"4"それは私に返す必要があります "あなたはお茶よりも何を好きですか?"

これを行うには?
今は自分のCSVファイルをそのままのフォルダに保存しました。

すべてのヘルプはあなたのようにCSVでデータを保存することができます

+2

「私はCSVファイルを解析するために、スタックとウェブで多くのことを検索しました。 'これは私が 'android parse csv file'のためにgoogleを作ったときの最初の結果であるので、少し疑わしいと思う。 http://stackoverflow.com/questions/5360628/get-and-parse-csv-file-in-android – 0xDEADC0DE

+0

を参照してください。私はidから値、つまり0,1,2を取得したいと思います。文字列 –

+0

私はループをたどりたくない。 –

答えて

0

をapppreciatedされます: -

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

としてそれをRetrive: - バイトをInputStreamを変換する

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

方法[] : -

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(); 
    } 
+0

これを試してみます。これは良いようです。 –

+0

それは働いています、おそらく私はそれにいくつかの変更を加えました。 –

0

として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); 
      } 

     } 
関連する問題