Get File Extension in Java


Ini sebenernya fungsi yang sangat sederhana, hanya bermain-main dengan String. Tapi fungsi ini cukup membantu jika kita ingin bermain-main dengan file. Daripada buat algoritme yang susah2, mending intip sj sourcecode di bawah:

import java.io.*;
import java.lang.String;

public class FileExtension{

  public void show(String extension){
    System.out.println("Extension is" + " " + extension);
    }

    public static void main(String args[])throws IOException{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter the file name:");
      String filenameExtension = bf.readLine();
      File filename = new File(filenameExtension);
        if(!filename.exists())
        {
      System.out.println("File does not exist.");
      System.exit(0);
        }
      String extension;
      int dotPos = filenameExtension.lastIndexOf(".");
      extension = filenameExtension.substring(dotPos);
      FileExtension abc = new FileExtension();
      abc.show(extension);
      }
}

diunduh dari roseindia…

About windupurnomo

I'm interested in programming. I am active with several programming such as Java, C #, C, JavaScript, HTML. I'm also develop desktop application (Java Swing), Mobile Application (Android), and Web programming (ASP MVC).
This entry was posted in Java and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s