Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将参数传递给";路径“;Java中的类型参数?_Java - Fatal编程技术网

如何将参数传递给";路径“;Java中的类型参数?

如何将参数传递给";路径“;Java中的类型参数?,java,Java,我使用BufferedReader类读取文本流,尝试了以下Java代码 public void editTexts(Path inputFile) throws IOException{ BufferedReader reader = Files.newBufferedReader(inputFile)){ // ---------- } } 在本代码中,参数类型被称为Path。我有一个问题,在传递的论点 我想知

我使用
BufferedReader
类读取文本流,尝试了以下Java代码

public void editTexts(Path inputFile) throws IOException{
    BufferedReader reader = Files.newBufferedReader(inputFile)){
        // ----------                         
    }
}
在本代码中,参数类型被称为
Path
。我有一个问题,在传递的论点

我想知道如何将参数传递给
路径
参数类型?

试试看


比如说,你有一个
路径
指向一个目录,比如说Windows机器的根目录

您可以像这样创建
路径

Path rootPath = Paths.get("C:\\");
如果您现在想要传递一个参数,比如文件名,那么您可以这样做

String fileName = "some_file.txt";
Path filePath = rootPath.resolve(fileName);
为确保一切正常,请打印两个
Path
s的绝对路径

System.out.println("root path is " + rootPath.toAbsolutePath().toString());
System.out.println("path to the file in root is " + fileInRootPath.toAbsolutePath().toString());
您可以对这些路径执行检查,因为在内存中创建它们并不一定意味着路径正确且文件系统对象存在

// check if the path exists
if (Files.exists(filePath)) {
    // check if the path points to a regular file (not a directory or symbolic link)
    if (Files.isRegularFile(filePath)) {
        System.out.println(filePath.toAbsolutePath().toString() 
                + " exists and is a regular file");
    } else {
        System.out.println(filePath.toAbsolutePath().toString() 
                + " exists, but is not a regular file");
    }
} else {
    System.out.println(filePath.toAbsolutePath().toString()
            + " does not exist");
}

你想传递什么参数?我想把参数传递到inputFile。我已经理解了,但是你想传递什么类型的参数呢?路径不一定指向文件(也可以是目录或符号链接),文件系统对象也不一定存在。您想通过文件名或子目录扩展路径吗?非常感谢您抽出时间!在我做了你建议的所有更改之后,它工作得非常好!您还可以进一步解释“我还怀疑您的代码示例没有使用路径inputFile…不应该在文件中使用bigFile.newbufferedeader(bigFile)替换为inputFile”@UpaniK不客气!你介意接受我的回答吗?当然@deHaar你能解释一下为什么我要使用
bigFile
而不是
inputFile
。我想这是因为最佳实践。我只是想确定一下。@UpaniK您不应该使用
bigFile
,因为它不是参数,而是另一个变量(可能是以前定义的)。在您的代码示例中,使用的两个
Path
s应该相同,参数。。。这就是为什么我在我的回答中写了这个,好的。我的错。!现在我明白了。我编辑了我的问题以避免讨论@德哈再次感谢你!谢谢你的回答<代码>路径。get()完成了任务!你能解释一下路径被逗号分隔的原因吗?
// check if the path exists
if (Files.exists(filePath)) {
    // check if the path points to a regular file (not a directory or symbolic link)
    if (Files.isRegularFile(filePath)) {
        System.out.println(filePath.toAbsolutePath().toString() 
                + " exists and is a regular file");
    } else {
        System.out.println(filePath.toAbsolutePath().toString() 
                + " exists, but is not a regular file");
    }
} else {
    System.out.println(filePath.toAbsolutePath().toString()
            + " does not exist");
}