有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在java中检查文件详细信息?

我有个档案。它有财产细节

<> P>考虑我的文件有以下内容。

primaryDirectory=C:/Users/aa021/Desktop/IS
primaryKey=InvoiceNumber
dbName=MYSQL
strDBHost=jdbc:mysql://localhost/sakila
dbUser=root
dbPass=root123

这是属性文件。我必须写一个程序来检查所有属性是否存在? 如果它不存在,那么我将编写日志文件

我的代码是:

public static void main(String[] args) throws Exception
    {

        String logPath="";
        String primaryDir="";
        String primaryKey="";
        String dbName="";
        String strDBHost="";
        String user="";
        String password="";
        String [] attributes=null;



        logPath=PropertyReader.getProperty("logFilePath");
        System.out.println(logPath);

        primaryDir=PropertyReader.getProperty("primaryDirectory");
        System.out.println(primaryDir);

        primaryKey=PropertyReader.getProperty("primaryKey");
        System.out.println(primaryKey);

        dbName=PropertyReader.getProperty("dbName");
        System.out.println(dbName);

        strDBHost=PropertyReader.getProperty("strDBHost");
        System.out.println(strDBHost);

        user=PropertyReader.getProperty("dbUser");
        System.out.println(user);

        password=PropertyReader.getProperty("dbPass");
        System.out.println(password);



    }

如何检查所有细节

如何为这个写条件?任何人都可以帮我


共 (1) 个答案

  1. # 1 楼答案

    这是一个属性文件,因此使用Properties类:

    final Properties properties = new Properties();
    
    final Path path = Paths.get("path/to/the/file");
    
    try (
        final BufferedReader reader = Files.newBufferedReader(path,
            StandardCharsets.UTF_8)
    ) {
        properties.load(reader);
    }
    
    // Use your properties here
    

    如果您要检查的是文件的元数据,那么请使用^{}类中专用于此的方法,例如:

    final BasicFileAttributeView view = Files.getFileAttributeView(path,
        BasicFileAttributeView.class);
    

    有关可用的不同属性,请参阅相关的javadoc