gpt4 book ai didi

java - java.io.File 中允许的文件分隔符

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:59 25 4
gpt4 key购买 nike

在学习文件分隔符的时候发现一个比较迷惑的问题

假设文件 c:\book\java 存在。以下哪行代码创建了一个代表该文件的对象? (选择所有适用项。)

1。新文件("c:\book\java");
2. new File("c:\\book\\java");
3. 新文件("c:/book/java");
4. new File("c://book//java");
5.以上都不是

这本书(假设一个基于dos的文件系统)说

  1. & 3.是正确答案
  1. is correct because Java requires a backslash to be escaped with another backslash.
  2. is also correct because Java will convert the slashes to the right one when working with paths..
  • 按这个逻辑4也能正确吗?
  • 当我更改为 mac/linux 文件系统时 - 答案应该是什么? (更新:我们只是在创建一个文件对象 - 它是否有意义不是这里的问题 - 问题是其中哪一个最终会返回上面显示的路径)

最佳答案

Can 4 also be correct by this logic?

鉴于问题是

Suppose that the file c:\book\java exists. Which of the following lines of code creates an object that represents the file? (Choose all that apply.)

  1. new File("c:\book\java");
  2. new File("c:\book\java");
  3. new File("c:/book/java");
  4. new File("c://book//java");
  5. None of the above

2和3显然是正确的。那么,4中的File对象是否“创建了一个表示文件的ojbect”?

是的。

假设C:\book\java存在,这段代码

public static void main( String[] args ) throws IOException
{
File f = new File( args[ 0 ] );
System.err.printf( "args[0]: %s\n", args[ 0 ] );
System.err.printf( "Path: %s\n", f.getCanonicalPath() );
}

产生这个输出:

args[0]: C://book//java
Path: C:\book\java

所以 new File( "C://book//java") 绝对是“创建一个代表文件的对象”,也是正确答案。

任何不是它的论点在字面上都是不正确的。问题是字符串是否“创建了一个代表文件的对象”。 C://book//java 确实做到了这一点

关于java - java.io.File 中允许的文件分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49092433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com