본문 바로가기
자바 풀스택 공부

Day 57. [Java] FileCopy, DataInput / OutputStream, ObjectOutput

by seung_nari 2022. 3. 28.

FileCopy

 

package chap15;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
	public static void main(String[] args) throws IOException {
		FileInputStream fis = new FileInputStream(args[0]);
		FileOutputStream fos = new FileOutputStream(args[1]);
		int data = 0;
		
		int cnt = 0;
		while((data=fis.read())!= -1) {
			char c = (char)data;
			System.out.print(c);
			fos.write(data);
			cnt++;
		}
		System.out.println(cnt + "bytes");
		
	}
}

DataInput/OutputStream

 

DataInputStream

 

package chap15;

import java.io.DataInputStream;
import java.io.FileInputStream;

public class DataInputEx3 {
	public static void main(String[] args) throws Exception{
		DataInputStream dis = new DataInputStream(new FileInputStream("result3.dat"));
		
		System.out.println(dis.readChar());
		System.out.println(dis.readChar());
		System.out.println(dis.readChar());
		System.out.println(dis.readChar());
		System.out.println(dis.readUTF());
		byte[] bytes = new byte[3];
		dis.read(bytes);
		System.out.println(new String(bytes, "ms949"));
		
		for(byte b : bytes) {
			System.out.printf("%x\n", b);
		}
	}
}

 

DataOutputStream

 

package chap15;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;

public class DataOutputEx3 {
	public static void main(String[] args) throws Exception{
		DataOutputStream dos = new DataOutputStream(
				new BufferedOutputStream(new FileOutputStream("result3.dat")));
		
		dos.writeChars("a가"); // 4
		dos.writeChar('a'); // 2
		dos.writeChar('가'); // 2
		dos.writeUTF("a가"); // 6
		dos.write("a가".getBytes("ms949")); // 4
		
		byte[] b = "a가".getBytes();
		System.out.println(b.length);
		
		dos.close();
	}
}

objectoutput

 

 

ObjectInput

package chap15;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

public class ObjectInputEx {
	public static void main(String[] args) throws Exception {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.ser"));
		List<Student> obj = (List<Student>)ois.readObject();
		
		for(Student s : obj) {
			System.out.println(s);
		}
	}
}

 

ObejctOutput

 

package chap15;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ObjectOutputEx {
	public static void main(String[] args) throws Exception{
		List<Student> list = new ArrayList<>();
		list.add(new Student(1, "1�浿", 80));
		list.add(new Student(2, "2�浿", 90));
		list.add(new Student(3, "3�浿", 70));
		
		System.out.println(list);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.ser"));
		oos.writeObject(list);
		
	}
}


class Student implements Serializable{
	int no;
	String name;
	int score;
	public Student(int no, String name, int score) {
		super();
		this.no = no;
		this.name = name;
		this.score = score;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}
}

댓글