728x90
๋ฐ์ํ
๐ซ ์ค์ต1
package com.kh.p2.filter.controller;
import java.io.*;
public class Filter {
public void consoleInOut() {
}
public void buf() {
}
public void data() {
FileOutputStream fout = null;
FileInputStream fin = null;
DataOutputStream dout = null;
DataInputStream din = null;
try {
fout = new FileOutputStream("member.txt");
fin = new FileInputStream("member.txt");
dout = new DataOutputStream(fout);
din = new DataInputStream(fin);
//ํ์ผ์ ์๋ฃํ๋ณ๋ก ๊ธฐ๋ก
dout.writeUTF("ํ๊ธธ๋");
dout.writeChar('๋จ');
dout.writeInt(32);
dout.writeUTF("์ ๊ด์");
dout.writeChar('์ฌ');
dout.writeInt(21);
dout.writeUTF("์ฅ๋ณด๊ณ ");
dout.writeChar('๋จ');
dout.writeInt(27);
while(true) {
System.out.println(din.readUTF() + ", " + din.readChar() + ", " +din.readInt());
}
}catch(EOFException e) {
System.out.println("ํ์ผ ์ฝ๊ธฐ ์๋ฃ");
}
catch(IOException e) {
e.printStackTrace();
}finally {
try {
fout.close();
fin.close();
dout.close();
}catch(IOException e) {
}
}
}
public void bufData() {
FileInputStream fin = null;
BufferedInputStream bin = null;
DataInputStream din = null;
try {
fin = new FileInputStream("member.txt");
bin = new BufferedInputStream(fin);
din = new DataInputStream(bin);
System.out.println(din.readUTF());
}catch(FileNotFoundException e) {
System.out.println("ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค.");
}catch(IOException e) {
e.printStackTrace();
}
}
public void reWri() {
File file = new File("exam.txt");
try(FileReader fr = new FileReader(file)) {
int check = 0;
while((check = fr.read()) != -1) {
System.out.print((char)check);
}
}catch (FileNotFoundException e) {
System.out.println("ํ์ผ์ด ์์ต๋๋ค.");
}catch(IOException e) {
System.out.println(e);
}
//๋ณด์กฐ์คํธ๋ฆผ(BufferedWriter, BufferedReader)์ ๋ฌ๊ณ ์ฐ์
try(BufferedWriter bw = new BufferedWriter(new FileWriter("fileReWri.txt"));
BufferedReader br = new BufferedReader(new FileReader("fileReWri.txt"))) {
//bw.write("์ฒซ์งธ์ค\n๋์งธ์ค\n์
์งธ์ค");
bw.write("์ฒซ์งธ์ค");
bw.newLine();
bw.write("๋์งธ์ค");
bw.newLine();
bw.write("์
์งธ์ค");
bw.newLine();
bw.flush(); //close ์ ์ด๋ฏ๋ก flush๋ฅผ ์ํด์ฃผ๋ฉด ๋ฒํผ์ ์์ฌ์๋ ์ํ์ด๋ค.
String str;
while((str=br.readLine())!=null) {
System.out.println(str);
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
package com.kh.p2.filter.run;
import com.kh.p2.filter.controller.Filter;
public class Run {
public static void main(String[] args) {
Filter f = new Filter();
//์ฝ์ ์
์ถ๋ ฅ(ํ์ค ์
์ถ๋ ฅ)
//f.consoleInOut();
//Buffered ๋ณด์กฐ ์คํธ๋ฆผ
//f.buf();
//Data ๋ณด์กฐ ์คํธ๋ฆผ & try with resource๋ฌธ
f.data();
//Buffered & Data ๋ณด์กฐ ์คํธ๋ฆผ
f.bufData();
f.reWri();
}
}
/*
ํ๊ธธ๋, ๋จ, 32
์ ๊ด์, ์ฌ, 21
์ฅ๋ณด๊ณ , ๋จ, 27
ํ์ผ ์ฝ๊ธฐ ์๋ฃ
ํ๊ธธ๋
ํ์ผ์ด ์์ต๋๋ค.
์ฒซ์งธ์ค
๋์งธ์ค
์
์งธ์ค
*/
๐ซ ์ค์ต2
package com.kh.p3.object.model.vo;
import java.io.Serializable;
public class Phone implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2410441448169889016L;
private String maker;
private int price;
public Phone() {
super();
}
public Phone(String maker, int price) {
super();
this.maker = maker;
this.price = price;
}
@Override
public String toString() {
return "Phone [maker=" + maker + ", price=" + price + "]";
}
}
package com.kh.p3.object.model.vo;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = -2795709242007578249L;
private String name;
private int age;
private char gender;
private Phone ph;
public Student() {
}
public Student(String name, int age, char gender, Phone ph) {
this.name = name;
this.age = age;
this.gender = gender;
this.ph = ph;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", gender=" + gender + ", ph=" + ph + "]";
}
}
package com.kh.p3.object.model.vo;
import java.io.Serializable;
public class Teacher implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6964533574688168950L;
private String classR;
private String name;
public Teacher() {
super();
}
public Teacher(String classR, String name) {
super();
this.classR = classR;
this.name = name;
}
@Override
public String toString() {
return "Teacher [classR=" + classR + ", name=" + name + "]";
}
}
package com.kh.p3.object.run;
import java.io.*;
import com.kh.p3.object.model.vo.Phone;
import com.kh.p3.object.model.vo.Student;
import com.kh.p3.object.model.vo.Teacher;
public class Run {
//์ง๋ ฌํ, ์ญ์ง๋ ฌํ
public static void main(String[] args) {
Phone[] ph = {new Phone("์ผ์ฑ", 70), new Phone("LG", 65), new Phone("ํ์จ์ด", 40)};
Student[] st = {new Student("ํ์1", 18, '๋จ', ph[0]),
new Student("ํ์2", 17, '์ฌ', ph[1]),
new Student("ํ์3", 19, '๋จ', ph[2])};
Teacher tc = new Teacher("S", "๊น์ฉ์น");
try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("student.txt"));
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Student.txt"))) {
for(int i=0;i<st.length;i++) {
oos.writeObject(st[i]);
}
oos.writeObject(tc);
//Studentํ ๋ค์ Teacherํ
for(int i=0 ; i<st.length;i++) {
System.out.println((Student)ois.readObject());
}
System.out.println(ois.readObject());
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
}
/*
Student [name=ํ์1, age=18, gender=๋จ, ph=Phone [maker=์ผ์ฑ, price=70]]
Student [name=ํ์2, age=17, gender=์ฌ, ph=Phone [maker=LG, price=65]]
Student [name=ํ์3, age=19, gender=๋จ, ph=Phone [maker=ํ์จ์ด, price=40]]
Teacher [classR=S, name=๊น์ฉ์น]
*/
728x90
๋ฐ์ํ
'Dev > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๊ฐ์ฒด(16)] ์ ์ถ๋ ฅ, ์คํธ๋ฆผ (0) | 2022.05.15 |
---|---|
[๊ฐ์ฒด(15)] ์ค์ต7, ์ค์ต8 (0) | 2022.05.15 |
[๊ฐ์ฒด(13)] ์ค์ต5, ์ค์ต6 (0) | 2022.05.15 |
[๊ฐ์ฒด(12)] ๋คํ์ฑ(Polymorphism) (0) | 2022.05.15 |
[๊ฐ์ฒด(11)] ์ค์ต3, ์ค์ต4 (0) | 2022.05.15 |