JAVA
[JAVA] 파일 클래스(File Class)
키플레이어
2019. 11. 12. 16:18
반응형


File class 예제
import java.io.*;
public class FileEx{
public static void main(String[] args) throws IOException{
String filePath = "C:\\java_choi\\study";
File f1 = new File(filePath);
String list[] = f1.list();
for(int i=0;i<list.length;i++){
File f2 = new File(filePath,list[i]);
if(f2.isDirectory()){
System.out.printf("%s : 디렉토리 %n",list[i]);
}else{
System.out.printf("%s : 파일(%,dbyte)%n", list[i],f2.length());
}
}
File f3 = new File("D:\\test.txt"); //D드라이브에 test.txt파일 생성
System.out.println(f3.createNewFile()); //true - 파일생성
System.out.println(f3.getAbsolutePath()); //D:\\test.txt
System.out.println(f3.getCanonicalPath()); //D:\\test.txt
System.out.println(f3.getPath()); //D:\\test.txt
System.out.println(f3.getName()); //test.txt
System.out.println(f3.getParent()); //D:\\
File f4 = new File("D:\\test.txt");
File f5 = new File("D:\\test1.txt");
System.out.println(f4.renameTo(f5)); //test.txt를 test1.txt로 변경
}
}
반응형