0% found this document useful (0 votes)
5 views5 pages

Write and Read CSV in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Write and Read CSV in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HaQT Java Programming

åc ghi file CSV trong Java

Möc löc

1 Sû döng lîp FileReader, FileWriter 2


1.1 V½ dö ghi nëi dung ra file CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 V½ dö ph¥n t½ch có ph¡p mët file CSV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

Comma-Separated Values (CSV) l mët tªp tin v«n b£n b¼nh th÷íng, nâ l÷u trú dú li»u theo tøng
cët, v chia nâ b¬ng d§u ph¥n c¡ch (th÷íng l d§u ph©y “,“).
V½ dö:

1,US,United States
2,VN,Viet Nam
3,AU,Australia

ho°c

"1","US","United States"
"2","VN","Viet Nam"
"3","AU","Australia"

1
HaQT Java Programming

1 Sû döng lîp FileReader, FileWriter

1.1 V½ dö ghi nëi dung ra file CSV

1 package com . g p c o d e r . c s v ;

3 public class Country {

private int id ;

5 private String code ;

private String name ;

7
public Country ( ) {

9 super () ;

11
public Country ( i n t id , String code , String name ) {

13 super () ;

this . id = id ;

15 t h i s . code = code ;

t h i s . name = name ;

17 }

19 @Override

public String toString () {

21 return " C o u n t r y [ i d=" + id + " , c o d e=" + code + " , name=" + name + "]";

23
public int getId () {

25 return id ;

27
public void setId ( int id ) {

29 this . id = id ;

31
public String getCode ( ) {

33 return code ;

35
public void setCode ( S t r i n g code ) {

37 t h i s . code = code ;

39
public String getName ( ) {

41 return name ;

43
public void set Name ( S t r i n g name ) {

45 t h i s . name = name ;

47 }

2
HaQT Java Programming

1 package com . g p c o d e r . c s v ;

3 import java . io . FileWriter ;

import java . i o . IOException ;

5 import java . u t i l . ArrayList ;

import java . u t i l . List ;

7
public class CsvWriterExample {

9 // Delimiter used in CSV file

private static final String COMMA_DELIMITER = " ," ;

11 private static final String NEW_LINE_SEPARATOR = " \n" ;

13 // CSV file header

private static final String FILE_HEADER = " i d , c o d e , name " ;

15
public static void main ( S t r i n g [ ] args ) {

17 String fileName = " data / c o n t r i e s . csv " ;

writeCsvFile ( fileName ) ;

19 }

21 public static void writeCsvFile ( String fileName ) {

// Create new Countrys objects

23 Country country1 = new Country ( 1 , "US" , " United States ") ;

Country country2 = new Country ( 2 , "VN" , " Viet Nam" ) ;

25 Country country3 = new Country ( 3 , "AU" , " Australia ") ;

27 // Create a new list of Country objects

L i s t <C o u n t r y> countries = new A r r a y L i s t <>() ;

29 c o u n t r i e s . add ( c o u n t r y 1 ) ;

c o u n t r i e s . add ( c o u n t r y 2 ) ;

31 c o u n t r i e s . add ( c o u n t r y 3 ) ;

33 FileWriter fileWriter = null ;

35 try {

fileWriter = new FileWriter ( fileName ) ;

37
// Write the CSV file header

39 f i l e W r i t e r . a p p e n d (FILE_HEADER) ;

41 // Add a new line separator after the header

f i l e W r i t e r . a p p e n d (NEW_LINE_SEPARATOR) ;

43
// Write a new Country object list to the CSV file

45 for ( Country country : countries ) {

f i l e W r i t e r . append ( S t r i n g . v a l u e O f ( c o u n t r y . g e t I d ( ) ) ) ;

47 f i l e W r i t e r . a p p e n d (COMMA_DELIMITER) ;

f i l e W r i t e r . append ( c o u n t r y . g e t C o d e ( ) ) ;

49 f i l e W r i t e r . a p p e n d (COMMA_DELIMITER) ;

f i l e W r i t e r . a p p e n d ( c o u n t r y . getName ( ) ) ;

51 f i l e W r i t e r . a p p e n d (NEW_LINE_SEPARATOR) ;

3
HaQT Java Programming

53
S y s t e m . o u t . p r i n t l n ( "CSV file was created successfully ! ! ! ") ;

55
} catch ( Exception e) {

57 System . o u t . p r i n t l n ( " E r r o r in CsvFileWriter ! ! ! ") ;

e . printStackTrace () ;

59 } finally {

try {

61 fileWriter . flush () ;

fileWriter . close () ;

63 } catch ( IOException e) {

System . o u t . p r i n t l n ( " E r r o r while flushing / closing fileWriter ! ! ! ") ;

65 e . printStackTrace () ;

67 }

69 }

Thüc thi ch÷ìng tr¼nh tr¶n, mët file [Link] s³ ÷ñc t¤o ra.

1.2 V½ dö ph¥n t½ch có ph¡p mët file CSV

1 package com . g p c o d e r . c s v ;

3 import java . i o . BufferedReader ;

import java . io . FileReader ;

5 import java . i o . IOException ;

import java . u t i l . ArrayList ;

7 import java . u t i l . List ;

9 public class CsvReaderExample {

11 private static final String COMMA_DELIMITER = " ," ; // Split by comma

13 public static void main ( S t r i n g [ ] args ) {

15 BufferedReader br = null ;

try {

17 String line ;

br = new B u f f e r e d R e a d e r ( new FileReader ( " data / c o n t r i e s . csv " ) ) ;

19
// How to read file in java line by line ?

21 while (( line = br . r e a d L i n e ( ) ) != null ) {

printContry ( parseCsvLine ( l i n e ) ) ;

23 }

25 } catch ( IOException e) {

e . printStackTrace () ;

27 } finally {

4
HaQT Java Programming

try {

29 if ( br != null ) {

br . c l o s e ( ) ;

31 }

} catch ( IOException crunchifyException ) {

33 crunchifyException . printStackTrace () ;

35 }

37
public static L i s t <S t r i n g > parseCsvLine ( String csvLine ) {

39 L i s t <S t r i n g > result = new A r r a y L i s t <S t r i n g > ( ) ;

if ( csvLine != null ) {

41 String [ ] splitData = c s v L i n e . s p l i t (COMMA_DELIMITER) ;

for ( int i = 0; i < splitData . length ; i ++ ) {

43 r e s u l t . add ( s p l i t D a t a [ i ] ) ;

45 }

return result ;

47 }

49 private static void p r i n t C o n t r y ( L i s t <S t r i n g > country ) {

System . o u t . p r i n t l n (

51 " Country [ i d= "

+ country . get (0)

53 + " , c o d e= " + country . get (1)

+ " , name=" + country . get (2)

55 + " ] ") ;

57 }

K¸t qu£ thüc thi ch÷ìng tr¼nh tr¶n:

Command window

1 C o u n t r y [ i d= id , c o d e= code , name=name ]

C o u n t r y [ i d= 1, c o d e= US , name=U n i t e d States ]

3 C o u n t r y [ i d= 2, c o d e= VN, name=V i e t Nam ]

C o u n t r y [ i d= 3, c o d e= AU, name=A u s t r a l i a ]

T i li»u tham kh£o:

• [Link]

You might also like