简体   繁体   English

如何从Java中的文件夹命名文件?

[英]How to name a file from a folder in java?

I have a file named InputFile.txt in a resources folder. 我在资源文件夹中有一个名为InputFile.txt的文件。 My project structure is like this: 我的项目结构如下:

  • VirtualMemory 虚拟内存
    • src src
      • resources 资源
        • InputFile.txt InputFile.txt
      • VirtualMemory 虚拟内存
        • VirtualMemory.java 虚拟内存

And I am trying to access the InputFile.txt in VirtualMemory.java class by like this: 我试图通过这样访问VirtualMemory.java类中的InputFile.txt:

String filename = ("./src/resources/InputFile.txt");
File file = new File(filename); 

But the file is not being found. 但是找不到该文件。 How to resolve this problem? 如何解决这个问题?

Below code will help load a properties file from any where in the classpath. 下面的代码将帮助从类路径中的任何位置加载属性文件。

ClassLoader cl = ClassLoader.getSystemClassLoader();
    if (cl != null) {
        URL url = cl.getResource(CONF_PROPERTIES);
        if (url == null) {
            url = cl.getResource("/" + CONF_PROPERTIES);
        }
        if (url != null) {
            try {
                InputStream in = url.openStream();
                props = new Properties();
                props.load(in);
            } catch (IOException e) {
                // Log the exception
            } finally {
               // close opened resources
            }

        }
    }

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

相关问题 Java从一个文件夹搜索文件名 - Java searching file name from the one folder Java将文件从文件夹复制到另一个名称不同的文件夹 - Java copy file from folder to another folder with different name 如何使用带有空格的文件夹名称的参数(即具有完整路径的文件名)从Java运行bat文件 - How to run bat file from java with arguments (i.e file name with full path) having folder name with space 如何使用Java代码从具有当前日期和时间的文件夹中获取文件名 - How to Fetch a file name from a folder with current Date and Time using Java Code 爪哇蔚来| 路径和文件 | 如何使用从其他对象获取的自定义名称创建自定义文件和文件夹? - Java NIO | Paths & Files | How to create custom file and folder with custom name fetched from other object? Java-从文件夹获取不带扩展名的文件名 - Java - Getting file name without extension from a folder Java代码从文件夹中获取文件名列表 - Java code to get the list of file name from a folder 如何从 Java 中的 jar 创建文件/文件夹? - How to create a file/folder from jar in Java? 如何从 Java Micronaut 中的文件夹中读取文件 - How to read file from folder in Java Micronaut 如何从Java中的其他文件夹导入文件? - How to import file from other folder in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM