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();
}
}
'자바 풀스택 공부' 카테고리의 다른 글
Day 59. [Java] StringReader/Writer (0) | 2022.03.30 |
---|---|
Day 58. [PL/SQL] 익명 블록, 제어문 (0) | 2022.03.29 |
Day 56. [JSP/Servlet/JavaScript] Cors, 자바로 웹 크롤링, 도로명주소API (0) | 2022.03.25 |
Day 55. [Java/JavaScript/jQuery] Ajax, jQuery, 메일 보내기 (0) | 2022.03.24 |
Day 54. [Html/JavaScript] Modal (0) | 2022.03.24 |
댓글