Category Archives: Java

「Java」XMLのエスケープ文字列をメタ文字に変換するコード

Javaコード
public static String Escape2MetaStr(String str) {
String cftResult = “”;
cftResult = str.replace(“&amp”, “&”);
cftResult = cftResult.replace(“&lt”, “<“);
cftResult = cftResult.replace(“&gt”, “>”);
cftResult = cftResult.replace(“&quot”, “\””);
cftResult = cftResult.replace(“&#039”, “\'”);

return cftResult;
}

「Java」keySet()と entrySet()でMapの中身を全て取得する方法

//keySet()  
Set<String> set = map.keySet();
for (String s:set) {
System.out.println(s+”,”+map.get(s));
}

  //entrySet()
Set<Map.Entry<String, String>> entryseSet=map.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
System.out.println(entry.getKey()+”,”+entry.getValue());
}

「java」List配列(数値)の最大値・最小値を取得するコード

1.List<double[]>配列
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 11.3, 15.5, 13.8, 16.8, 23.4, 24.4, 26.4, 25.1, 23.6, 34.3, 17.2,
13.9 });
values.add(new double[] { 10, 10, 12, 35, 20, 24, 26, 26, 23, 18, 14, 11 });
values.add(new double[] { 6, 5.3, 8, 32, 37, 22, 24.2, 24, 19, 15, 9, 6 });
values.add(new double[] { 27, 10, 31, 15, 39, 23, 34, 25, 56, 18, 13, 10 });

2.最大値(max value)
public double ListDoubleArrMaxVal(List<double[]> dataList) {
double max = Double.MIN_VALUE;
for (double[] ds : dataList) {
for (double d : ds) {
if (d > max) {
max = d;
}
}
}
return max;
}
3.最小値(min value)
public double ListDoubleArrMinVal(List<double[]> dataList) {
double min = Double.MAX_VALUE;
for (double[] ds : dataList) {
for (double d : ds) {
if (d < min) {
min = d;
}
}
}
return min;
}

「Spring」設定ファイルを読み込む方法

Javaコード

public void readprop() throws IOException
{
Resource resource = ApplicationContextFactory.getApplicationContext().getResource(“classpath:com/cftspring/resource/tdemoconfig.txt”);

File file = resource.getFile();
byte[] buffer =new byte[(int) file.length()];
FileInputStream is =new FileInputStream(file);

is.read(buffer, 0, buffer.length);

is.close();
String str = new String(buffer);
System.out.println(str);

}

「Java」コレクションSetのメモ

1.Set<String>のコード
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}

2.forループのコード
for (String str : set) {
System.out.println(str);
}

3.Set<Object>のコード
Set<Object> set = new HashSet<Object>();
for (Object obj: set) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
//some code
}

「java」Log4jでログ配置ファイルのコード

1.必要なjar
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-rc1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-rc1</version>
</dependency>

2.log4j.properties
#Global logging configuration
log4j.rootLogger=DEBUG,stdout,file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[comment-album-service] %-5p %d{HH:mm:ss SSS} %c:%L – %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/logs/album/album-picture-service.log
log4j.appender.file.DatePattern=’.’yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss SSS} %-5p [%c:%L] – %m%n

「java」Class.forName(“java.lang.String”)の使い方

javaコード
Class c = Class.forName(“java.lang.String”);
Object obj = c.newInstance();

class ABC {
public void master(String s) {
System.out.println(“Working on \”” + s + “\””);
}
}
Class cft = ABC.class;
Class[] argTypes = {String.class};
Method worker = cft.getMethod(“master”, argTypes);
Object[] theData = {“食品メーカーが塩分や糖質を抑えたカップ麺の販売”};
worker.invoke(new ABC(), theData);

「Java」DOMによるXML文書を作成するコード

Javaコード
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = fact.newDocumentBuilder();
Document doc = parser.newDocument();

Node root = doc.createElement(“Poem”);
doc.appendChild(root);

Node stanza = doc.createElement(“Stanza”);
root.appendChild(stanza);

Node line = doc.createElement(“Line”);
stanza.appendChild(line);
line.appendChild(doc.createTextNode(“this is a demo one”));
line = doc.createElement(“Line”);
stanza.appendChild(line);
line.appendChild(doc.createTextNode(“this is a demo two”));

「Javar」jarまたはzipファイルを読み書きメモ

Javaコード
// jarまたはzipを読む
ZipFile zf = new ZipFile(“startnews.jar”);
Enumeration all = zf.entries();
while (all.hasMoreElements()) {
ZipEntry entry = (ZipEntry)all.nextElement();
if (entry.isFile())
println(“Directory: ” + entry.getName());

// 读写文件
FileOutputStream os = new FileOutputStream(entry.getName());
InputStream is = zf.getInputStream(entry);
int n = 0;
byte[] b = new byte[8092];
while ((n = is.read(b)) > 0) {
os.write(b, 0, n);
is.close();
os.close();
}
}

// gzip書き込み/読み込み
FileInputStream fin = new FileInputStream(FILENAME);
GZIPInputStream gzis = new GZIPInputStream(fin);
InputStreamReader xover = new InputStreamReader(gzis);
BufferedReader is = new BufferedReader(xover);
String line;
while ((line = is.readLine()) != null)
System.out.println(“Read: ” + line);

「Spring 」静的リソースへのアクセス方法まとめ

方法1
<mvc:resources mapping=”/images/**” location=”/images/” />

方法2
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>