1. 파일 및 디렉터리 생성 시 주요 기술 사항은 다음과 같습니다.
1. File 클래스의 CreateNewFile은 추상 경로를 기반으로 새로운 빈 파일을 생성합니다. 추상 경로에 지정된 파일이 존재하면 생성이 실패합니다.
2. File 클래스의 mkdir 메소드는 추상 경로를 기반으로 디렉토리를 생성합니다.
3. File 클래스의 mkdirs 메소드는 존재하지 않는 상위 디렉토리 생성을 포함하여 추상 경로를 기반으로 디렉토리를 생성합니다.
4. File 클래스의 createTempFile 메소드는 파일 이름 접두사, 접미사 및 파일이 있는 디렉터리를 지정할 수 있습니다. 디렉터리를 지정하지 않으면 시스템의 임시 폴더에 저장됩니다.
5. mkdirs 방법을 제외하고 위의 방법으로 파일과 디렉터리를 생성할 때 대상 파일이 존재하지 않고 상위 디렉터리가 존재하는지 확인해야 합니다. 그렇지 않으면 생성이 실패합니다.
둘째, 예제 데모는 다음과 같습니다.
import java.io.IOException; public class CreateFileUtil { public static boolean createFile(String destFileName) { 파일 파일 = new File(destFileName); if(file.exists()) { System.out.println ("단일 파일 만들기" + destFileName + "실패했습니다. 대상 파일이 이미 존재합니다!") return false; (destFileName.endsWith(File.separator)) { System.out.println("단일 파일 생성" + destFileName + "실패했습니다. 대상 파일은 디렉터리일 수 없습니다!") return false; 대상 파일이 있는 위치existsif(!file.getParentFile().exists()) { //대상 파일이 있는 디렉터리가 없으면 상위 디렉터리를 생성합니다. System.out.println("대상 파일이 있는 디렉터리가 존재하지 않습니다. 생성 준비를 하세요!"); .getParentFile().mkdirs()) { System.out.println("대상 파일이 있는 디렉터리를 생성하지 못했습니다!") return false } } //대상 파일 생성 try { if(file.createNewFile) ()) { System.out.println("단일 파일 만들기" + destFileName + "성공!"); return true; } else { System.out.println("단일 파일 만들기" + destFileName + "실패!") } catch(IOException e) { e.printStackTrace( ) ; System.out.println("단일 파일 만들기" + destFileName + "실패했습니다!" + e.getMessage()) return false } } createDir(String destDirName) { File dir = new File(destDirName); if (dir.exists()) { System.out.println("디렉토리 생성" + destDirName + "실패했습니다. 대상 디렉터리가 이미 존재합니다."); ; } if (!destDirName.endsWith(File.separator)) { destDirName = destDirName + File.separator } //디렉토리 생성 if (dir.mkdirs()) { System.out.println("디렉토리 생성" + destDirName + "성공! "); true 반환; } else { System.out.println("디렉토리 생성" + destDirName + "실패!"); false 반환; } } public static String createTempFile(String prefix, String suffix, String dirName) { 파일 tempFile = null; if (dirName == null) { try{ //기본 폴더에 임시 파일을 생성합니다. tempFile = File.createTempFile(prefix, suffix); //임시 파일의 경로를 반환 return tempFile.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); System.out.println("임시 파일 생성에 실패했습니다!" + e.getMessage ()) ; return null; } } else { File dir = new File(dirName); //임시 파일이 있는 디렉터리가 없으면 먼저 만듭니다. if (!dir.exists()) { if (!CreateFileUtil.createDir(dirName)) { System.out.println("임시 파일을 생성하지 못했습니다. 임시 파일이 있는 디렉터리를 생성할 수 없습니다!") return null; 지정된 디렉터리의 임시 파일 tempFile = File.createTempFile(prefix, suffix, dir) return tempFile.getCanonicalPath() } catch(IOException e); e.printStackTrace(); System.out.println("임시 파일 생성에 실패했습니다!" + e.getMessage()); return null } } } public static void main(String[] args) { //디렉터리 생성 String; dirName = "D:/work/temp/temp0/temp1"; CreateFileUtil.createDir(dirName) //파일 생성 String fileName = dirName + "/temp2/tempFile.txt"; CreateFileUtil.createFile(fileName); //임시 파일 생성 String prefix = "temp"; String suffix = ".txt" for (int i = 0; i < 10; i++) { System.out.println("임시 파일을 만들었습니다: " + CreateFileUtil.createTempFile(prefix, suffix, dirName)); //기본 디렉터리에 임시 파일 생성 for (int i = 0; i < 10; i++) { System.out.println("임시 파일이 기본 디렉터리에 생성되었습니다:" + CreateFileUtil.createTempFile(prefix, suffix , null )) } } }출력 결과:
D:/work/temp/temp0/temp1 디렉토리가 성공적으로 생성되었습니다! 대상 파일이 있는 디렉터리가 존재하지 않습니다. 생성할 준비를 하세요! 단일 파일 D:/work/temp/temp0/temp1/temp2/tempFile.txt를 성공적으로 생성했습니다! 임시 파일이 생성되었습니다: D:work emp emp0 emp1 emp5171.txt 임시 파일이 생성되었습니다: D:work emp emp0 emp1 emp5172.txt 임시 파일이 생성되었습니다: D:work emp emp0 emp1 emp5173.txt 임시 파일이 생성되었습니다 : D:work emp emp0 emp1 emp5174.txt 임시 파일 생성: D:work emp emp0 emp1 emp5175.txt 생성된 임시 파일: D:work emp emp0 emp1 emp5176.txt 생성된 임시 파일: D:work emp emp0 emp1 emp5177.txt 생성된 임시 파일: D:work emp emp0 emp1 emp5178.txt 생성된 임시 파일: D :일 emp emp0 emp1 emp5179.txt 임시 파일이 생성되었습니다: D:work emp emp0 emp1 emp5180.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt 기본 디렉터리에 임시 파일이 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt 기본 디렉터리에 임시 파일이 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C: 문서 및 설정관리자로컬 설정Temp emp5185.txt 기본 디렉터리에 임시 파일이 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C: 문서 및 설정관리자로컬 설정Temp emp5188.txt 기본 디렉터리에 임시 파일이 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt 임시 파일이 기본 디렉터리에 생성되었습니다: C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt