5월 16일 내용정리-1
1.파일과 문자셋(Charset)
.File 객체를 생성
File 객체는 실제 존재파일 유/무는 상관없다.
(파일이 없는 데 사용할려고 하는 경우 사용할려는 시점에서 FileNotFoundException 발생시킴)
2.File 생성자
File(String pathname):경로 위치를 가르키는 파일 객체 생성
예) File nFile = new File("C:\test\abc.txt");
File(File parent, String child):parent 폴더에 child파일을 가르키는 파일 객체생성
File(String parent, String child):parent 폴더에 child파일을 가르키는 파일 객체생성
File(URI uri):uri위치를 가르키는 파일 객체 생성
boolean exists(): 파일 또는 폴더의 있는지 여부를 확인
boolean createNewFile(): 경로위치에 파일 생성
boolean mkdir():경로 위치에 폴더 생성
3.윈도우의 NTFS 파일시스템은 c드라이브 바로밑에 파일 생성을 못하게함
4.구분자를 일일히 쓰기 싫거나 운영체제가 다를경우
"C:"File.separator+"test"+File.separator+"abc.txt"
5.윈도우 파일 구분자를 역슬래시() 을 쓰는데, "이안에 쓸경우" 제어문자로 인식한다.
따라서 "C:\test\abc.txt" 이렇게 \두번써준다. 왜?? 제어문자인 \t 와 \n 과 헷갈릴수있음
6.윈도우와 맥 모두 공통사용 가능한 파일 구분자: File.separator
예)String path="C:" +File.separator+ "test"+ File.separator+"abc.txt"
7.File 절대 경로: 드라이브명(C,D)부터 특정위치까지 경로를 모두 표기
예)File newFile = new File("C:\test\abc.txt")
8.File 상대 경로: 현재 작업폴더 위치를 기준으로 상대적인 경로를 표시
예)File newFile2 = new File("test\abc.txt")
예)File newFile2 = new File("abc.txt")
현재 작업하고 있는 파일을 경로기준으로 표기한것
9.프로그램 입장에서 키보드나 마우스나 파일, 네트워크로 들어오는모든것은 '입력'으로 본다. 프로그램 입장에서 뭔가를 처리하고 내보내면 '출력'으로 본다.
10.InputStream -> 프로그램 -> OutputStream (byte단위, 자바에서 처리하는걸 주로씀)
11.Reader -> 프로그램 -> writer (char단위, 채팅창같이 단어길이가 긴 특수한경우)
package study_0516_01;
import java.io.*;
public class InputOutput {
public static void main(String[] args) throws IOException{
File testDirect= new File("C:\\test");
if(! testDirect.exists()) { //만약에 "test"폴더가 없으면 새롭게 생성해 달라
testDirect.mkdir();
//파일객체 생성
File newFile1 = new File("C:\\test\\abc.txt");
if(!newFile1.exists()) { //만약에 newFile1 파일이 없으면 새롭게 생성해 달라
newFile1.createNewFile();} //예외가 발생 하므로 throws로 던져버림
File newFile2= new File("C:/test/abc.txt");
System.out.println(newFile1.exists());
System.out.println(newFile2.exists());
}
}
}