[๊ฐ์ฒด(16)] ์ž…์ถœ๋ ฅ, ์ŠคํŠธ๋ฆผ

2022. 5. 15. 19:45ยทDev/Java
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' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ฐธ์กฐํ˜• ๊ฐ์ฒด, String, StringBuilder ๋น„๊ตํ•ด๋ณด๊ธฐ  (1) 2025.04.15
[๊ฐ์ฒด(17)] ์‹ค์Šต9  (0) 2022.05.15
[๊ฐ์ฒด(15)] ์‹ค์Šต7, ์‹ค์Šต8  (0) 2022.05.15
[๊ฐ์ฒด(14)]  (0) 2022.05.15
[๊ฐ์ฒด(13)] ์‹ค์Šต5, ์‹ค์Šต6  (0) 2022.05.15
'Dev/Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • ์ฐธ์กฐํ˜• ๊ฐ์ฒด, String, StringBuilder ๋น„๊ตํ•ด๋ณด๊ธฐ
  • [๊ฐ์ฒด(17)] ์‹ค์Šต9
  • [๊ฐ์ฒด(15)] ์‹ค์Šต7, ์‹ค์Šต8
  • [๊ฐ์ฒด(14)]
๋ธ”ํ˜œ
๋ธ”ํ˜œ
  • ๋ธ”ํ˜œ
    Blehye Dev
    ๋ธ”ํ˜œ
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (133)
      • Dev (69)
        • Java (45)
        • HTML5 CSS3 (16)
        • Javascript (2)
        • ๊ตญ๋น„ํ•™์› (4)
        • Error! (2)
      • Algorithm (12)
        • PS (9)
        • Algorithm (3)
      • English (22)
        • Webtoon (6)
        • Grammar In Use (15)
      • DAILY (20)
        • Trip (10)
        • Musical (2)
        • Swimming (5)
        • Book (1)
        • Test (1)
      • etc. (10)
        • Display (10)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • STUDY
    • DAILY
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    SKCTํŒ
    ํ˜ผ์ž์—ฌํ–‰
    ๋ถ€์‚ฐํ˜ผ์ž์—ฌํ–‰
    ์†ก๋„ํ•ด์ˆ˜์š•์žฅ
    SKํ•˜์ด๋‹‰์Šค
    SKCT
    SKCTํ›„๊ธฐ
    SKCT๊ฟ€ํŒ
    ํƒœ์ข…๋Œ€
    SKCT์‹œํ—˜
    ํ™๋Œ€๊ฐœ๋ฏธ
    ์ธ์ ์„ฑ
    ๋ถ€์‚ฐ์—ฌํ–‰
    ๊ฐ์ฒœ๋ฌธํ™”๋งˆ์„
    ํ•˜์ด๋‹‰์Šค
    ์—ฌ์žํ˜ผ์ž์—ฌํ–‰
    ํฐ์—ฌ์šธ๋ฌธํ™”๋งˆ์„
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • ๋ฐ˜์‘ํ˜•
    250x250
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
๋ธ”ํ˜œ
[๊ฐ์ฒด(16)] ์ž…์ถœ๋ ฅ, ์ŠคํŠธ๋ฆผ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”