자바(Java)/자바 로봇 프로그래밍

[자바파싱] 파일내용 읽기 - java io 한글깨짐 처리

xemaker 2024. 7. 25. 13:31
package robot_bbs_insert;



import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;



public class ReadFile {

	

	public static String DIR_NAME = "D:\\bet365\\";

	public static String FILE_NAME = "baseball.txt";

	

	

	public void readFiles(String dir) throws Exception {

		File dirFile=new File(dir);

		String [] filenames = dirFile.list();

		for(String filename:filenames) {

			System.out.println(filename);

		}

	}

	

	public void readFile(String dirFile) throws Exception {

		

		//BufferedReader in =new BufferedReader(new FileReader(dirFile));

		

		//한글깨질때

		FileInputStream input=new FileInputStream(dirFile);

		InputStreamReader reader=new InputStreamReader(input, "UTF-8");

		BufferedReader in=new BufferedReader(reader);

		

		String str;

		StringBuffer response=new StringBuffer();

		while((str=in.readLine()) != null) {

			response.append(str);

		}

		in.close();

		String html=response.toString();

		System.out.println(html);

	}

	

	public static void main(String[] args){

		System.out.println("시작");

		try {

			ReadFile rf=new ReadFile();

			//rf.readFiles(DIR_NAME);

			rf.readFile(DIR_NAME+FILE_NAME);

			

		}catch(Exception e) {

			e.printStackTrace();

		}

		System.out.println("끝");

	}



}