Javaファイルクラス

WBOY
リリース: 2024-08-30 16:01:06
オリジナル
611 人が閲覧しました

Java の File クラスは、アプリケーションのパスを保存したり、さまざまなファイルをディレクトリ形式で管理したりする上で非常に重要な役割を果たします。また、この方法は、抽象的すぎる順序または階層でパスを管理およびバージョン管理するために使用されます。任意の文字列を使用してファイルに名前を付けるだけでは、アプリケーションのパスを言及するのに十分ではありません。異なるプラットフォームでアプリケーションを実行し、アプリケーション自体を 1 つ以上の属性でバインドしてコンパイルしようとします。 File クラスはさまざまなメソッドで構成されており、これにより Java はファイルの作成、ファイル名の変更、ファイルの削除を柔軟に行うことができます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文とパラメータ:

File abslt = new File("https://cdn.educba.com/usr/local/bin/at.txt");
ログイン後にコピー

構文フローは次のように動作します。

  • ファイル: これは、ファイルが操作の実行で考慮されることを表します。
  • abslt: ファイル名を表します。ファイルに関連するものであれば何でもよく、つまり、何らかのコンテンツや値が含まれます。
  • new File(): ファイルに関連するオブジェクト、またはファイル値に関する関連情報を含めて後で値を取得できるファイル オブジェクトの作成。
  • (“/usr/local/bin/at.txt”): 渡されるパラメータは、推奨される絶対パスです。相対パスをパラメータとして渡す方法は、あまり推奨されません。

Java ではファイル クラスはどのように機能しますか?

Java のファイル クラスは、クラス内のファイルに後で必要になる可能性のあるコンテンツが含まれるように機能します。これらは、ファイル オブジェクトを使用して後で取得および変更できます。これらのファイルは異なる名前とパス名で階層内に維持されるため、名前として文字列のみを使用するだけでは十分ではなく、推奨されます。これは、維持されるファイルとディレクトリのパス名の抽象表現です。

ユーザーの指定に応じて、パス名は絶対パス名または相対パス名になります。絶対パスであれば、検索時に絶対パスで指定されたファイルを取得するのに役立つので、非常に推奨される方法です。したがって、実際の作業は、まずファイル クラスを作成することから始まり、次に、ファイル名とディレクトリ名が渡されるそれぞれのオブジェクトが作成されます。ファイル システムは、実際のファイル システム オブジェクトにすでに存在する制約により、オブジェクトのアクセス時にアクセスを禁止したり、制限を設けたりすることがあります。これにより、読み取り、書き込み、実行などの操作に支障が生じます。ファイル クラスによって作成されたインスタンスは不変であるため、一度作成されるとパス名を変更することはできず、ファイル オブジェクトの表現も変更されません。

コンストラクター

Java の File クラスに渡され、頻繁に使用されるコンストラクターがあります:

  • File(Fileparent, String child): ファイルの親パラメータは、作成されたファイル オブジェクトから抽象パス名を呼び出す役割を果たし、String 子パラメータはファイルから子のパス名文字列を呼び出す役割を果たします。ファイルの新しいインスタンスとして作成されたオブジェクト。
  • File(URI uri): 指定されたファイルの URI が抽象パス名に変換されると、ファイルの新しいインスタンスが作成されます。
  • File(Stringparent, String child): ファイルの新しいインスタンスが親のパス名から作成され、子のパス名の文字列がパラメータとしてファイルに渡されます。
  • File(String pathname): パス名の文字列が抽象パス名に変換されるとすぐに、ファイルの新しいインスタンスが作成されます。

メソッド

