Skip to content

Instantly share code, notes, and snippets.

@shoheiyokoyama
Last active November 6, 2016 07:17
Show Gist options
  • Save shoheiyokoyama/0def5b93834e408e2ca2 to your computer and use it in GitHub Desktop.
Save shoheiyokoyama/0def5b93834e408e2ca2 to your computer and use it in GitHub Desktop.
デザインパターン「Adapter」 ref: http://qiita.com/shoheiyokoyama/items/bd1c692db480b640c976
Apple=iPod
import java.io.*;
public interface FileIo {
public void readFromFile(String filename) throws IOException;
public void writeToFile(String filename) throws IOException;
public void setValue(String key, String value);
public String getValue(String key);
}
import java.util.*;
import java.io.*;
public class FileProperties extends Properties implements FileIo {
public void readFromFile(String filename) throws IOException {
load(new FileInputStream(filename));
}
public void writeToFile(String filename) throws IOException {
store(new FileOutputStream(filename), "Written by FileProperties");
}
public void setValue(String key, String value) {
setProperty(key, value);
}
public String getValue(String key) {
return getProperty(key, "");
}
}
public static void main(String[] args) {
FileIo fileIo = new FileProperties();//※
try {
fileIo.readFromFile("file.txt");
fileIo.setValue("Apple", "iPhone");
fileIo.setValue("Google", "Android");
fileIo.setValue("Git", "Hub");
fileIo.setValue("DesignPattern", "Adapter");
fileIo.writeToFile("newfile.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
#Written by FileProperties
#Sun Feb 07 20:49:50 JST 2016
DesignPattern=Adapter
Apple=iPhone
Git=Hub
Google=Android
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment