일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 검색기능
- 입력메소드
- 국민취업지원제도
- 로그아웃
- 비밀번호변경
- 내배카
- jdbc환경설정
- 회원정보수정
- 내일배움카드
- emmet환경설정
- 비밀번호암호화
- 인코딩
- jsp기본
- 정처기
- mvc
- github
- 회원탈퇴
- 페이징
- 관리자회원조회
- 국비학원
- forward
- redirect
- 권한변경
- 배열
- 페이지 재사용
- 국취제
- jdbc설정
- Git
- live server 환경설정
- 별찍기
- Today
- Total
기록
*21일차 실습 본문
[실습문제 1]
- 패키지 : com.io.test1
- 클래스 : Test1
>> main() 포함
=> fileSave() 메소드 실행함
>> 메소드 추가함
+ fileSave() : void
=> 키보드로 사용할 파일명을 입력받음
BufferedReader의 readLine() 사용함
=> 파일출력용 스트림 객체 생성함
FileWriter 사용함
=> 화면에 "파일에 저장할 내용을 입력하시오." 출력
입력값을 읽어들여서 바로 파일에 기록 저장처리
반복실행함
=> "exit"가 입력되면, 반복은 종료하고
화면에 "파일에 성공적으로 저장되었습니다." 출력하고
=> 파일출력 스트림을 닫음.
private void filesave() {
InputStream is = System.in;
InputStreamReader isr =new InputStreamReader(is);
BufferedReader br =new BufferedReader(isr);
String filename =null;
try {
System.out.println("파일명 입력");
filename=br.readLine();
BufferedWriter fw = new BufferedWriter(new FileWriter(filename,true));
while(true) {
System.out.print("파일에 저장할 내용을 입력하시오: ");
String data = br.readLine();
if(data.equals("exit")) {
fw.close();
break;
}else {
fw.write(data+"\n");
fw.flush();
}
}
System.out.println("파일에 성공적으로 저장되었습니다.");
}catch(IOException e) {
e.printStackTrace();
}
}
[실습문제 2]
- 위에서 작성한 클래스에 메소드 추가함
>> 메소드
+ fileRead() : void
=> 키보드로 읽을 대상파일명을 입력받음
=> 파일 읽기용 스트림 객체 생성함 : FileReader 사용
=> 파일 안의 내용을 읽어서, StringBuilder 에 보관함
=> 다 읽은 다음, StringBuilder 에 보관된 값을
String으로 바꾸어 화면에 출력함
public class Test1 {
public static void main(String[] args) {
Test1 test = new Test1();
test.filesave();
test.fileRead();
}
public void fileRead() {
FileReader fr = null;
BufferedReader fbr = null;
String fileName = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> 읽어올 파일명을 입력하세요 : ");
try {
fileName = br.readLine();
// FileReader는 파일에 읽기용 문자기반스트림.
fbr = new BufferedReader(new FileReader("test1/" + fileName));
// 읽어온 데이터를 담을 정수형변수
String data = null;
StringBuilder sb = new StringBuilder();
while ((data = fbr.readLine()) != null) {
System.out.println(data);
sb.append(data);
sb.append("\n");
}
// 출력
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
System.out.println("[" + fileName + "] 해당파일을 찾을 수 없습니다. ");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fbr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
[실습문제 3]
- 클래스 : 07_OOP프로젝트 com.oop.employee.model.vo.Employee 복사
-> com.io.employee.model.vo.Employee생성.
- 실행용클래스 : com.io.employee.run.EmployeeTest
메소드 : saveEmployee();
=> Employee객체배열을 사용할 것.
=> 3개의 Employee객체생성후 ObjectOutputStream을 통해서 empoloyee.dat에 쓰기.
메소드 : loadEmployee();
=> employee.dat 파일의 내용을 ObjectInputStream을 이용해서 읽어와
=> Employee 객체에 저장하고
=> 화면 출력 확인함
package com.io.employee.run;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.io.employee.model.vo.Employee;
/**
* [실습문제 3]
* <p>
* - 클래스 : 07_OOP프로젝트 com.oop.employee.model.vo.Employee 복사 ->
* com.io.employee.model.vo.Employee생성. - 실행용클래스 :
* com.io.employee.run.EmployeeTest 메소드 : saveEmployee(); => Employee객체배열을 사용할
* 것. => 5개의 Employee객체생성후 DataOutputStream을 통해서 empoloyee.dat에 쓰기.
*
* <p>
* 메소드 : loadEmployee(); => employee.dat 파일의 내용을 데이터 종류별로 읽어서 => Employee 객체에
* 저장하고 => 화면 출력 확인함
*
*
*
*/
public class EmployeeTest {
public static void main(String[] args) {
saveEmployee();
loadEmployee();
}
public static void loadEmployee() {
Employee[] empArr = new Employee[5];
try (ObjectInputStream ois = new ObjectInputStream(
new BufferedInputStream(new FileInputStream("emp/employee.dat")))) {
for (int i = 0; i < empArr.length; i++) {
empArr[i] = (Employee) ois.readObject();
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
// 확인
for (int i = 0; i < empArr.length; i++) {
empArr[i].printEmployee();
}
}
public static void saveEmployee() {
Employee[] empArr = new Employee[5];
empArr[0] = new Employee(0, "제갈량", '남', "010-3131-3131", "영업부", 30000000, 0.15);
empArr[1] = new Employee(1, "조자룡", '남', "010-8901-2345", "영업부", 20000000, 0.1);
empArr[2] = new Employee(2, "유비", '남', "010-1234-5678", "영업부", 50000000, 0.2);
empArr[3] = new Employee(3, "조조", '남', "010-5454-4545", "영업부", 34000000, 0.05);
empArr[4] = new Employee(4, "초선", '여', "010-8787-7878", "영업부", 24000000, 0.1);
try (ObjectOutputStream oos = new ObjectOutputStream(
new BufferedOutputStream(new FileOutputStream("emp/employee.dat")))) {
for (int i = 0; i < empArr.length; i++) {
Employee emp = empArr[i];
oos.writeObject(emp);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("직원저장완료!");
}
}
--------------------------------------------
package com.io.employee.model.vo;
import java.io.Serializable;
public class Employee implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int empNo;
private String name;
private char gender;
private String phone;
private String dept;
private int salary;
private double bonusPct;
public Employee(){}
public Employee(int empNo, String name, char gender, String phone) {
super();
this.empNo = empNo;
this.name = name;
this.gender = gender;
this.phone = phone;
}
public Employee(int empNo, String name, char gender, String phone, String dept, int salary, double bonusPct) {
super();
this.empNo = empNo;
this.name = name;
this.gender = gender;
this.phone = phone;
this.dept = dept;
this.salary = salary;
this.bonusPct = bonusPct;
}
public void printEmployee(){
System.out.printf("%d, %s, %c, %s, %s, %d, %f%n", empNo, name, gender, phone, dept, salary, bonusPct);
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public double getBonusPct() {
return bonusPct;
}
public void setBonusPct(double bonusPct) {
this.bonusPct = bonusPct;
}
}