Manipulasi File pada Java

Untuk melakukan manipulasi File pada Java menggunakan class java.io.File mulai dari membuat file, menghapus file, cek permision sebuah file, cek meta data file dan lain sebagainya.

1. Manipulasi File pada Java: Cek File apakah ada atau tidak

import java.io.*;
 
public class FileChecker {
 
  public static void main(String args[]) {
 
	  File f = new File("c:\\jagungodak.txt");
 
	  if(f.exists()){
		  System.out.println("File existed");
	  }else{
		  System.out.println("File not found!");
	  }
 
  }
 
}

2. Manipulasi file pada java: Membuat File Baru

import java.io.File;
import java.io.IOException;
 
public class CreateFileExample
{
    public static void main( String[] args )
    {
    	try {
 
	      File file = new File("c:\\newfile.txt");
 
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
 
    	} catch (IOException e) {
	      e.printStackTrace();
	}
    }
}

3. Menghapus File berdasarkan Extensi tertentu

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.io.*;
 
public class FileChecker {
 
   private static final String FILE_DIR = "c:\\folder";
   private static final String FILE_TEXT_EXT = ".txt";
 
   public static void main(String args[]) {
	new FileChecker().deleteFile(FILE_DIR,FILE_TEXT_EXT);
   }
 
   public void deleteFile(String folder, String ext){
 
     GenericExtFilter filter = new GenericExtFilter(ext);
     File dir = new File(folder);
 
     //list out all the file name with .txt extension
     String[] list = dir.list(filter);
 
     if (list.length == 0) return;
 
     File fileDelete;
 
     for (String file : list){
   	String temp = new StringBuffer(FILE_DIR)
                      .append(File.separator)
                      .append(file).toString();
    	fileDelete = new File(temp);
    	boolean isdeleted = fileDelete.delete();
    	System.out.println("file : " + temp + " is deleted : " + isdeleted);
     }
   }
 
   //inner class, generic extension filter
   public class GenericExtFilter implements FilenameFilter {
 
       private String ext;
 
       public GenericExtFilter(String ext) {
         this.ext = ext;
       }
 
       public boolean accept(File dir, String name) {
         return (name.endsWith(ext));
       }
    }
}

4. Menampilkan Tanggal Dibuat sebuah File

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class GetFileCreationDateExample
{
    public static void main(String[] args)
    {
 
    	try{
 
    		Process proc =
    		   Runtime.getRuntime().exec("cmd /c dir c:\\logfile.log /tc");
 
    		BufferedReader br =
    		   new BufferedReader(
    		      new InputStreamReader(proc.getInputStream()));
 
    		String data ="";
 
    		//it's quite stupid but work
    		for(int i=0; i<6; i++){
    			data = br.readLine();
    		}
 
    		System.out.println("Extracted value : " + data);
 
    		//split by space
    		StringTokenizer st = new StringTokenizer(data);
    		String date = st.nextToken();//Get date
    		String time = st.nextToken();//Get time
 
    		System.out.println("Creation Date  : " + date);
    		System.out.println("Creation Time  : " + time);
 
    	}catch(IOException e){
 
    		e.printStackTrace();
 
    	}
 
    }
}

Refrence

http://java.sun.com/javase/6/docs/api/java/io/File.html

Share This

 

Satu tanggapan untuk “Manipulasi File pada Java

  1. Ping-balik: Manipulasi File pada Java – Jagungodak | Free ALL Script PHP, javascript, C++

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

four × 4 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.