[JAVA] TCP, UDP 통신 (1)

·2023년 10월 10일
0

JAVA

목록 보기
10/15

1. 파일 전송 클래스 : File

(1) javax.swing.패키지

  • awt는 운영체제가 제공하는 컴포넌트를 이용한다.
  • 운영체제에 종속되지 않는 GUI를 만들기 위해 나온 패키지이다.
  • awt의 모든 컴포넌트에 j 버튼을 붙이면 swing 컴포넌트가

(2) File 클래스를 이용하여 파일 객체 만들기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) {
		File f1 = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src\\aaa.txt");
		File f2 = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src", "aaa.txt");
		File dir =  new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File f3 = new File(dir, "aaa.txt");
		// 추상 경로명 dir : 변수로 쉽게 접근 가능
		// f1, f2, f3 은 모두 같은 위치의 파일을 가르친다. 파일은 존재하지 않아도 된다.
		
		// Java Swing 을 이용하여 파일 선택 dialog를 표시하고, 사용자가 파일을 선택하면 선택한 파일을 가져온다
		JFileChooser jfc = new JFileChooser(".");		// 파일을 선택하는 클래스
		
		int res = jfc.showOpenDialog(null);
		File f4 = null;
		if (res == 0) {
			f4 = jfc.getSelectedFile();
		}
	}
}

파일 셀렉 dialog 생성 코드

(3) 폴더 구별자 정리

폴더 구별자란?

각 운영체제마다 다른 상위, 하위 폴더 표시자인 \, / 등을 java에게 알아서 구분하라고 해 주는 폴더 구별자
: 플랫폼 간의 이식성을 높여 줄 수 있다.

폴더 구별자 종류 4가지

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) {
		System.out.println("File.separator = " + File.separator);
		// \ : 폴더 구별자
		System.out.println("File.separatorChar = " + File.separatorChar);
		// 플랫폼에 따라 다르며, Windows에서는 \ (역슬래시), Unix/Linux/macOS에서는 / (슬래시)를 나타낸다.
		System.out.println("File.pathSeparator = " + File.pathSeparator);
		// 플랫폼에 따라 다르며, Windows에서는 ;, Unix/Linux/macOS에서는 :를 나타낸다.
		System.out.println("File.pathSeparatorChar = " + File.pathSeparatorChar);
		// 플랫폼에 따라 다르며, Windows에서는 ; (세미콜론), Unix/Linux/macOS에서는 : (콜론)를 나타낸다.
		
		File dir = new File("D:" + File.separator + "nam" + File.separator + 
				"2_JAVA" + File.separator + "JAVA_Study" + File.separator + "Day15"
				+ File.separator + "src"); 
	}
}

(4) 파일 관련 메서드 정리

1 파일 생성하기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 1 파일 생성하기
		if (file.createNewFile()) {
			System.out.println("aaa.txt 파일을 만들었습니다");
		}else {
			System.out.println("aaa.txt 파일은 존재하는 파일입니다.");
		}
	}
}

2 같은 종류의 파일 생성하기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 2 같은 종류의 파일 생성하기

		File imsi = File.createTempFile("temp", ".dat", dir);
		// "파일명 앞에 붙을 것" / "파일명 끝값" / 경로
		// 중간에 로또 세 번 맞을 정도의 큰값이 들어감~~
		
	}
}

결과 :

3 파일 지우기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 2 파일 지우기

		File imsi = File.createTempFile("temp", ".dat", dir);
		
		imsi.deleteOnExit(); 		// 1. 프로그램 종료 시 파일을 삭제
		try {
			Thread.sleep(5000);
		}catch(Exception e) {}
		
		
		imsi.delete(); 				// 2. 즉시 삭제해라
		try {
			// +) 파일이 존재하면 삭제 후 true, 없으면 false 반환 : if 가능
			Thread.sleep(5000);
		}catch(Exception e) {}
		
	}
}

4 파일 존재 유무 묻기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 4 파일의 존재 유무 묻기

		if (file.exists() ) {
			System.out.println("aaa.txt 는 있는 파일입니다");
		}else {
			file.createNewFile();
			System.out.println("aaa.txt 파일을 만들었습니다.");
		}
		
	}
}

5 파일에 아무거나 적고 저장하기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 5 파일에 아무거나 적고 저장하기

		System.out.println("파일 크기 : " + file.length()); // 바이트로 파일 크기 알려 줌
		// int len = file.length();			// 파일의 자료형은 long으로 반환됨. 
		System.out.println();
		
	}
}

6 파일의 이름과 경로 추적하기

import java.io.*;
import javax.swing.JFileChooser;


public class Exam_01 {
	public static void main(String[] args) throws IOException {
		File dir = new File("D:\\nam\\2_JAVA\\JAVA_Study\\Day15\\src");
		File file = new File(dir, "aaa.txt");
		
		// 파일 관련 메서드 정리
		// 6 파일의 이름과 경로 추적하기

		System.out.println("file.name = " + file.getName());				// 파일 이름
		System.out.println("file.parentName = " + file.getParentFile());	// 파일의 경로만 출력
		System.out.println("file.toString = " + file.toString());		 	// 파일의 경로와 이름이 출력
		
	}
}
profile
자바 백엔드 개발자 개인 위키

0개의 댓글

관련 채용 정보