예제1
package 입출력;
import java.io.File;
import java.io.IOException;
public class file01 {
public static void main(String[] args) throws IOException {
File f = new File("C:\\Users\\index\\Desktop\\springBoot\\FileEx1.java");
String fileName = f.getName();
int pos = fileName.lastIndexOf(".");
System.out.println("경로를 제외한 파일이름 - " + f.getName());
System.out.println("확장자를 제외한 파일이름 - " + fileName.substring(0,pos)); // 0부터 pos까지?
System.out.println("확장자 - " + fileName.substring(pos+1)); // pos 이후?
System.out.println("경로를 포함한 파일이름 - " + f.getPath());
System.out.println("파일의 절대경로 - " + f.getAbsolutePath());
System.out.println("파일의 정규경로 - " + f.getCanonicalPath()); // IOException 없으면 예외발생
System.out.println("파일이 속해있는 디렉토리 - " + f.getParent());
System.out.println();
System.out.println("File.pathSeparator - " + File.pathSeparator);
System.out.println("File.pathSeparatorChar - " + File.pathSeparatorChar);
System.out.println("File.separator - " + File.separator);
System.out.println("File.separatoChar - " +File.separatorChar);
System.out.println();
System.out.println("user.dir = " + System.getProperty("user.dir")); // 디렉토리 보여주는것같음
System.out.println("sun.boot.class.path = " + System.getProperty("sun.boot.class.path"));
}
}
File 인스턴스"만" 생성하고 메서드를 이용해 파일의 경로와 구분자 등의 정보를 출력하는 예제.
- 절대경로(absolute path)에 대해
파일시스템 루트(root)로부터 시작하는 파일의 전체 경로
OS에 따라 하나의 파일에 대해 둘 이상의 절대경로가 존재할 수 있다. 현재 디렉토리를 의미하는 '.' 기호나 링크를 포함하는 경우 등
정규경로(canonical path)의 경우 기호나 링크 등을 포함하지 않는 유일한 경로이다.
- 시스템속성 중 user.dir의 값을 확인하면 현재 프로그램이 실행중인 디렉토리를 알 수 있음
- 우리가 OS 시스템변수로 설정하는 classpath 외에 sun.boot.class.path라는 시스템속성에 기본적 classpath가 있어서 기본적 경로들은 이미 설정되어 있다는데, 오라클이 아니라 julu라서 그런지 경로가 null로 나온다!!
예제 대신 다른 생성자를 사용해 File 인스턴스를 생성할 수 있다.
File f = new FIle("C:\\Users\\index\\Desktop\\springBoot", "FileEx1.java");
//또는
File dir = new File("C:\\Users\\index\\Desktop\\springBoot");
File f = new File(dir, "FileEx1.java");
* File 인스턴스"만" 생성했기 때문에 파일이나 디렉토리는 생성되지 않는다.
파일명이나 디렉토리명으로 지정된 문자열이 유효하지 않더라도 컴파일 에러나 예외를 발생시키지 않는다.
새로운 파일을 생성하기 위해서는 File 인스턴스를 생성한 다음, 출력스트림을 생성하거나 createNewFile()을 호출해야함.
1. 이미 존재하는 파일을 참조할 때
File f = new File("C:\\Users\\index\\Desktop\\springBoot\\FileEx1.java");
2. 기존에 없는 파일을 새로 생성할 때
File f = new File("C:\\Users\\index\\Desktop\\springBoot\\FileEx1.java");
f.createFile(); // 새로운 파일이 입력한 경로에 지정한 이름으로 생성됨
예제2
package 입출력;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class file04 {
public static void main(String[] args) {
String currDir = System.getProperty("user.dir");
File dir = new File(currDir);
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++) {
File f = files[i];
String name = f.getName();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mma");
String attribute = "";
String size = "";
if(files[i].isDirectory()) { //file[i]가 디렉토리라면 DIR을 반환함
attribute = "DIR";
} else { // 디렉토리가 아니라면!!
size = f.length() + ""; // File배열의 길이
attribute = f.canRead() ? "R" : " "; // 읽을 수 있는 파일인가?
attribute += f.canWrite() ? "W" : " "; // 쓸 수 있는 파일인가?
attribute += f.isHidden() ? "H" : " "; // 파일의 속성이 숨김인가?
}
// long lastModified() : 파일이 마지막으로 수정된 시간으로 반환
System.out.printf(
"%s %3s %6s %s\n", df.format(new Date(f.lastModified())), attribute, size, name);
}
}
}
https://b1uffer.tistory.com/46
현재 디렉토리에 속한 파일과 디렉토리의 이름, 크기 등 상세정보를 보여주는 예제
* String currDir = System.getProperty("user.dir"); 실행위치에 있는 파일을 읽어들일 수 있음
주로 자바 실행 시 실행되는 곳의 정보를 얻어오거나 운영체제의 정보가 필요할 때 사용한다
System.getProperty()의 인자에 값을 적어넣으면 String으로 출력된다
'JAVA' 카테고리의 다른 글
| 날짜와 시간 & 형식화 - Instant (0) | 2025.06.25 |
|---|---|
| Path.resolve() (0) | 2025.06.21 |
| 네트워킹 - URL, URI (0) | 2025.06.20 |
| I/O - File(Path 공부) (0) | 2025.06.19 |
| 바이트기반 보조스트림 - BufferedInputStream, BufferedOutputStream (0) | 2025.06.17 |