관리 메뉴

거니의 velog

(18) 토요일 수업 3 본문

대덕인재개발원/대덕인재개발원_final project

(18) 토요일 수업 3

Unlimited00 2024. 1. 8. 09:00
package test.java01.test02.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class FileUtil {
	public static void main(String[] args) throws Exception {
		String path = "D:\\A_TeachingMaterial\\06_JSP_Spring\\test.java01\\src\\test\\java01\\test02\\base\\ParamMap.java";
		read3(path);
	}
	
	public static void read3(String path) throws Exception {
		// 작업절차 : 객체생성 -> 작업 -> 종료
		BufferedReader fr = new BufferedReader(new FileReader(path));
		//char[] ch = new char[500]; // 자동으로 담을 그릇을 만들었음.
		while(true) {
			String readLine = fr.readLine();
			if(readLine==null) { break; }
			System.out.println(readLine);
		}
		fr.close();
	}
	
	public static void read2(String path) throws Exception {
		// 작업절차 : 객체생성 -> 작업 -> 종료
		FileReader fr = new FileReader(path);
		char[] ch = new char[500];  // 수동으로 담을 그릇을 만들었음. 
		while(true) {
			int read = fr.read(ch);  // read : 읽은 글자의 수, ch : 읽은 데이터
			if(read==-1) {
				break;
			}
			String str = new String(ch, 0, read);
			//System.out.print(str);
			System.out.println(read);
		}
		fr.close();
	}
	
	public static void read(String path) throws Exception {
		// 작업절차 : 객체생성 -> 작업 -> 종료
		FileReader fr = new FileReader(path);
		//char[] ch = new char[1024];
		while(true) {
			int read = fr.read();  // read : 읽은 문자의 값
			if(read==-1) {
				break;
			}
			System.out.print((char)read);
		}
		fr.close();
	}

	public static List<String> readList(String path) {
		// TODO Auto-generated method stub
		return null;
	}

}
package test.java01.test02;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import test.java01.test02.util.FileUtil;

public class Test02 {
	public static void main(String[] args) throws UnsupportedEncodingException {
		
//		Properties properties = System.getProperties();
//		Set<Object> keySet = properties.keySet();
//		for (Object o : keySet) {
//			if(String.valueOf(o).contains("encoding")) {
//				System.out.println(o + " : " + properties.getProperty(String.valueOf(o)));
//			}
//		}
//		
//		
//		// 한글 
//		String test = "한글";
//		System.out.println(test.getBytes().length);
//		System.out.println(test.getBytes("utf-8").length);
		
		String path = "D:\\A_TeachingMaterial\\06_JSP_Spring\\test.java01\\src\\test\\java01\\test02\\base\\ParamMap.java";
//		StringBuffer sb = FileUtil.read(path);
//		List<String> list =  FileUtil.readList(path);
	}

}

package test.java01.test03;

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

public class FileUtil {
	
	public static void main(String[] args) {
		
		StringBuffer read = FileUtil.read("src\\test\\java01\\test03\\Test01.java");
		System.out.println(read);
		List<String> readLines = FileUtil.readLines("src\\test\\java01\\test03\\Test01.java");
		for (String s : readLines) {
			System.out.println(s);
		}
	}
	
	
	
