본문 바로가기
Java 문제

[Java] 각 로그의 라인은 객체로 변환 후 처리

by 두리두리안 2021. 5. 30.

LogModel

package Version2;

public class LogModelPart {
	private String time;
    private String id;
    private String status;
    
    public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
	public LogModelPart(String line) throws IllegalAccessError {
        String[] splitOption = line.split(" ");
            this.time = splitOption[0];
            this.id = splitOption[1];
            this.status = splitOption[2];
            
            

    }
    @Override
    public String toString() {
        //toString 메소드란 객체가 가지고 있는 정보나 값들을 문자열로 만들어 리턴하는 메소드이다.
        return "LogModel{" +
                "time='" + time + '\'' +
                ", id='" + id + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}

 

Main.java

package Version2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
	
	  public static void main(String[] args) {
	        List<LogModelPart> logList = new ArrayList<>();

	        BufferedReader bufferedReader = new Main().fileReader();
	        try {
	            String line;
	            while ((line = bufferedReader.readLine()) != null)
	            {
	                try {
	                    LogModelPart model = new LogModelPart(line);
	                    //클래스를 new로 생성
	                    logList.add(new LogModelPart(line));
	                   
	           
	                } catch (Exception e) {
	                    System.out.println(e);
	                }
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        System.out.println(logList);
	    }
	  
	public BufferedReader fileReader() {
		FileReader fileReader;
		BufferedReader bufferedReader = null;
		
		try {
			String filePath= "C:\\Users\\최민준\\OneDrive\\바탕 화면\\말똥가리\\0527.txt";
			fileReader = new FileReader(filePath);
			bufferedReader = new BufferedReader(fileReader);
			
		}catch(FileNotFoundException e) {
			System.out.println("파일을 읽을수 없습니다.");
		}
		return bufferedReader;
	}
}