Java-io-file-tostring

提供:Dev Guides
2020年6月29日 (月) 10:25時点におけるMaintenance script (トーク | 投稿記録)による版 (Imported from text file)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先:案内検索

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

説明

  • java.io.File.toString()*メソッドは、この抽象パス名のgetPath()メソッドによって返されたパス名文字列を返します。

宣言

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

public String toString()

パラメーター

NA

戻り値

メソッドは、この抽象パス名の文字列形式を返します。

例外

NA

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

package com.finddevguides;

import java.io.File;

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

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

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

        //if file exists
         if(bool) {

           //pathname string of this abstract pathname
            str = f.toString();

           //print
            System.out.println("pathname string: "+str);
         }

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

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

pathname string: test.txt