January 3, 2017

File

Java IO API ဟာ JDK version 1.0 ကထဲကပါဝင်လာခဲ့တဲ့ Input Output ကို လုပ်ဆောင်ဖို့အတွက် API တစ်ခုဖြစ်ပါတယ်။ File System ထဲမှာ ရှိတဲ့ File တွေ Directory တွေရဲ့ Path ကို ကိုယ်စားပြုဖို့အတွက် java.io.File Class ကို ပြင်ဆင်ထားပါတယ်။ File Class ဟာ Serializable နဲ့ Comparable<File> ကို Implement လုပ်ထားတဲ့ Class တစ်ခုဖြစ်ပါတယ်။


How To Create File Object


အရင်ဦးဆုံး File Object တစ်ခုကို ဘယ်လို တည်ဆောက်မလဲ ဆိုတာကနေစပြီး လေ့လာကြည့်ကြရအောင်။ File Object ကို တည်ဆောက်ဖို့ အတွက် Constructors တွေကို ပြင်ဆင်ထားပါတယ်။ File Object ကို တည်ဆောက်တဲ့ နေရာမှာ လက်ရှိရှိပြီးသား File Directory တွေကိုကိုယ်စားပြုတဲ့ Object တွေကိုလဲ တည်ဆောက်နိုင်သလို၊ လက်တွေ့မှာ မရှိသော်လည်း Virtually တည်ဆောက်ထားလို့ရပါတယ်။ File Object ကို တည်ဆောက်ပြီး လိုအပ်လာတဲ့အခါကျမှ Create လုပ်လို့လဲ ရနိုင်ပါတယ်။ ဒါဆို File Class ထဲမှာပါတဲ့ Constructors တွေကို လေ့လာကြည့်ကြရအောင်။

Constructor Description
File(File parent, String child) Parent ဖြစ်တဲ့ File Object တစ်ခုနဲ့ Child Path Name တို့ဖြင့် File Object ကို တည်ဆောက်နိုင်တဲ့ Constructor ဖြစ်ပါတယ်
File(String pathname) File Directory ရဲ့ Path String ကနေ File Object ကို တည်ဆောက်နိုင်တဲ့ Constructor ဖြစ်ပါတယ်။ Path အနေနဲ့ Absolute Path ကော Relative Path ကို ပါ အသုံးပြုနိုင်မှာ ဖြစ်ပါတယ်
File(String parent, String child) File Directory တို့ရှိနေတဲ့ Directory Path နဲ့ File Name တို့ကနေ File Object ကို တည်ဆောက်နိုင်တဲ့ Constructor ဖြစ်ပါတယ်။
File(URI uri) File Directory တို့ကိုဖေါ်ပြနိုင်တဲ့ URI Object ကနေတဆင့် File Object ကိုတည်ဆောက်နိုင်တဲ့ Costructor ဖြစ်ပါတယ်။

File Class ရဲ့ Constructor တွေကို လေ့လာကြည့်ရင် File တစ်ခုကို တည်ဆောက်ဖို့အတွက်က အဲ့ဒီ File ရဲ့ Path ကို ဖေါ်ပြပေးနိုင်ဖို့လိုတာကို တွေ့ရပါမယ်။ အဲ့ဒီ Path ကို ဖေါ်ပြတဲ့နေရာမှာ Absoulte Path နဲ့လည်း ဖေါ်ပြနိုင်သလို Relative Path နဲ့ လည်းဖေါ်ပြနိုင်မှာ ဖြစ်ပါတယ်။
import java.io.*;

public class FileCreation {
    
    public static void main(String [] args) {

        // File Path
        File file1 = new File("file1");

        // Parent File and File Path
        File file2 = new File(file1, "file2.txt");

        // Parent File Path and File Path
        File file3 = new File("file1", "file3.txt");

        // URI
        URI uri = new URI("c:\\file1\\file4.txt");
        File file4 = new File(uri);
    }
}


