티스토리 뷰

자바를 이용하여 파일을 생성하는 방법을 알아 보도록 하자.

Example :: Checking for a File's Existence

메소드 exists()를 이용하여 찾고자 하는 파일이 실제로 존재하는지 확인 할 수 있다.

package com.umejintan.file;

import java.io.File;

public class CheckFile {

	public static void main(String[] args) {
		
		// Create a file object
		File file = new File("c://Example//File//existFile.txt");
		
		// 1. check if the file exists or not
		boolean isExists = file.exists();
		
		if(isExists) {
			System.out.println("I find the existFile.txt");
		} else {
			System.out.println("No, there is not a no file.");
		}
	}
}

실제로 파일이 존재하지 않는 관계로 없다는 결과가 나왔다. 그럼 어떻게 할까? 실제 Directory와 파일을 만들어 놓으면 실제로 원하는 결과값을 얻을 수 있을 것이다. 자 그럼 다른 방식의 파일 Ojbect를 생성하는 방법에 대해서 알아 보자.


Example :: Creating a file object in multipul ways

package com.umejintan.file;

import java.io.File;

public class CreateFileObject {
	
	public static void main(String[] args) {
		
		// 1. Create a file object with whole path.
		File absolutePath = new File("c://Example//File//existFile.txt");
		System.out.println(absolutePath.exists());
		
		// 2.1 Create a directory object
		// 2.2 Create another file object with 2.1 object included directory's information 
		File dir = new File("c://Example//File//");
		File pathWithDir = new File(dir, "existFile.txt");
		System.out.println(pathWithDir.exists());
		
		// 3. This is the same concept with 2, but directory's information with a string value.
		File pathWithStrDir = new File("c://Example//File//", "existFile.txt");
		System.out.println(pathWithStrDir.exists());
	}
}

그럼 실제 파일을 생성하고 삭제를 해보도록 하자

Example :: Creating and Deleting a file

메소드 :: createNewFile() / deleteOnExit()

package com.umejintan.file;

import java.io.File;
import java.io.IOException;

public class CreateFlie {
	
	public static void main(String[] args) {
		
		// Create a file ojbect.
		File file = new File("c://Example//File//umejintan.txt");
		
		// 1. check if the file exists or not
		boolean isExists = file.exists();
		
		try {
			// 2. Create a file if the file is not existed.
			if(!isExists) {
				file.createNewFile();
				System.out.println("The file is created.");
			}
			
			// 3. Delete the file to clean it. Check the file status again.
			isExists = file.exists();
			if(isExists) {
				file.deleteOnExit();
				System.out.println("The file is deleted.");
			}
			
		} catch(IOException ex) {
			ex.getMessage();			
		}
	}
}
Info!
delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.
deleteOnExit() Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Files (or directories) are deleted in the reverse order that they are registered. Invoking this method to delete a file or directory that is already registered for deletion has no effect. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification. Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care. Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

Reference

  1. Oracle Java



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함