2010-06-06 15 views
5

पढ़ और लिखना मैं 2 सीएसवी फाइलें पढ़ रहा हूं: store_inventory & new_acquisitions
मैं सीएसवी फ़ाइल की तुलना new_acquisitions से तुलना करने में सक्षम होना चाहता हूं। 1) यदि आइटम नाम मिलान केवल store_inventory में मात्रा को अद्यतन करते हैं। 2) यदि new_acquisitions में एक नया आइटम है जो store_inventory में मौजूद नहीं है, तो इसे store_inventory पर जोड़ें।जावा: सीएसवी फ़ाइल

यहां मैंने जो किया है, लेकिन यह बहुत अच्छा नहीं है। मैंने टिप्पणियां जोड़ दीं जहां मुझे & जोड़ने की आवश्यकता है।
उपरोक्त कार्यों को करने के लिए कोई सलाह या कोड बहुत अच्छा होगा! धन्यवाद।

File new_acq = new File("/src/test/new_acquisitions.csv"); 
    Scanner acq_scan = null; 
    try { 
     acq_scan = new Scanner(new_acq); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    String itemName; 
    int quantity; 
    Double cost; 
    Double price; 

    File store_inv = new File("/src/test/store_inventory.csv"); 
    Scanner invscan = null; 
    try { 
     invscan = new Scanner(store_inv); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    String itemNameInv; 
    int quantityInv; 
    Double costInv; 
    Double priceInv; 


    while (acq_scan.hasNext()) { 
     String line = acq_scan.nextLine(); 
     if (line.charAt(0) == '#') { 
      continue; 
     } 
     String[] split = line.split(","); 

     itemName = split[0]; 
     quantity = Integer.parseInt(split[1]); 
     cost = Double.parseDouble(split[2]); 
     price = Double.parseDouble(split[3]); 


     while(invscan.hasNext()) { 
      String line2 = invscan.nextLine(); 
      if (line2.charAt(0) == '#') { 
       continue; 
      } 
      String[] split2 = line2.split(","); 

      itemNameInv = split2[0]; 
      quantityInv = Integer.parseInt(split2[1]); 
      costInv = Double.parseDouble(split2[2]); 
      priceInv = Double.parseDouble(split2[3]); 


      if(itemName == itemNameInv) { 
       //update quantity 

      } 
     } 
     //add new entry into csv file 

    } 

किसी भी मदद के लिए धन्यवाद। =]

+1

आप अधिक और बेहतर जवाब पाने यदि आप वास्तव में एक सवाल पूछ मिल जाएगा। –

उत्तर

5

सुझाव दें कि आप मौजूदा सीएसवी पार्सर जैसे कि Commons CSV या Super CSV पहिया को पुनर्निर्मित करने के बजाय उपयोग करें। अपने जीवन को बहुत आसान बनाना चाहिए।

+0

मैंने opencsv डाउनलोड किया, लेकिन मुझे नहीं पता कि लाइब्रेरी का उपयोग कैसे करें। क्या आप मुझे सही दिशा में इंगित कर सकते हैं। मैं netbeans का उपयोग कर – nubme

+0

ओपनसीएसवी के साथ पढ़ने और लिखने के उदाहरणों के लिए, http://opencsv.sourceforge.net/#how-to-read देखें – seangrieve

1

आपके द्वारा किए जा रहे ऑपरेशन के लिए आपके नए अधिग्रहण में प्रत्येक आइटम के लिए आवश्यकता होगी, आपको प्रत्येक आइटम को एक मैच के लिए सूची में खोजना होगा। यह न केवल कुशल नहीं है, बल्कि आपके इन्वेंट्री फ़ाइल के लिए सेट किए गए स्कैनर को प्रत्येक आइटम के बाद रीसेट करने की आवश्यकता होगी।

मैं सुझाव दूंगा कि आप अपने नए अधिग्रहण और संग्रह में अपनी सूची जोड़ दें और फिर अपने नए अधिग्रहणों पर फिर से शुरू करें और अपने इन्वेंट्री संग्रह में नया आइटम देखें। यदि आइटम मौजूद है, तो आइटम को अपडेट करें। यदि ऐसा नहीं होता है, तो इसे सूची संग्रह में जोड़ें। इस गतिविधि के लिए, एक सूची वस्तु रखने के लिए एक साधारण वर्ग लिखना अच्छा हो सकता है। इसका इस्तेमाल नए अधिग्रहण और सूची दोनों के लिए किया जा सकता है। एक तेज़ लुकअप के लिए, मैं सुझाव दूंगा कि आप अपने इन्वेंट्री संग्रह के लिए हैशसेट या हैश मैप का उपयोग करें।

प्रक्रिया के अंत में, अपनी सूची फ़ाइल में परिवर्तनों को जारी रखना न भूलें।

3

आपका कार्यान्वयन line.split(",") का उपयोग करके अल्पविरामों पर लाइन को तोड़ने की सामान्य गलती करता है। यह काम नहीं करता है क्योंकि मूल्यों में स्वयं को अल्पविराम हो सकता है। यदि ऐसा होता है, तो मूल्य उद्धृत किया जाना चाहिए, और आपको कोट्स के भीतर अल्पविरामों को अनदेखा करने की आवश्यकता है। split विधि यह नहीं कर सकती - मुझे यह गलती बहुत पसंद है।

यहाँ एक कार्यान्वयन है कि इसे सही ढंग से करता है का स्रोत है: http://agiletribe.wordpress.com/2012/11/23/the-only-class-you-need-for-csv-files/

1

जावा के रूप में मूल रूप से CSV फ़ाइलों को पार्स का समर्थन नहीं करता है, हम तीसरे पक्ष के पुस्तकालय का सहारा लेना पड़। Opencsv इस उद्देश्य के लिए उपलब्ध सर्वोत्तम लाइब्रेरी में से एक है। यह खुला स्रोत है और इसे अपाचे 2.0 लाइसेंस के साथ भेज दिया गया है जो वाणिज्यिक उपयोग के लिए इसे संभव बनाता है।

यहां, this link आपको और अन्य स्थितियों में मदद करनी चाहिए!

1

खुला स्रोत पुस्तकालय uniVocity-parsers की मदद से, आप निम्नलिखित के रूप में सुंदर स्वच्छ कोड के साथ विकसित कर सकता:

private void processInventory() throws IOException { 
    /** 
    * --------------------------------------------- 
    * Read CSV rows into list of beans you defined 
    * --------------------------------------------- 
    */ 
    // 1st, config the CSV reader with row processor attaching the bean definition 
    CsvParserSettings settings = new CsvParserSettings(); 
    settings.getFormat().setLineSeparator("\n"); 
    BeanListProcessor<Inventory> rowProcessor = new BeanListProcessor<Inventory>(Inventory.class); 
    settings.setRowProcessor(rowProcessor); 
    settings.setHeaderExtractionEnabled(true); 

    // 2nd, parse all rows from the CSV file into the list of beans you defined 
    CsvParser parser = new CsvParser(settings); 
    parser.parse(new FileReader("/src/test/store_inventory.csv")); 
    List<Inventory> storeInvList = rowProcessor.getBeans(); 
    Iterator<Inventory> storeInvIterator = storeInvList.iterator(); 

    parser.parse(new FileReader("/src/test/new_acquisitions.csv")); 
    List<Inventory> newAcqList = rowProcessor.getBeans(); 
    Iterator<Inventory> newAcqIterator = newAcqList.iterator(); 

    // 3rd, process the beans with business logic 
    while (newAcqIterator.hasNext()) { 

     Inventory newAcq = newAcqIterator.next(); 
     boolean isItemIncluded = false; 
     while (storeInvIterator.hasNext()) { 
      Inventory storeInv = storeInvIterator.next(); 

      // 1) If the item names match just update the quantity in store_inventory 
      if (storeInv.getItemName().equalsIgnoreCase(newAcq.getItemName())) { 
       storeInv.setQuantity(newAcq.getQuantity()); 
       isItemIncluded = true; 
      } 
     } 

     // 2) If new_acquisitions has a new item that does not exist in store_inventory, 
     // then add it to the store_inventory. 
     if (!isItemIncluded) { 
      storeInvList.add(newAcq); 
     } 
    } 
} 

बस इस कोड नमूना मैं अपनी आवश्यकताओं के अनुसार बाहर काम का पालन करें। ध्यान दें कि पुस्तकालय ने सीएसवी फाइलों को पार्स करने के लिए सरलीकृत एपीआई और महत्वपूर्ण प्रदर्शन प्रदान किया है।

0

सीएसवी

को
public void writeCSV() { 

     // Delimiter used in CSV file 
     private static final String NEW_LINE_SEPARATOR = "\n"; 

     // CSV file header 
     private static final Object[] FILE_HEADER = { "Empoyee Name","Empoyee Code", "In Time", "Out Time", "Duration", "Is Working Day" }; 

     String fileName = "fileName.csv"); 
     List<Objects> objects = new ArrayList<Objects>(); 
     FileWriter fileWriter = null; 
     CSVPrinter csvFilePrinter = null; 

     // Create the CSVFormat object with "\n" as a record delimiter 
     CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR); 

     try { 
      fileWriter = new FileWriter(fileName); 

      csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); 

      csvFilePrinter.printRecord(FILE_HEADER); 

      // Write a new student object list to the CSV file 
      for (Object object : objects) { 
       List<String> record = new ArrayList<String>(); 

       record.add(object.getValue1().toString()); 
       record.add(object.getValue2().toString()); 
       record.add(object.getValue3().toString()); 

       csvFilePrinter.printRecord(record); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       fileWriter.flush(); 
       fileWriter.close(); 
       csvFilePrinter.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
संबंधित मुद्दे