Create & Delete File

File Object ကနေ File Directory တွေကို တည်ဆောက်နိုင်ဖို့၊ ပြီးတော့ ရှိပြီးသား File တွေကို ဖျက်နိုင်ဖို့အတွက် အောက်ပါ Methods တွေကို အသုံးပြုနိုင်ပါတယ်။

Method Return Type & Description
createNewFile() boolean

File Object မှာ သတ်မှတ်ထားခဲ့တဲ့ Abstract Path Name ဖြင့် Empty New File တစ်ခုကို တည်ဆောက်ပေးပါလိမ့်မယ်။ တကယ်လို့ Path အတိုင်း File ဟာရှိနေမယ်ဆိုရင် ဆောက်ပေးနိုင်မှာ မဟုတ်ပါဘူး။ File ကို တည်ဆောက်နိုင်ခဲ့ရင် true ကို Return လုပ်မှာ ဖြစ်ပြီး မဆောက်နိုင်ခဲ့ပါက false ကို Return ပြန်မှာ ဖြစ်ပါတယ်။
IOException ကို ဖြစ်ပေါ်စေနိုင်တဲ့အတွက် Handle လုပ်ပေးဖို့လိုအပ်ပါတယ်။
mkdir() boolean

File Object မှာ သတ်မှတ်ထားတဲ့ Abstract Path Name ဖြင့် Directory ကို တည်ဆောက်ပေးမှာ ဖြစ်ပါတယ်။ တည်ဆောက်နိုင်ခဲ့ရင် true ကို Return ပြန်မှာ ဖြစ်ပြီး မတည်ဆောက်နိုင်ခဲ့ပါက false ကို Return ပြန်မှာ ဖြစ်ပါတယ်
mkdirs() boolean

File Object မှာ သတ်မှတ်ထားတဲ့ Abstract Path Name ဖြင့် Directory ကို တည်ဆောက်ပေးမှာ ဖြစ်ပါတယ်။ Child Directories တွေပါလာရင်တော့ ဒီ Method ကို အသုံးပြုရမှာ ဖြစ်ပါတယ်။ တည်ဆောက်နိုင်ခဲ့ရင် true ကို Return ပြန်မှာ ဖြစ်ပြီး မတည်ဆောက်နိုင်ခဲ့ပါက false ကို Return ပြန်မှာ ဖြစ်ပါတယ်
delete() boolean

File Object ဖြင့် ကိုယ်စားပြုသော File Directory ကို delete လုပ်လိုတဲ့အခါမှာ အသုံးပြုနိုင်ပါတယ်။ အောင်မြင်စွာ ပြီးဆုံးပါက true ကို Return ပြန်မှာ ဖြစ်ပြီး မဖျက်နိုင်ခဲ့ပါက false ကို Return ပြန်မှာ ဖြစ်ပါတယ်။
deleteOnExit() void

File Object ကို JVM ကနေ Application ကို Run တာပြီးတဲ့ အချိန်မှာ Delete လုပ်ပေးမှာ ဖြစ်ပါတယ်။ Return Type တော့မရှိပါဘူး။

import java.io.*;

public class FileTest {

    public static void main(String[] args) throws IOException{
        
        File file = new File("Test.txt");
        boolean result1 = file.createNewFile();

        System.out.println(result1 ? "Success" : "Fails");

        File dir1 = new File("outer");
        File dir2 = new File(dir1, "inner");

        dir1.mkdirs();

        boolean result2 = dir1.delete();
        System.out.println(result2 ? "Success" : "Fails");
    }
}



About Path Information

Path လမ်းကြောင်းနဲ့ပတ်သက်ပြီး အောက်ပါ Method များကို အသုံးပြုနိုင်ပါတယ်။

Method Return Type Description
getAbsoluteFile() File

Absolute Path ပုံစံနဲ့ တည်ဆောက်ထားတဲ့ File Object ကို ရရှိမှာ ဖြစ်ပါတယ်။
getAbsolutePath() String

