Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java and CSV > Java Read and Get CSV file



Clound SSD Virtual Server

Java Read and Get CSV file

Java Read and Get CSV file บทความแรกจะเป็นการเขียน Java เพื่ออ่าน CSV ไฟล์ โดยจะมี 2 ตัวอย่าง คือ ตัวอย่างแรกจะใช้การอ่านข้อมูลเหมือนกับ Text file เพียงแต่เมื่อได้ข้อมูลแต่ล่ะ Line จะใช้การ Split ด้วยเครื่องหมายคอมมา (,) เพื่อแบ่งข้อมูลแต่ล่ะ Column จากนั้นใช้การแสดงผลออกทางหน้าจอ ส่วนตัวอย่างที่ 2 จะมีการใช้ Library ไฟล์ jar เข้ามาช่วยในการอ่านไฟล์ CSV

thaicreate.csv
C001,Win Weerachai,[email protected],TH,1000000,600000
C002,John Smith,[email protected],UK,2000000,800000
C003,Jame Born,[email protected],US,3000000,600000
C004,Chalee Angel,[email protected],US,4000000,100000


Java Read and Get CSV file

ไฟล์ CSV เมื่อเปิดผ่านโปรแกรม Excel


Example 1 การอ่านโดยใช้ java.io เหมือนกับการอ่าน Text file

MyClass.java
package com.java.myapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class MyClass {

	public static void main(String[] args) {
		
		String path = "C:\\java\\thaicreate.csv";
		File file = new File(path);
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				String[] arr = line.split(",");
                System.out.print(arr[0]);
                System.out.print(" - ");
                System.out.print(arr[1]);
                System.out.print(" - ");
                System.out.print(arr[2]);
                System.out.print(" - ");
                System.out.print(arr[3]);
                System.out.print(" - ");
                System.out.print(arr[4]);
                System.out.print(" - ");
                System.out.print(arr[5]);
                System.out.println("");
			}
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}	
	
}


Output
C001 - Win Weerachai - [email protected] - TH - 1000000 - 600000
C002 - John Smith - [email protected] - UK - 2000000 - 800000
C003 - Jame Born - [email protected] - US - 3000000 - 600000
C004 - Chalee Angel - [email protected] - US - 4000000 - 100000




Example 2 การอ่านโดยใช้ Library ไฟล์ .jar ของ opencsv โดยในขั้นแรกจะต้องทำการ Download ตัว Library และนำมา Import เข้ามาใน Project ซะก่อนถึงจะใช้งานได้

Dpwnload opencsv


การ Add Jar Library ใช้งาน External Library บน Eclipse IDE ในการเขียน Java


Java Read and Get CSV file

หลังจากที่ได้ไฟล์มาแล้วให้ Copy ไปไว้ในโฟเดอร์ lib ของโปรเจค (ถ้าไม่มีให้สร้างมาใหม่)

Java Read and Get CSV file

ไฟล์ jar อยู่ในโฟเดอร์ lib








Java Read and Get CSV file

คลิกขวาที่ Project เลือก Properties

Java Read and Get CSV file

ในส่วนของ Java Build Path ให้เลือก Add JARs...

Java Read and Get CSV file

เลือกไฟล์ jar ที่ต้องการ

Java Read and Get CSV file

ถูก Add เข้ามาใน Project เรียบร้อยแล้ว จากนั้นเราก็สามารถเรียกใช้งาน Library ของ jar ได้แล้ว

MyClass.java
package com.java.myapp;


import java.io.FileReader;
import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;

public class MyClass {

	public static void main(String[] args) {
		
		String path = "C:\\java\\thaicreate.csv";
		
		try {
			
			CSVReader reader = new CSVReader(new FileReader(path));
			String [] nextLine;
		    while ((nextLine = reader.readNext()) != null) {
		    	System.out.print(nextLine[0]);
                System.out.print(" - ");
                System.out.print(nextLine[1]);
                System.out.print(" - ");
                System.out.print(nextLine[2]);
                System.out.print(" - ");
                System.out.print(nextLine[3]);
                System.out.print(" - ");
                System.out.print(nextLine[4]);
                System.out.print(" - ");
                System.out.print(nextLine[5]);
                System.out.println("");
		    }
		    
		    reader.close();
			    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}	
	
}


Output
C001 - Win Weerachai - [email protected] - TH - 1000000 - 600000
C002 - John Smith - [email protected] - UK - 2000000 - 800000
C003 - Jame Born - [email protected] - US - 3000000 - 600000
C004 - Chalee Angel - [email protected] - US - 4000000 - 100000


สิ่งที่ได้จากการเรียกใช้งาน Library ของไฟล์ jar ซึ่งก็ไม่ค่อยแตกต่างเท่าไหร่นัก







.

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-05-26 11:29:22 / 2017-03-27 14:07:47
  Download : No files
 Sponsored Links / Related

 
Java and CSV file (Comma-separated values)
Rating :

 
Java Mapping Column and CSV file
Rating :

 
Java Create and Write CSV file
Rating :

 
Java Convert Array / ArrayList to CSV file
Rating :

 
Java Convert Export CSV file to HashMap / ArrayList
Rating :

 
Java Convert HashMap/ArrayList Write to CSV file
Rating :

 
Java Import CSV to Database
Rating :

 
Java Export Database to CSV file
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่