简体   繁体   English

Linux上的Java保存文件在哪里?

[英]Where is java saving files on Linux?

so I have this problem: 所以我有这个问题:

I need to know where is Java saving the files when you create them with new, like this File file = new File ("file.txt"); 我需要知道在使用new创建文件时Java保存文件的位置,例如File file = new File ("file.txt"); on Linux? 在Linux上?

Linux on not File file = new File ("file.txt") does not create a file on the file system. Linux on not File file = new File ("file.txt")不会在文件系统上创建文件。 File is just a file path holder. 文件只是一个文件路径持有者。 You need to call file.createNewFile to create a file. 您需要调用file.createNewFile来创建文件。 Relative paths like file.txt are resolved against the current user directory, typically the directory in which the Java virtual machine was invoked file.txt这样的相对路径是针对当前用户目录解析的,通常是调用Java虚拟机的目录

Assuming the file is actually being created by additional code (as Evgeniy mentioned), you could try checking to see if your current working directory isn't what you expect it to be. 假设文件实际上是由其他代码创建的(如Evgeniy所提到的),您可以尝试检查当前的工作目录是否与您预期的不同。 To find that directory you could try: 要找到该目录,您可以尝试:

String cwd = System.getProperty("user.dir"));
System.out.println("Current working directory: " + cwd);

or just 要不就

System.out.println ("Path to file: " + file.getAbsolutePath());

To see where it should end up. 看看它应该在哪里结束。

像在Windows中一样的地方:在当前目录中

Your mentioned code doesn't create new file physically on drive, but logically yes. 您提到的代码不会在驱动器上物理创建新文件,但逻辑上是。 If you alter the code to create a new file like file.createNewFile then the directory where you executed the code, a file would be created there. 如果您更改代码以创建一个新文件,如file.createNewFile,那么您执行代码的目录将在那里创建一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM