public class PracticeFile {
public static void main(String[] args) {
PracticeFile file = new PracticeFile();
String path = "/Users/hanjiyeon/Desktop/practice";
file.isPath(path);
}
public void isPath(String pathName){
File file = new File(pathName);
System.out.println(pathName + " is exist = " + file.exists());
}
}
public class PracticeFile {
public static void main(String[] args) {
String path2 = "/Users/hanjiyeon/Desktop/practice2";
file.createDir(path2);
file.isDir(path2);
}
public void createDir(String pathName){
File file = new File(pathName);
System.out.println("Create " + pathName + " result = " + file.mkdir());
}
public void isDir(String pathName){
File file = new File(pathName);
System.out.println(pathName + " is Directory? = " + file.isDirectory());
System.out.println(pathName + " is File? = " + file.isFile());
}
}
mkdir()
: 디렉토리 하나 만듦mkdirs()
: 여러 개의 하위 디렉토리 만듦isDirectory()
: File 객체가 경로인지 확인isFile()
: File 객체가 하나의 file인지 확인public class ManageFile {
public static void main(String[] args) {
ManageFile file = new ManageFile();
String pathName = "/Users/hanjiyeon/Desktop/practice";
String fileName = "practice.txt";
file.createFile(pathName, fileName);
}
public void createFile(String pathName, String fileName){
File file = new File(pathName, fileName);
try {
System.out.println("is Created ? = " + file.createNewFile()); // true
} catch (Exception e){
e.printStackTrace();
}
}
public void getFileInfo(File file) throws IOException {
System.out.println("Absolute Path:" + file.getAbsolutePath());
System.out.println("Absolute Path:" + file.getAbsoluteFile());
System.out.println("Absolute Path:" + file.getCanonicalPath());
System.out.println("Absolute Path:" + file.getCanonicalFile());
}
}
createNewFile()
: IOException을 던지기 때문에 try-catch 안에 사용 해야함
...Path()
: 경로 String 리턴
...File()
: File 객체 리턴
public static void main(String[] args) {
File file2 = new File(pathName);
/* list method */
File[] files = File.listRoots();
String[] list = file2.list();
String[] list2 = file2.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
File[] fileList = file2.listFiles();
File[] fileList2 = file2.listFiles(new FileFilter() {
@Override
public boolean accept(File path) {
return path.isFile();
}
});
File[] fileList3 = file2.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
}
listRoots()
: 루트 디렉토리 목록 File[] 리턴
list()
: 현재 디렉토리에 있는 목록 String[] 리턴
list(FileNameFilter filter)
: 조건에 맞는 목록만 String[] 리턴
listFiles()
: 현재 디렉토리 하위에 있는 목록 File[] 리턴
listFiles(FileNameFilter filter)
: 조건에 맞는 목록만 File[] 리턴
listFiles(FileFilter Filter)
: 조건에 맞는 목록 File[] 리턴