首页 > Java > java教程 > 正文

Java文件类

WBOY
发布: 2024-08-30 16:01:06
原创
831 人浏览过

Java中的File类在保存应用程序的路径和以目录格式管理各种文件方面起着非常重要的作用。此外,此方法用于按过于抽象的顺序或层次结构管理和版本化路径。使用任何字符串命名任何文件不足以提及应用程序的路径。在不同平台上运行应用程序并尝试使用一个或多个属性来绑定和编译自身。 File 类由各种方法组成,这使 java 可以灵活地创建文件、重命名文件和删除文件。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法和参数:

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(File Parent, String child): 文件父参数负责从创建的文件对象中调用抽象路径名,字符串子参数负责从文件中调用子路径名字符串作为文件的新实例创建的对象。
  • 文件(URI uri):当给定文件的 URI 转换为抽象路径名时,将创建文件的新实例。
  • 文件(字符串父级,字符串子级):从父路径名创建文件的新实例,并将子路径名字符串作为参数传递给文件。
  • 文件(字符串路径名):一旦路径名字符串转换为抽象路径名,就会创建文件的新实例。

方法

方法如下:

  • 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 类为方法和所有其他文件提供了很大的灵活性。

以上是Java文件类的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板