以下にメソッドを示します:

  • boolean createNewFile(): The abstract pathname passed as an argument to the method is responsible for creating a new empty file.
  • int compareTo(File pathname): When any two pathnames are arranged in lexical order, this method is used to compare both the files and then create a new instance of the file.
  • boolean canExecute(): The abstract pathname parameter passed as an argument is responsible for testing whether the application can execute and function properly.
  • boolean canRead(): The abstract pathname parameter passed as an argument is responsible for testing whether the application can read the passed file.
  • boolean canWrite(): The abstract pathname parameter passed as an argument is responsible for testing whether the application can write some content on the passed file.
  •  String getAbsolutePath(): The abstract pathname passed as a parameter for creating an instance returns the abstract pathname string.
  • boolean exists(): This method is responsible for verifying whether the abstract pathname exists.
  • boolean delete(): This method deletes the file or the directory having the contents which is pointed with this abstract pathname.
  • static File createTempFile(String prefix, String suffix): This method is used for creating an empty temporary file or directory by default.
  • boolean equals(Object obj): The file object created as part of the abstract pathname is responsible for verifying the equality with the given object.
  • String[] list(): Files present in the directory or file system gets returned in the form of an array as strings.
  • long length(): The abstract pathname denoted by the file object is responsible for getting the length of the file.
  • boolean isHidden(): This method is responsible for testing whether the abstract pathname names the file is a hidden file.
  • boolean isFile(): This method is responsible for testing whether the file returned is a normal file or is denoted by the abstract pathname object.
  • boolean isDirectory(): This method is responsible for testing whether the file denoted by this abstract pathname is a directory or not.
  • boolean mkdir(): This method is responsible for creating the directory named by passing the abstract pathname.
  •  File[] listFiles(): All the files present in the directory is responsible for returning an array of abstract pathnames as denoted by the object.
  • boolean setReadable(boolean readable): It is used for setting the owner’s or user’s permission for reading the content present in the file.
  • boolean renameTo(File dest): The file represented by the abstract pathname is responsible for the renaming of the file.
  • boolean setExecutable(boolean executable): This method is used for setting the owner’s execution permission.
  • boolean setReadable(boolean readable, boolean ownerOnly): This method is responsible for setting up the constraint owner’s readability permission.
  • URI toURI(): Representation of this abstract pathname is done by constructing the URI file.
  • String toString(): This method is responsible for returning the string pathname of the abstract pathname.
  • boolean setReadOnly(): This method is used only to perform the read operations exclusively.
  • boolean setWritable(boolean writable): This method is used for setting up the write permission for the owner even at the time of execution.

Examples

Now we will learn about the Java File and its various operations with the help of examples.

Example #1

This program is used for demonstrating the creation of a new file and return the file is already present if it exists already.

Code:

import java.io.File;
import java.io.IOException;
public class FilePropTest {
public static void main(String[] args) {
try {
File file = new File("java_File.txt");
if (file.createNewFile()) {
System.out.println("Created a new file");
} else {
System.out.println("File is already present ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
ログイン後にコピー

Output:

Javaファイルクラス

Example #2

This program is used to demonstrate the filenames being passed as an absolute pathname and retrieving all the filenames within the directory of the drive.

Code:

import java.io.File;
public class FilePropTest2 {
public static void main(String[] args) {
File fil_nm=new File("C:/Users/adutta/documents");
String all_file_names[]=fil_nm.list();
for(String enlist_filename:all_file_names){
System.out.println(enlist_filename);
}
}
}
ログイン後にコピー

Output:

Javaファイルクラス

注: 説明および定義したすべてのメソッドはこの方法で実証でき、Java のファイル クラスにすでに存在するメソッド名を呼び出すだけで実行できます。

結論 – Java ファイルクラス

Java の File クラスは、外部仕様が一切含まれていないものの、ファイル パッケージの一部として存在するメソッドが含まれているため、非常に便利なクラスであり、取得などのファイル関連アクティビティを実行するためにシームレスに使用できます。ファイルの名前とファイルの取得をそれぞれ行います。したがって、File クラスはメソッドと他のすべてのファイルに大きな柔軟性を提供します。

以上がJavaファイルクラスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!