java 上文件传示例_Java文件类String [] list()方法(带示例)

java 上文件传示例

文件类字符串[] list() (File Class String[] list())

  • This method is available in package java.io.File.list().

    软件包java.io.File.list()中提供了此方法。

  • This method is used to return the names of all the files in the form of an array of strings which is represented in the filepath.

    此方法用于以字符串数组的形式返回所有文件的名称,这些字符串以文件路径表示。

  • The return type of this method is String[] i.e. It returns an array of strings of all the files which is represented in filepath if the given path is directory else return null.

    此方法的返回类型为String [],即,如果给定路径为目录,则返回文件路径中表示的所有文件的字符串数组,否则返回null。

  • This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.

    如果未授予文件写入权限,则此方法可能会引发异常(即Security Exception)。

  • This method is overridable. The first method does not accept any parameter and the second method accept one parameter.

    此方法是可重写的。 第一种方法不接受任何参数,第二种方法则接受一个参数。

Syntax:

句法:

    String[] list(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is String[] i.e. it returns all files names in an array of strings which is represented in a filepath.

此方法的返回类型为String [],即它以文件路径表示的字符串数组形式返回所有文件名。

Java程序演示list()方法的示例 (Java program to demonstrate example of list() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise an 
// exception when working with files
import java.lang.Exception;

public class ToListFiles {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");

            // By using list() returns all the files and directories 
            // which is represented in a file path if file path is a directory .
            String[] filelist = file.list();
            System.out.println("These are the name of files represented in a given directory :" + file.getPath());

            // By using loop to traverse the filenames and directories 
            // in the given path .
            for (int i = 0; i < filelist.length; i++)
                System.out.println(filelist[i]);
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

D:\Programs>javac ToListFiles.java

D:\Programs>java ToListFiles
These are the name of files represented in a given directory : C:\Users\computer clinic\OneDrive\Articles
Java LinkedList addFirst.docx
myjava.txt
Pattern_in_java.docx
Types of Inheritance in Java.docx
unpublished_article.docx


翻译自: https://www.includehelp.com/java/file-class-string-list-method-with-example.aspx

java 上文件传示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值