challenge 初期設定の読み書き

 初期設定を読み書きするプログラムを書いてください。

 保存先や形式は問いませんが,OS,ライブラリ,言語等の環境で標準的なものがあれば,なるべくそちらを用いてください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package org.doukaku.ja.preference;

import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;

public class HelloPreference {
    private static final String MESSAGE_KEY = "message";
    private static final String DEFAULT_MESSAGE = "Hello, Preference.";

    private String message;
    private Preferences pref;

    public HelloPreference() {
        loadPreference();
    }

    public void loadPreference() {
        setPreference(Preferences.userNodeForPackage(this.getClass()));
        setMessage(pref.get(MESSAGE_KEY, DEFAULT_MESSAGE));
    }

    public void setMessage(String message) { this.message = message; }
    public String getMessage() { return this.message; }
    public void setPreference(Preferences pref) { this.pref = pref; }
    public Preferences getPreference() { return this.pref; }

    public void showMessage() {
        System.out.println(getMessage());
    }
    public void storePreference() throws BackingStoreException {
        Preferences pref = getPreference();
        pref.put(MESSAGE_KEY, getMessage());
        pref.flush();
    }

    public static void main(String[] args) {
        try {
            HelloPreference    hello = new HelloPreference();
            if (args.length > 0) {
                hello.setMessage(args[0]);
            }
            hello.showMessage();
            hello.storePreference();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Posted feedbacks - C#

お題のJavaを.NETに移植した感じ。 System.Configuration.dllを参照してビルドすること。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Configuration;

class HelloPreference
{
  const string MESSAGE_KEY = "message";
  const string DEFAULT_MESSAGE = "Hello, Preference.";

  public string Message { get; set; }
  public Configuration Configuration { get; set; }

  public HelloPreference()
  {
    LoadPreference();
  }

  public void LoadPreference()
  {
    Configuration conf = ConfigurationManager
      .OpenExeConfiguration(ConfigurationUserLevel.None);
    this.Configuration = conf;
    KeyValueConfigurationElement element
      = conf.AppSettings.Settings[MESSAGE_KEY];
    this.Message = element != null
      ? element.Value
      : DEFAULT_MESSAGE;
  }

  public void StorePreference()
  {
    Configuration conf = this.Configuration;
    KeyValueConfigurationCollection settings
      = conf.AppSettings.Settings;
    KeyValueConfigurationElement elem
      = settings[MESSAGE_KEY];
    if (elem != null)
    {
      elem.Value = this.Message;
    }
    else
    {
      settings.Add(MESSAGE_KEY, this.Message);
    }
    conf.Save();
  }

  public void ShowMessage()
  {
    Console.WriteLine(this.Message);
  }

  static void Main(string[] args)
  {
    try
    {
      HelloPreference hello = new HelloPreference();
      if (args.Length > 0)
      {
        hello.Message = args[0];
      }
      hello.ShowMessage();
      hello.StorePreference();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex);
    }
  }
}

Index

Feed

Other

Link

Pathtraq

loading...