File Object ရဲ့ Absolute Path ကို ရရှိမှာ ဖြစ်ပါတယ်
getCanonicalFile() File

Canonical ပုံစံဖြင့်တည်ဆောက်ထားတဲ့ File Object ကို ရရှိမှာ ဖြစ်ပါတယ်
getCanonicalPath() String

Absolute and Unique ဖြစ်တဲ့ Canonical File Path ကို ရရှိမှာ ဖြစ်ပါတယ်
getName() String

File Name ကို ရရှိမှာ ဖြစ်ပါတယ်။
getParent() String

Parent Directory ရဲ့ Name ကို ရရှိမှာ ဖြစ်ပါတယ်။ တကယ်လို့ သတ်မှတ်ထားခြင်း မရှိပါက null ကို ပြန်ရမှာ ဖြစ်ပါတယ်။
getParentFile() File

Parent Directory ရဲ့ File Object ကို ပြန်ရမှာဖြစ်ပါတယ်။ သတ်မှတ်ထားခြင်းမရှိပါက null ကို ပြန်ရမှာ ဖြစ်ပါတယ်။
getPath() String

Absolute Path Name ကို Return အနေနဲ့ ပြန်လည် ရရှိမှာ ဖြစ်ပါတယ်။
isAbsolute() boolean

File Object ကို သတ်မှတ်ထားတဲ့ File Name ဟာ Absolute Path ဟုတ်မဟုတ်ကို စစ်ဆေးနိုင်တဲ့ Method ဖြစ်ပါတယ်။
renameTo(File dest) boolean

လက်ရှိ File Object ကို Parameter ထဲက File အဖြစ်ပြောင်းလဲနိုင်တဲ့ Method တစ်ခုဖြစ်ပါတယ်။ File အမည် ပြောင်းတဲ့ နေရာမှာလဲ အသုံးဝင်သလို File တစ်ခုကနေ တစ်ခုကို ပြောင်းရွှေ့တဲ့နေရာမှာလဲ အသုံးဝင်ပါတယ်။

import java.io.*;

public class MoveFile {

    public static void main(String[] args) throws Exception {
        File f1 = new File("file1.txt");
        f1.createNewFile();

        File f2 = new File("dir");
        f2.mkdir();

        File f3 = new File(f2, "file2.txt");
        f1.renameTo(f3);
    }

}


Using Metadata


Method Return Type Description
isDirectory() boolean

Directory တစ်ခု ဟုတ်မဟုတ်ကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
isFile() boolean

File တစ်ခု ဟုတ်မဟုတ်ကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
isHidden() boolean

Hidden File တစ်ခု ဟုတ်မဟုတ်ကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
lastModified() long

နောက်ဆုံး Modified လုပ်သွားသော အချိန်ကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
getTotalSpace() long

File Name နဲ့သတ်မှတ်ထားတဲ့ Partition ရဲ့ ပမာဏကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
getFreeSpace() long

File Name နဲ့သတ်မှတ်ထားတဲ့ Partition ထဲမှာရှိတဲ့ Free Space ကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
getUsableSpace() long

File Name နဲ့သတ်မှတ်ထားတဲ့ Partition ထဲမှာရှိတဲ့ အသုံးပြုနိုင်သော ပမာဏကို သိရှိစေနိုင်သော Method ဖြစ်ပါတယ်
setLastModified(long time) boolean

နောက်ဆုံး Modified လုပ်တဲ့အချိန်ကို သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်
setExcutable(boolean executable) boolean

Program တစ်ခုအနေနဲ့ Run လို့ရမရကို သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်
setExcutable(boolean executable, boolean ownerOnly) boolean

Program တစ်ခုအနေနဲ့ Run လို့ရမရကို File Owner တစ်ယောက်ထဲ လုပ်လို့ရတာလား ဆိုတဲ့ အချက်ကိုပါ သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်
setReadable(boolean readable) boolean