	public static StringBuffer read(String path) {
		
		StringBuffer sb = new StringBuffer(); 
		try(BufferedReader br = new BufferedReader(new FileReader(path));) {
			boolean isFirstLine = true; 
			while(true) {
				String readLine = br.readLine();
				if(readLine==null) {break; }
				if(isFirstLine) {
					sb.append(readLine);
					isFirstLine = false; 
				}else {
					sb.append("\r\n"+readLine);
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb; 
	}
	
	public static StringBuffer read(File file) {
		return read(file.getAbsolutePath());
	}
	
	public static List<String> readLines(String path) {
		List<String> list = new ArrayList<>();
		try(BufferedReader br = new BufferedReader(new FileReader(path));) {
			boolean isFirstLine = true; 
			while(true) {
				String readLine = br.readLine();
				if(readLine==null) {break; }
				list.add(readLine);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list; 
	}
	
	public static List<String> readLines(File file) {
		return readLines(file.getAbsolutePath());
	}
}
package test.java01.test03;

import java.io.File;

public class Test01 {
	public static void main(String[] args) {
		
		// 지금 읽고 있는 파일경로 -> IO 
		// D:\A_TeachingMaterial\06_JSP_Spring\test.java01
		// D:\A_TeachingMaterial\06_JSP_Spring\test.java01\src\test\java01\test03\Test01.java
		{
			File file = new File("src\\test\\java01\\test03\\Test01.java");
			System.out.println(file.getAbsolutePath());
			System.out.println(file.exists());
		}
		{
			File file = new File("D:\\A_TeachingMaterial\\06_JSP_Spring\\test.java01\\src\\test\\java01\\test03\\Test01.java");
			System.out.println(file.getAbsolutePath());
			System.out.println(file.exists());
		}
		
		
		
		
	}
}
package test.java01.test03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class Test02 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = null;
		try {
			File file = new File("src\\test\\java01\\test03\\Test01.java");
			while(true) {
				String readLine = br.readLine();
				if(readLine==null) break; 
				System.out.println(readLine);
			}
		}finally {
			if(br!=null) {
				br.close(); 
			}
		}
		
		
	}
}
package test.java01.test03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test03 {
	public static void main(String[] args) {
		
		File file = new File("src\\test\\java01\\test03\\Test01.java");
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			///////////////////////////////////////////
			while(true) {
				String readLine = null;
				try {
					readLine = br.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(readLine==null) break; 
				System.out.println(readLine);
				br.close();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		///////////////////////////////////////////
		try {
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
}
package test.java01.test03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test04 {
	public static void main(String[] args)  {
		
		File file = new File("src\\test\\java01\\test03\\Test01.java");
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(file));
			while(true) {
				String readLine = null;
					readLine = br.readLine();
				if(readLine==null) break; 
				System.out.println(readLine);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			if(br!=null) {
				br.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
	}
}
package test.java01.test03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;

public class Test05 {
	public static void main(String[] args) throws Exception {
		BufferedReader br = null;
		try {
			File file = new File("src\\test\\java01\\test03\\Test01.java");
			while(true) {
				String readLine = br.readLine();
				if(readLine==null) break; 
				System.out.println(readLine);
			}
		}catch(FileNotFoundException e) {	
			e.printStackTrace();
			// 처리작업
			throw e;
		}finally {
			if(br!=null) {
				br.close(); 
			}
		}
		
		
	}

		public static void main(String[] args) throws Exception {
			BufferedReader br = null;
			try {
				File file = new File("src\\test\\java01\\test03\\Test01.java");
				while(true) {
					String readLine = br.readLine();
					if(readLine==null) break; 
					System.out.println(readLine);
				}
			}finally {
				if(br!=null) {
					br.close(); 
				}
			}
			
			
		}
	}
}
package test.java01.test03;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Test06 {
	public static void main(String[] args) {
		// 1.8 
		File file = new File("src\\test\\java01\\test03\\Test01.java");
		try(
				File file2 = new File("src\\test\\java01\\test03\\Test01.java");
				BufferedReader br = new BufferedReader(new FileReader(file));
				
				BufferedReader br2 = new BufferedReader(new FileReader(file));
				BufferedReader br3 = new BufferedReader(new FileReader(file));
		) {
			while(true) {
				String readLine = br.readLine();
				if(readLine==null) break; 
				System.out.println(readLine);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

'대덕인재개발원 > 대덕인재개발원_final project' 카테고리의 다른 글

(20) 토요일 수업 4  (0) 2024.01.13
(19) 보강 12  (0) 2024.01.08
(17) Tomcat Servers 설정  (1) 2024.01.05
(16) 보강 11  (0) 2024.01.05
(15) Oracle SQL 쿼리문  (2) 2024.01.04