2

Webアプリケーションで改札を使用しています。次のように、いくつかの.propertiesファイルに文字列を保存します。

foo.properties

page.label=dummy

htmlファイルでは、page.label次のように文字列にアクセスできます。

index.html

<wicket:message key="page.label">Default label</wicket:message>

ここで、アプリケーション用にいくつかのjunitテストケースを作成し、プロパティファイルに保存されている文字列にアクセスしたいと思います。私の質問は、プロパティファイルから文字列を読み取る方法ですか?

4

5 に答える 5

4

これを試して

import java.io.FileInputStream;
import java.util.Properties;

public class MainClass {
  public static void main(String args[]) throws IOException {
    Properties p = new Properties();
    p.load(new FileInputStream("foo.properties"));
    Object label = p.get("page.label");
    System.out.println(label);
  }
}

このセクションでは、必要な場所からすべてのプロパティファイルを読み取り、プロパティにロードできます。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MainClass {

    private static String PROPERTIES_FILES_PATHNAME = "file:///Users/ftam/Downloads/test/";// for mac

    public static void main(String args[]) throws Exception {
        Properties p = new Properties();

        List<File> files = getFiles();
        for(File file : files) {
            FileInputStream input = new FileInputStream(file);
            p.load(input);
        }

        String label = (String) p.get("page.label");
        System.out.println(label);
    }

    private static List<File> getFiles() throws IOException, URISyntaxException {
        List<File> filesList = new ArrayList<File>();

        URL[] url = { new URL(PROPERTIES_FILES_PATHNAME) };
        URLClassLoader loader = new URLClassLoader(url);
        URL[] urls = loader.getURLs();

        File fileMetaInf = new File(urls[0].toURI());
        File[] files = fileMetaInf.listFiles();
        for(File file : files) {
            if(!file.isDirectory() && file.getName().endsWith(".properties")) {
                filesList.add(file);
            }
        }

        return filesList;
    }
}
于 2012-12-04T09:06:58.360 に答える
2

Wicketには、コンポーネントツリーを考慮して、リソースをローカライズする独自の方法があります。StringResourceLoaderについては、javadocを参照してください。

リソースをロードする1つの方法は、次のとおりです。

WicketTester tester = new WicketTester(new MyApplication());
tester.startPage(MyPage.class);
Localizer localizer = tester.getApplication().getResourceSettings()
                            .getLocalizer();
String foo = localizer.getString("page.label",tester.getLastRenderedPage(), "")
于 2012-12-04T16:33:24.933 に答える
1

Apache Commons Configurationを使用することは、かなり良い選択です。

于 2012-12-04T09:00:04.697 に答える
1

使用loadしてからget("page.label")

于 2012-12-04T09:02:02.720 に答える
0

このフィールドをクラス内に配置します。

import java.util.ResourceBundle;

private static ResourceBundle settings = ResourceBundle.getBundle("test",Locale.getDefault());

次に、次のtest.propertiesようなファイル:

com.some.name=someValueHere

最後に、次の方法でプロパティ値にアクセスできます。

private String fieldName = settings.getString("com.some.name");
于 2013-12-24T10:50:16.177 に答える