从添加到项目的jar中读取动态Web项目中存在的属性文件

时间:2014-12-12 11:06:41

标签: java jar

我正在开发一个动态网络项目(DWP),它有一个属性文件。创建了一个jar,它应该读取DWP中属性文件的内容以及属性文件中的更改,并将更改合并到DWP中。我无法从jar中读取属性文件。我想知道DWP中的jar如何读取DWP的属性文件

这里是jar的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class fileReader extends Thread 
{
    publicstatic long oldTMS;
    long currentTMS;
    File file;
    public static long limit;
    //static String Path = "./configuration/RequestLimit.properties";
    static String Path="configuration/RequestLimit.properties";
    public static Properties properties;    
    public fileReader() throws IOException, InterruptedException {    
        System.out.println("Reading the property file for the first time");
        Thread thread = new Thread();
        setDaemon(true);    
        FileInputStream in = new FileInputStream(Path);
        System.out.println("fd:"+in.getFD());
        properties = new Properties();
        properties.load(in);
        String requestLimit = properties.getProperty("RequestLimit");           
        System.out
                .println("request limit as read from the property file is ----"
                        + requestLimit);
        limit = Long.parseLong(properties.getProperty("RequestLimit"));    
        System.out.println("request limit as per the  reading is ---" + limit);
        start();
        // waits for the thread to die
        join();    
    }    
    public void run() {    
        System.out.println(" thread starts");
        if (Thread.currentThread().isDaemon()) {
            while (true) {
                try {
                    Thread.sleep(3000);
                    System.out.println("entered in infinite loop");
                    file = new File(Path);
                    System.out.println("file name is --------" + file);
                    currentTMS = file.lastModified();
                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "E yyyy.MM.dd 'at' hh:mm:ss a zzz");    
                    System.out.println("file initial change time stamp:::::"
                            + dateFormat.format(currentTMS));    
                    try {    
                        System.out
                                .println("lets see if the file has changed or not !!");    
                        FileInputStream d = new FileInputStream(file);    
                        if (oldTMS != currentTMS) {
                            System.out.println("checking for old and new time stamp!");    
                            oldTMS = currentTMS;    
                            System.out.println("file modified last at-----------"
                                            + oldTMS);
                            // reload property
                            try {    
                                System.out.println("Reloading property file");
                                properties.load(d);    
                                System.out.println("After Reloading property file properties : "
                                                + properties
                                                        .getProperty("RequestLimit"));    
                                limit = Long.parseLong(properties
                                        .getProperty("RequestLimit"));
                                System.out.println("request limit as per the  reading is ---"
                                                + limit);    
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                } 
                catch (InterruptedException e2) 
                {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }    
            }
        }    
    }    
} 

像这样在DWP中使用jar

package com.ING;    
import java.io.IOException;    
import com.change.fileReader;    
public class deamonCaller
{    
    public long callDeamon() throws IOException, InterruptedException 
    {
        fileReader fr = new fileReader();
         Long requestLimit =fileReader.limit;
        return requestLimit;            
    }    
}

3 个答案:

答案 0 :(得分:1)

请尝试使用以下代码。我希望,它会帮助你很多。

static String Path="configuration/RequestLimit.properties";
loadProperties(Path);     

private void loadProperties(String propertiesName) {

        if (properties != null) {
            return;
        }

        // Properties properties = null;
        InputStream inputStream = null;

        inputStream = this.getClass().getResourceAsStream(propertiesName);
        if (inputStream == null) {
            throw new Exception(propertiesName + "something");
        }

        properties = new Properties();
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

答案 1 :(得分:0)

将路径作为-Dproperty = .....提供给java nad然后从那里读取而不是硬编码该值。

答案 2 :(得分:0)

对于那些可能像我一样被卡住的人来说。这对我来说是一个意外之灾。当一个想法点击我的头,实施并且它起作用时,解释了对老年人的问题..无论如何,这是一个解决方案。

将Propertyfile直接放在Web内容中,无需将文件放在动态Web项目的文件夹中。在Dynamic Web proj(DWP)的Servelet中,使用以下代码行:

String pathPropertyFile = request.getSession().getServletContext()
                .getRealPath("")
                + "\\RequestLimit.properties";
        System.out.println("path of the property file is"+pathforFileReader);

传递此路径获取的" pathPropertyFile"到具有读取属性文件的代码的jar的类。 我使用了Bean类,在beann中设置路径,然后从bean中获取路径并将其传递给读取属性文件的JAR类的方法。

public class deamonCaller {


    public long callDeamon(Tao tao) throws IOException, InterruptedException {

        // obtain the path from the bean Tao
        String Path = tao.getPath();
        System.out.println("path in deamon:" + Path);
        // call the jar method while passing PATH as a parameter
        fileReader fr = new fileReader(Path);

        Long requestLimit = fileReader.limit;
        return requestLimit;

    }

}

文件阅读器的代码

package com.change;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class fileReader extends Thread {
    static long oldTMS;
    long currentTMS;
    File file;
    public static long limit;
    //static String Path = "./configuration/RequestLimit.properties";
     public static String pathName;
    public static Properties properties;

    public fileReader(String Path) throws IOException, InterruptedException {

        pathName=Path;
        System.out.println("Reading the property file for the first time");
        Thread thread = new Thread();
        setDaemon(true);

        FileInputStream in = new FileInputStream(pathName);
        System.out.println("fd:"+in.getFD());
        properties = new Properties();
        properties.load(in);
        String requestLimit = properties.getProperty("RequestLimit");

        System.out
                .println("request limit as read from the property file is ----"
                        + requestLimit);
        limit = Long.parseLong(properties.getProperty("RequestLimit"));

        System.out.println("request limit as per the  reading is ---" + limit);
        start();
        // waits for the thread to die
        join();

    }

    public void run() {

        System.out.println(" thread starts");
        if (Thread.currentThread().isDaemon()) {
            while (true) {
                try {
                    Thread.sleep(3000);
                    System.out.println("entered in infinite loop");
                    file = new File(pathName);
                    System.out.println("file name is --------" + file);
                    currentTMS = file.lastModified();
                    SimpleDateFormat dateFormat = new SimpleDateFormat(
                            "E yyyy.MM.dd 'at' hh:mm:ss a zzz");

                    System.out.println("file initial change time stamp:::::"
                            + dateFormat.format(currentTMS));

                    try {

                        System.out
                                .println("lets see if the file has changed or not !!");

                        FileInputStream d = new FileInputStream(file);

                        if (oldTMS != currentTMS) {
                            System.out
                                    .println("checking for old and new time stamp!");

                            oldTMS = currentTMS;

                            System.out
                                    .println("file modified last at-----------"
                                            + oldTMS);
                            // reload property
                            try {

                                System.out.println("Reloading property file");
                                properties.load(d);

                                System.out
                                        .println("After Reloading property file properties : "
                                                + properties
                                                        .getProperty("RequestLimit"));

                                limit = Long.parseLong(properties
                                        .getProperty("RequestLimit"));
                                System.out
                                        .println("request limit as per the  reading is ---"
                                                + limit);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }

                } catch (InterruptedException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }

            }
        }

    }

}