Java-io-file-getabsolutefile

提供:Dev Guides
移動先:案内検索

Java.io.File.getAbsoluteFile()メソッド

説明

  • java.io.File.getAbsoluteFile()*メソッドは、この抽象パス名の絶対形式を返します。

宣言

以下は* java.io.File.getAbsoluteFile()*メソッドの宣言です-

public File getAbsoluteFile()

パラメーター

NA

戻り値

このメソッドは、絶対抽象パス名で定義された同じファイルを返します。

例外

*SecurityException* -システムプロパティ値にアクセスできない場合。

次の例は、java.io.File.getAbsoluteFile()メソッドの使用法を示しています。

package com.finddevguides;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      File f = null;
      File f1 = null;
      String path = "";
      boolean bool = false;

      try {
        //create new files
         f = new File("test.txt");

        //create new file in the system
         f.createNewFile();

        //create new file object from the absolute path
         f1 = f.getAbsoluteFile();

        //returns true if the file exists
         bool = f1.exists();

        //returns absolute pathname
         path = f1.getAbsolutePath();

        //if file exists
         if(bool) {

           //prints
            System.out.print(path+" Exists? "+ bool);
         }

      } catch(Exception e) {
        //if any error occurs
         e.printStackTrace();
      }
   }
}

上記のプログラムをコンパイルして実行すると、次の結果が生成されます-

D:\EclipseAndroid\IO\BufferedInputStream\test.txt Exists? true