File 읽고 데이터 삽입하기 예제

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.infoin.springbatch2.board;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
 
@Service
@Transactional
public class BoardServiceImpl implements BoardService {
 
    @Autowired
    private BoardDAO boardDAO;
    
    @Override
    public List<Board> selectBoardList(Model model) throws Exception {
        
        List<Board> listview = new ArrayList<Board>();
        listview = boardDAO.selectBoardList();
        
        model.addAttribute("listview", listview);
        return listview;
                
    }
    
    @Override
    public void schedulerInsertData() throws Exception {
        System.out.println("boards ::::::::::::::::::: 진입");
        
        // 텍스트 파일 읽기
        
        File inFile = new File("C:\\Users\\Astory\\Desktop\\SpringBatch""sample2.txt");
        BufferedReader br = null;
        Board board = new Board();
        
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile.getPath()), "UTF-8"));
            String line;
            while((line = br.readLine()) != null) {
                System.out.println(line);
                
                String[] boards = line.trim().split(",");
                
                board.setBrdno(boards[0]);
                board.setBrdtitle(boards[1]);
                board.setBrdwriter(boards[2]);
                board.setBrdmemo(boards[3]);
                board.setBrddate(boards[4]);
                
                boardDAO.insertBoard(board);
                
                System.out.println("boards :::::::::::: " + board);
            }
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
    }
}
 
cs





'프로그래밍 > Spring' 카테고리의 다른 글

mybatis 빈설정  (0) 2018.07.14
로그인 예제  (0) 2018.07.13
RSA 암호화 예제 2  (0) 2018.07.13
예제 프로젝트 만들기  (0) 2018.06.27
스프링 MVC : 기본기 1.1 메이븐 웹 프로젝트 생성 및 설정  (0) 2018.01.23
TAGS.

Comments