File ကို Read လုပ်လို့ရမရကိုသတ်မှတ်ပေးနိုင်သော Methodဖြစ်ပါတယ်
setReadable(boolean readable, boolean ownerOnly) boolean

File ကို Read လုပ်လို့ရမရကို File Owner တစ်ယောက်ထဲ လုပ်လို့ရတာလား ဆိုတဲ့ အချက်ကိုပါ သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်
setReadOnly() boolean

Read Only File အဖြစ်သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်။
setWritable(boolean writable) boolean

Write လုပ်လို့ရမရကို သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်။
setWritable(boolean writable, boolean ownerOnly) boolean
Write လုပ်လို့ရမရကို File Owner တစ်ယောက်ထဲ လုပ်လို့ရတာလား ဆိုတဲ့ အချက်ကိုပါ သတ်မှတ်ပေးနိုင်သော Method ဖြစ်ပါတယ်


Methods for Directory

အောက်ပါ  Method တွေကတော့ File Object ဟာ Directory တစ်ခုဖြစ်မှသာ အသုံးပြုနိုင်မည့် Method တွေ ဖြစ်ပါတယ်။

Method Return Type Description
list() String[]

လက်ရှိ Directory အောက်မှာရှိတဲ့ File တွေ Directory တွေရဲ့ အမည်ကို String [] အနေနဲ့ ပြန်လည် ရရှိမှာ ဖြစ်ပါတယ်။
list(FileNameFilter filter) String[]

လက်ရှိ Directory အောက်မှာရှိတဲ့ File တွေရဲ့ Directory တွေရဲ့ အမည်ကို FileNameFilter Interface Object ကို သုံးပြီး Filter လုပ်ပြီး ပြန်လည်ရရှိနိုင်မှာ ဖြစ်ပါတယ်
listFiles() File[]

လက်ရှိ Directory အောက်မှာရှိတဲ့ File တွေ Directory တွေကို File[] အနေနဲ့ ပြန်လည် ရရှိမှာ ဖြစ်ပါတယ်။
listFiles(FileNameFilter filter) File[]

လက်ရှိ Directory အောက်မှာရှိတဲ့ File တွေ Directory တွေကို FileNameFilter Interface Object ကို သုံးပြီး Filter လုပ်ပြီး ပြန်လည်ရရှိနိုင်မှာ ဖြစ်ပါတယ်
listFiles(FileFilter filter) File[]

လက်ရှိ Directory အောက်မှာရှိတဲ့ File တွေ Directory တွေကို FileFilter Interface Object ကို သုံးပြီး Filter လုပ်ပြီး ပြန်လည်ရရှိနိုင်မှာ ဖြစ်ပါတယ်

ဒီတစ်ခေါက်တော့ Directory ထဲမှာရှိတဲ့ File တွေကို နာမည်တစ်ခုနဲ့ ရှာပြီး ဖျက်တဲ့ နမူနာတစ်ခု ရေးကြည့်ပါမယ်။ Maven ကို သုံးပြီး Library တွေကို Download လုပ်တဲ့အခါမှာ Connection မကောင်းရင် LastUpdate ဆိုတဲ့နာမည်နဲ့ File တွေကို ချန်ထားပြီး ရပ်သွားတတ်ပါတယ်။ အဲ့ဒီ File တွေကို ရှာပြီး Delete လုပ်ပေးနိုင်တဲ့ နမူနာ တစ်ခုကို ရေးကြည့်ပါမယ်။
import java.io.File;

public class MavenDelete {
    
    public static void main(String[] args) {
        String userHome = System.getProperty("user.home");
        File maven = new File(userHome, ".m2");
        File mavenRepo = new File(maven, "repository");
        deleteFiles(mavenRepo);
    }
    
