728x90
버퍼링 기능으로 파일 복사하기
package test15;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyTest1 {
public static void main(String[] args) {
//FileInputStream, FileOutputStream 클래스를 사용하여 이미지 파일을 읽어와서 복사
long ms = 0;
try(FileInputStream fis = new FileInputStream("src/test15/redpanda.png");
FileOutputStream fos = new FileOutputStream("src/test15/redpanda_1.png");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos)){
ms = System.currentTimeMillis();
int i;
while((i = bis.read())!= -1) {
bos.write(i);
}
//파일을 복사하는 데 걸리는 시간
ms = System.currentTimeMillis();
ms = System.currentTimeMillis() - ms;
}catch(IOException e) {
e.printStackTrace();
}
System.out.println("파일 복사 " + ms + "소요됨");
}
}
직렬화
package test15_1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person implements Serializable{
//private static final long
String name;
String job;
//기본 생성자
public Person() {}
//생성자
public Person(String name, String job) {
this.name = name;
this.job = job;
}
//toString 재정의(오버라이딩)
public String toString() {
return name + " " + job;
}
}
public class SeriallizationTest {
public static void main(String[] args){
Person p1 = new Person("이성계","창업주");
Person p2 = new Person("이방원","명예회장");
System.out.println(p1);
System.out.println(p2);
//직렬화
try(FileOutputStream fos = new FileOutputStream("src/test15_1/serial.out");
ObjectOutputStream oos = new ObjectOutputStream(fos)){
//p1과 p2의 값을 파일에 씀(직렬화)
oos.writeObject(p1);
oos.writeObject(p2);
} catch (IOException e) {
e.printStackTrace();
}
//역직렬화
try(FileInputStream fis = new FileInputStream("src/test15_1/serial.out");
ObjectInputStream ois = new ObjectInputStream(fis)){
//p1과 p2의 값을 파일에서 읽어 들임(역직렬화)
Person pa = (Person)ois.readObject();
Person pb = (Person)ois.readObject();
System.out.println(pa);
System.out.println(pb);
} catch(IOException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
※transient 예약어
직렬화하고 싶지 않은 변수가 있을 때 사용하는 예약어
-> 멤버변수 앞에 사용
그 외 입출력 클래스
File 클래스
package test15_1;
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) throws IOException {
//file 객체 생성
File file = new File("src/test15_1/newFile.txt"); //해당 경로에 File 클래스 생성. 실제 생성된 것은 아님
file.createNewFile(); //실제 파일 생성
//파일 속성 메소드 호출하여 출력
System.out.println("파일인지 확인 file.isFile() : " + file.isFile());
System.out.println("디렉토리 확인 file.isDirectory() : " + file.isDirectory());
System.out.println("파일명 확인 file.getName() : "+file.getName());
System.out.println("파일크기 확인 file.length() : "+file.length());
System.out.println("파일 절대 경로 file.getAbsolutePath() : "+file.getAbsolutePath());
System.out.println("생성자에 넣어준 파일 경로 file.getPath() : "+file.getPath());
System.out.println("파일을 읽기 권한 확인 file.canRead() : "+file.canRead());
System.out.println("파일을 쓰기 권한 확인 file.canWrite() : "+ file.canWrite());
file.delete(); //파일 삭제
}
}
package test15_1;
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) throws IOException {
/*
//file 객체 생성
File file = new File("src/test15_1/newFile.txt"); //해당 경로에 File 클래스 생성. 실제 생성된 것은 아님
file.createNewFile(); //실제 파일 생성
//파일 속성 메소드 호출하여 출력
System.out.println("파일인지 확인 file.isFile() : " + file.isFile());
System.out.println("디렉토리 확인 file.isDirectory() : " + file.isDirectory());
System.out.println("파일명 확인 file.getName() : "+file.getName());
System.out.println("파일크기 확인 file.length() : "+file.length());
System.out.println("파일 절대 경로 file.getAbsolutePath() : "+file.getAbsolutePath());
System.out.println("생성자에 넣어준 파일 경로 file.getPath() : "+file.getPath());
System.out.println("파일을 읽기 권한 확인 file.canRead() : "+file.canRead());
System.out.println("파일을 쓰기 권한 확인 file.canWrite() : "+ file.canWrite());
file.delete(); //파일 삭제
*/
//test 15_1 패키지 내의 파일/폴더 목록을 문자열 배열로 반환
File file = new File("src/test15_1/");
String[] filelist = file.list();
for(String namelist : filelist) {
System.out.println(namelist);
}
}
}
내부클래스, 람다식
java document 에서 Nasted Class Summary를 볼 수 있는데 이는 중첩 클래스(클래스 내의 클래스)를 뜻함
package test16;
//외부 클래스
class OuterClass{
int a = 3;
static int b = 4;
//내부 클래스
class Inner{
int c = 5;
public void innerMethod() {
System.out.println("inner innerMethod()");
System.out.println("c 변수값 출력 : " + c);
}
}
}
public class InnerTest1 {
public static void main(String[] args) {
System.out.println("외부 클래스");
OuterClass oc = new OuterClass(); //외부클래스 객체 생성
System.out.println("OuterClass a = " + oc.a);
System.out.println("OuterClass b = " + OuterClass.b);
System.out.println("내부 클래스");
OuterClass.Inner i = oc.new Inner(); //내부클래스 객체 생성
System.out.println("Inner c = " + i.c);
i.innerMethod(); //내부클래스의 method 호출해서 사용
//외부클래스부터 객체를 먼저 생성한 후 내부클래스 객체를 생성해야함
//내부클래스 객체 생성 형식 : 외부클래스명.내부클래스명 객체명 = 외부클래스생성된객체명.new 내부클래스명();
}
}
728x90
'KDT > Java' 카테고리의 다른 글
240313 Java - 람다식 (0) | 2024.03.13 |
---|---|
240311 Java - 내부 클래스2 (0) | 2024.03.11 |
240306 Java 입출력과 스트림 2 (0) | 2024.03.06 |
240304 Java - 입출력과 스트림 1 (0) | 2024.03.04 |
240131 Java - 자바 입출력 (0) | 2024.01.31 |