2012-11-21 36 views
-1

假設有文件夾C:\文件上傳包含這兩個CSV文件中:檢索的java從多個CSV文件的多個列中的值


file1.csv有2列(廠商ID,姓名)


file2.csv有2列(廠商ID,地址)/////這裏廠商ID是對應於file1.csv和隨機放置在file2.csv

假設file1.csv包含:


101,供應商1 102,供應商2


file2.csv包含 102,澳大利亞 101,USA


我想以檢索來自這兩個文件中的數據和在oracle數據庫中存儲爲:

廠商ID,姓名,地址


101,供應商1,美國 102,供應商2,澳大利亞

和PLZ具體地告訴我通過Java只有 任何幫助將非常感激。

由於

回答

-1

textfile1.csv

loginid3,password3 
loginid5,password5 
loginid1,password1 
loginid4,password4 
loginid2,password2 

textfile2.csv

loginid3,address3 
loginid2,address2 
loginid1,address1 
loginid5,address5 
loginid4,address4 

輸出:

Key : loginid3 Value : password3 address :address3 
Key : loginid2 Value : password2 address :address2 
Key : loginid1 Value : password1 address :address1 
Key : loginid4 Value : password4 address :address4 
Key : loginid5 Value : password5 address :address5 

CSVReadLoginPass.java

import java.io.BufferedReader; 
import java.io.DataInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStreamReader; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 


public class CSVReadLoginPass { 

public static Map<String,String> map1 = null; 
public static Map<String,String> map2 = null; 
    public static void main(String[] args) { 

     try 
     { 
      readFileandPopulate(); 
      for (Map.Entry<String, String> entry : map1.entrySet()) { 
       System.out.println("Key : " + entry.getKey() + " Value : " 
        + entry.getValue()+" address :"+map2.get(entry.getKey())); 
       //insert into DB 
      } 
      }catch (Exception e){//Catch exception if any 
       e.printStackTrace(); 
      System.err.println("Error: " + e.getMessage()); 
      } 

    } 
    public static void readFileandPopulate() throws Exception 
    { 


     FileInputStream fstream = new FileInputStream("E:\\Eclipse-Leo\\StackOverflow\\src\\resources\\textfile1.csv"); 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     String strLine; 
     map1 = new HashMap<String,String>(); 
      while ((strLine = br.readLine()) != null) { 
       System.out.println(strLine); 
      String temp[] = strLine.split(","); 
      map1.put(temp[0], temp[1]); 

     } 
    in.close(); 
    System.out.println("done 1"); 


    FileInputStream fstream2 = new FileInputStream("E:\\Eclipse-Leo\\StackOverflow\\src\\resources\\textfile2.csv"); 
     DataInputStream in2= new DataInputStream(fstream2); 
     BufferedReader br2 = new BufferedReader(new InputStreamReader(in2)); 
     String strLine2; 
      map2 = new HashMap<String,String>(); 
      while ((strLine2 = br2.readLine()) != null) { 
       System.out.println(strLine2); 
      String temp[] = strLine2.split(","); 
      map2.put(temp[0], temp[1]); 

     } 
    in2.close(); 
    } 
}