Javaファイルクラスとは
Fileクラスはjava.ioパッケージで定義されており、ストリームでは直接動作しません。Java Fileクラスはファイルやディレクトリに関する情報を管理するクラスです。ご存知のとおり、オペレーティング システム レベルでは、ファイルとディレクトリは別のオブジェクトです。ただし、Java では、両方とも同じFileクラスによって記述されます。ファイルの参照になぜ文字列を使用しないのかと疑問に思われるかもしれません。まず第一に、ファイル アクセスはオペレーティング システムによって異なるためです。ファイルクラスのコンストラクター
Java.io.Fileクラス オブジェクトがファイルを表すかディレクトリを表すかに応じて、コンストラクターの 1 つを使用してオブジェクトを作成できます。 File(String pathname)この場合、コンストラクターは指定されたパス名を変換して新しいFileインスタンスを作成します。文字列を抽象パス名に変換します。 File(String myPath, String myString)このコンストラクターは、myPath パス名文字列と myString パス名文字列から 新しいFileインスタンスを作成します。File(Fileparent, String name) は、ファイル抽象パス名と名前パス名の文字列から 新しいFileインスタンスを作成します。この場合のFile(URI uri)コンストラクターは、指定された file: URI を抽象パス名に変換することによって、新しいFileインスタンスを作成します。URI は、Uniform Resource Identifier (URI) 参照を表す Java クラスです。以下は、さまざまなJava.io.Fileクラス コンストラクターの例です。import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
public class FileTest {
public static void main(String[] args) throws URISyntaxException {
File myFile1 = new File("d:\\MyCat");
File myFile2 = new File ("d:\\MyCat", "cat.txt");
File myFile3 = new File(myFile1, "cat.txt");
URI uri = new URI ("https://docs.oracle.com/javase/7/docs/api/java/net/URI.html");
File myFile4 = new File(uri);
}
}
ファイルクラスのメソッド
Java Fileクラスには、ファイルやディレクトリを操作できるメソッドが多数あります。それらのいくつかを考えてみましょう。-
boolean createNewFile() は、コンストラクターに渡されたパスに新しいファイルを作成します。成功した場合は true を返し、それ以外の場合は false を返します。
-
boolean delete() は、コンストラクターに渡されたパス内のディレクトリまたはファイルを削除します。削除が成功すると true を返します
-
boolean selected() は、コンストラクターで指定されたパスにファイルまたはディレクトリが存在するかどうかを確認します。ファイルまたはディレクトリが存在する場合は true を返し、存在しない場合は false を返します。
-
String getAbsolutePath() は、オブジェクトのコンストラクターに渡されたパスの絶対パスを返します。
-
String getName() はファイルまたはディレクトリの短縮名を返します
-
String getParent()は親ディレクトリの名前を返します。
-
boolean isDirectory() は、指定されたパスにディレクトリが含まれている場合に true を返します。
-
boolean isFile() は、指定されたパスにファイルがある場合に true を返します。
-
boolean isHidden() は、ディレクトリまたはファイルが非表示の場合に true を返します。
-
long length()はファイルのサイズをバイト単位で返します。
-
long lastModified() は、ファイルまたはディレクトリが最後に変更された時刻を返します。値は、Unix エポックから経過したミリ秒数を表します。
-
String[] list() は、特定のディレクトリにあるファイルとサブディレクトリの配列を返します。
-
File[] listFiles() は、特定のディレクトリにあるファイルとサブディレクトリの配列を返します。
-
boolean mkdir()は新しいディレクトリを作成し、成功した場合は true を返します。
-
boolean renameTo(File dest)ファイルまたはディレクトリの名前を変更します
Java File クラスのいくつかの機能
-
抽象パスまたは文字列パスは、絶対パスまたは相対パスのいずれかにすることができます。抽象パスの親は、そのクラスのgetParent()メソッドを呼び出すことで取得できます。
-
まず、 Fileクラスのオブジェクトを作成し、それにファイルまたはディレクトリの名前を渡します。ファイル システムは、読み取り、書き込み、実行など、実際のファイル システム オブジェクトに対する特定の操作に制限を課すことができます。このような制限をアクセス許可と呼びます。
-
Fileクラスのインスタンスは不変です。つまり、ファイルを作成すると、Fileオブジェクトによって表される抽象パスは決して変更されません。
ファイルクラスのコード例
ディレクトリを操作するプログラムを作成しましょう。まず、指定されたパスにディレクトリを作成し、次に新しいファイルを作成して、ファイルとディレクトリが存在するかどうかを確認する必要があります。import java.io.File;
import java.io.IOException;
public class FileTest2 {
public static void main(String[] args) throws IOException {
//create a directory using mkdir() File class method
File dir = new File("d:\\MyDir");
boolean created = dir.mkdir();
if(created)
System.out.println("Folder has been created...");
else
System.out.println("Folder hasn't been created...");
File myFile = new File("d:\\MyDir\\cat.txt");
myFile.createNewFile();
System.out.println("File name: " + myFile.getName());
System.out.println("Parent folder: " + myFile.getParent());
if(myFile.exists())
System.out.println("File exists");
else
System.out.println("File not found");
}
}
出力は次のとおりです。
import java.io.File;
public class FileTest3 {
public static void main(String[] args) {
File dir = new File("d:\\MyDir");
File myFile = new File("d:\\MyDir\\cat.txt");
System.out.println("File name: " + myFile.getName());
System.out.println("Parent folder: " + myFile.getParent());
if (myFile.exists())
System.out.println("File exists");
else
System.out.println("File not found");
System.out.println("Absolute path: " + myFile.getAbsolutePath());
if (myFile.exists()) {
System.out.println("Is writable: " + myFile.canWrite());
System.out.println("Is readable: " + myFile.canRead());
System.out.println("Is a directory: " + myFile.isDirectory());
System.out.println("myFile Size in bytes = " + myFile.length());
}
}
}
プログラムの出力は次のとおりです。
GO TO FULL VERSION