File.exist()方法不起作用?

时间:2016-12-13 06:48:18

标签: java

更新1: - 大家好,我发现有些人无法提出任何建议,因为问题不是那么清楚。

所以我简化了它们的代码并能够再次重现它。现在您只需要按ctrl c和ctrl v并相应地设置属性文件。

package com.test;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

public class ReproduceIssue {

    static {

        try {

            String propertyFilePath = "/home/jioapp/apache-tomcat-7.0.56";

            File file1 = new File(propertyFilePath);

            URL[] urls = { file1.toURI().toURL() };

            ClassLoader loader = new URLClassLoader(urls);

            ResourceBundle resourceBundle = ResourceBundle.getBundle("TinyOffline",Locale.getDefault(), loader);

            String timestamp = new SimpleDateFormat("dd.MM.yyyy-HH.mm.ss").format(new Date());

            /** Note: If file already open, moving no longer possible. */

            String logFileAbsolutePath = resourceBundle.getString("log4j.absolute.file.path");

            System.out.println(logFileAbsolutePath); /** /home/jioapp/apache-tomcat-7.0.56/OfflineLog/TinyOffline.log */

            File file = new File(logFileAbsolutePath);

            System.out.println(file);/** \home\jioapp\apache-tomcat-7.0.56\OfflineLog\TinyOffline.log */

            System.out.println(file.exists());/** false */

            if (file.exists()) {

                String renameToFile = logFileAbsolutePath + "." + timestamp;

                if (!file.renameTo(new File(
                        renameToFile))) {

                    System.out.println("Renaming log file failed : " + logFileAbsolutePath);

                }

            } else {

                System.out.println("Unable to open log file : "
                        + logFileAbsolutePath);

            }

            /** Note: If Piwik file already exist then rename it **/

            String piwikLogFileAbsolutePath = resourceBundle.getString("sys.piwik.absolute.file.path");

            System.out.println(piwikLogFileAbsolutePath);/** /home/jioapp/apache-tomcat-7.0.56/PiwikLog/access.log */

            File piwikFile = new File(piwikLogFileAbsolutePath);

            System.out.println(piwikFile);/** \home\jioapp\apache-tomcat-7.0.56\PiwikLog\access.log */

            System.out.println(piwikFile.exists());/** true */

            if (piwikFile.exists()) {

                if (!piwikFile.renameTo(new File(piwikLogFileAbsolutePath + "."
                        + timestamp))) {

                    System.out.println("Renaming piwik log file failed : "
                            + logFileAbsolutePath);

                    System.out.println("Stopping the services....");

                    System.exit(0);

                }

            } else {

                System.out.println("No Piwik access log file exist :"
                        + piwikLogFileAbsolutePath);

            }

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
}

TinyOffline.properties

log4j.absolute.file.path=/home/jioapp/apache-tomcat-7.0.56/OfflineLog/TinyOffline.log
sys.piwik.absolute.file.path=/home/jioapp/apache-tomcat-7.0.56/PiwikLog/access.log

我认为这可能有所帮助。

Java JDK-7.0.79

2 个答案:

答案 0 :(得分:0)

这真的很尴尬,但我只是重写了#34; TinyOffline"日志文件的文件名以及属性文件中的文件名也按预期开始运行。我假设这可能是由于字符串值中的一些垃圾字符。

答案 1 :(得分:-1)

首先尝试检查logFileAbsolutePath是否已保存字符串。如果resourceBundle.getString返回一些值,则尝试检查它返回的内容。在我看来,它不会在对象中返回/存储值。