티스토리 뷰
students.csv
id,name
1,A
2,B
3,C
public class StudentInfo { private int id; private String name; public StudentInfo(int id, String name){ this.id = id; this.name = name; } public int studentId(){ return id; } @Override public String toString(){ return "Student Id: " + this.id + ", " + "name: " + this.name + "\n"; } }
public class Main {
public static void main(String[] args) throws FileNotFoundException {
//ArrayList
ArrayList<StudentInfo> studentInfos = new ArrayList<>();
try {
//read the CSV file
BufferedReader sc = new BufferedReader(new FileReader("src" + File.separator + "csv_input" + File.separator + "students.csv"));
//skip the first line
String re = sc.readLine(); //id, name
//while loop until the end of file
while ((re = sc.readLine()) != null) {
//while (re != null) { // If only 're' assigned, NumberFormatException error will occur because 're' has 'id','name' as String
//split comma-separated
String[] data = re.split(",");
//way1. create method
//assign to ArrayList
//StudentInfo student = createFile(data);
//studentInfos.add(student);
//way2. creating object inside the add method.
studentInfos.add(new StudentInfo(Integer.parseInt(data[0]), data[1]));
//re = sc.readLine(); // If the data is String, assigning 're' and put this line to read next line is fine.
//But, It occurs Error here. Because, this example have to convert String to Int to calculate.
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//create text file
File file = new File("src" + File.separator + "txt_output" + File.separator + "reportCard.txt");
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getPath()), StandardCharsets.UTF_8));
try {
for (int i = 0; i < studentInfos.size(); i++) {
output.write(studentInfos.get(i).toString());
}
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// way1. declare createFile method to assign data
// private static StudentInfo createFile(String[] s) {
// int id = Integer.parseInt(s[0]);
// String name = s[1];
// return new StudentInfo(id, name);
// }
}
|
reportCard.txt
Student Id: 1, name: A
Student Id: 2, name: B
Student Id: 3, name: C
'JAVA' 카테고리의 다른 글
Write to file in Java - double output using BufferedWriter (0) | 2019.07.08 |
---|---|
File I/O - Decorator pattern, File.separator (0) | 2019.07.05 |
Convert Array to ArrayList, ArrayList to Set (0) | 2019.07.01 |
enum in Java (0) | 2019.06.24 |
Exception handling in Java (0) | 2019.06.24 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- spring boot application
- easy javascript algorithm
- HackerRank Algorithm
- easy algorithm
- C++
- algorithm
- repeat()
- compareTo()
- Collection Framework
- ... in Javascript
- 프로그래머스 알고리즘문제
- HashMap
- hackerrank javascript solution
- rest parameter
- 프로그래머스
- hackerrank solution
- math.abs
- substring()
- Javascript Algorithm
- code refactoring
- java
- math.max
- javascript
- string class in java
- 알고리즘
- 프로그래머스 알고리즘
- hackerrank javascript
- equals()
- Object type casting
- hackerrank
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함