    static void deleteFiles(File file) {
        if(file.isDirectory()) {
            File [] files = file.listFiles(f -> f.isDirectory() || f.getName().endsWith("lastUpdate"));
            for(File f : files) {
                deleteFiles(f);
            }
        } else {
            file.delete();
        }
    }
}

အထက်ပါ နမူနာထဲမှာတော့ mavenRepo file ကို deleteFiles method ကို သုံးပြီး ဖျက်ခိုင်းပါတယ်။ တကယ်လို့ file Object ဟာ directory ဖြစ်ရင် အဲ့ဒီ​ Directory ထဲက Directory တွေနဲ့ lastUpdate နဲ့ ဆုံးရင် ရယူလာအောင် FileFilter နေရာကို Lambda Expression နဲ့ Filter လုပ်ထားပါတယ်။ရလာတဲ့ File တွေကို Loop ပတ်ပြီး
deleteFile ကို ပြန်ခေါ်ခိုင်းပါတယ်။

တကယ်လို့ Directory ဆိုရင် အောက်မှာ ရှိတဲ့ File တွေကို ရှာပြီး deleteFile ကို ပြန်ခေါ်ပါလိမ့်မယ်။ Directory မဟုတ်ပဲ File တွေဖြစ်နေလို့ကတော့ lastUpdate နဲ့ ဆုံးနေမှာ ဖြစ်တဲ့အတွက် delete လုပ်သွားပါမယ်။


Switch Other Types


Method Return Type Description
toPath() Path

Java NiO2 ရဲ့ Path Object အဖြစ်ပြောင်းပေးနိုင်တဲ့ Method ဖြစ်ပါတယ်
toStirng() String

File Object ရဲ့ Path Name ကို ရရှိနိုင်မှာ ဖြစ်ပါတယ်။ getPath method နှင့် ရလဒ်ခြင်း အတူတူဖြစ်ပါတယ်
toURI() URI

File Path Name ကို ကိုယ်စားပြုသော URI Object ကို ပြန်လည်ရရှိမှာ ဖြစ်ပါတယ်

File Object ဟာ Java IO ကို အသုံးပြုရာမှာ မရှိမဖြစ်လိုအပ်သော Object တစ်ခုဖြစ်ပါတယ်။ လက်ရှိ File တွေ Directory တွေရဲ့ Abstract Path Name ကို ကိုယ်စားပြုတဲ့ Object တစ်ခုဖြစ်ပါတယ်။ File Object တစ်ခုကို ဘယ်လို Create လုပ်မယ်။ File တွေ Directory တွေကို ဘယ်လို တည်ဆောက်မယ်၊ ပြီးရင် Delete လုပ်မယ်။ Directory ထဲမှာရှိတဲ့ File တွေကို ဘယ်လို ရှာဖွေမယ်ဆိုတာကို သိရှိထားအောင် လေ့လာထားဖို့ လိုအပ်ပါမယ်။

ဆက်ပါဦးမယ်
မင်းလွင်

9 comments:

  1. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
    Java Training in Chennai

    ReplyDelete
  2. I'm so glad I can read your very interesting article at least our of Prediksi togel hari ini is very fond of her once ,, thank you for always success for you

    ReplyDelete
  3. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.
    Click here:
    angularjs training in chennai
    Click here:
    angularjs2 training in chennai
    Click here:
    angularjs4 Training in Chennai
    Click here:
    angularjs5 Training in Chennai

    ReplyDelete
  4. This is very good content you share on this blog. it's very informative and provide me future related information.
    Click here:
    Microsoft azure training in online
    Click here:
    Microsoft azure training in tambaram
    Click here:
    Microsoft azure training in chennai
    Click here:
    Microsoft azure training in annanagar

    ReplyDelete
  5. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Blueprism training in Chennai

    Blueprism training in Bangalore

    Blueprism training in Pune

    Blueprism online training

    Blueprism training in tambaram

    ReplyDelete
  6. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops training in tambaram|Devops training in velachery|Devops training in annanagar|Devops training in sholinganallur

    ReplyDelete