Category Archives: Java

「java」getClassLoader().getResourceAsStream()の使い方

1.test.properties
city=Tokyo

2.Javaコード
public class CftInfo {
//some code
try {
InputStream is = null;
Properties dd = new Properties();
is = CftInfo.class.getClassLoader().getResourceAsStream(“test.properties”);
dd.load(is);
is.close();

String strCity = dd.getProperty(“city”);
//略
} catch (Exception e) {
e.printStackTrace();
}

}

「Java」 セッションIDを再発行するメモ

javaコード
String oldID = super.servletRequest.getSession().getId();
super.servletRequest.getSession().invalidate();
super.servletRequest.getSession(true);
String newID = super.servletRequest.getSession(true).getId();

「Java」getName()、getSimpleName()とgetCanonicalName()の使い方

javaコード
package cftj;

public class ChangfaTunClassDemo {

public static void main(String[] args) {
System.out.println(“外部クラス”);
System.out.println(” Name:” + ChangfaTunClassDemo.class.getName());
System.out.println(” Simple: ” + ChangfaTunClassDemo.class.getSimpleName());
System.out.println(” Canonical: ” + ChangfaTunClassDemo.class.getCanonicalName());
System.out.println(“匿名クラス”);
new Object() {
public void className() {
System.out.println(” Name : ” + getClass().getName());
System.out.println(” Simple: ” + getClass().getSimpleName());
System.out.println(” Canonical: ” + getClass().getCanonicalName());
}
}.className();

}
}
結果
外部クラス
Name:cftj.ChangfaTunClassDemo
Simple: ChangfaTunClassDemo
Canonical: cftj.ChangfaTunClassDemo
匿名クラス
Name : cftj.ChangfaTunClassDemo$1
Simple:
Canonical: null

「Java」java.util.ArrayDeque.pop()のサンプルコード

Javaコード
import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequePopDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> deque = new ArrayDeque<Integer>(8);

// use add() method to add elements in the deque
deque.add(15);
deque.add(23);
deque.add(36);
deque.add(45);

// printing all the elements available in deque
for (Integer number : deque) {
System.out.println(“Number = ” + number);
}

int retval = deque.pop();
System.out.println(“Element removed is ” + retval);

// printing all the elements available in deque after using pop()
for (Integer number : deque) {
System.out.println(“Number = ” + number);
}
}
}
結果
Number = 15
Number = 23
Number = 36
Number = 45
Element removed is 15
Number = 23
Number = 36
Number = 45

「Java」matcher.find()の使い方

Javaコード
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherDemo {

public static void main(String[] args) {
String strOo = “BB XXX PP BB QQQ BBB ZZ”;

String string = “BB”;
Pattern pattern = Pattern.compile(string);
Matcher matcher = pattern.matcher(strOo);

while(matcher.find()) {
System.out.println(“Found at: “+ matcher.start()+
” – ” + matcher.end());
}
}
}
結果
Found at: 0 – 2
Found at: 10 – 12
Found at: 17 – 19

「Java」properties ファイルからの値を取得するサンプルコード

1.kuroapp.properties
username = root
password = 123456
2.PropRead.java
import java.util.Properties;
import java.io.FileInputStream;
import java.io.InputStream;

public class PropRead {
public static void main (String[] args) {

Properties properties = new Properties();

String file = “/dara/kuroapp.properties”;
try {
InputStream inputStream = new FileInputStream(file);
properties.load(inputStream);
inputStream.close();

System.out.println(properties.getProperty(“username”));
System.out.println(properties.getProperty(“password”));

} catch (Exception ex) {
System.out.println(ex.getMessage());

}

}
}

「java」propertiesファイルを読み込むサンプルコード

Javaコード
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class OperateProperties {
public OperateProperties(){

}
// keyからvalueの取得
public static String readValue(String filePath,String key) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
String value = props.getProperty (key);
System.out.println(key+value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

//propertiesファイルからの値を取得
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
props.load(in);
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty (key);
System.out.println(key+Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}

//propertiesを書く
public static void writeProperties(String filePath,String parameterName,String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
prop.load(fis);
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
prop.store(fos, “Update ‘” + parameterName + “‘ value”);
} catch (IOException e) {
System.err.println(“Visit “+filePath+” for updating “+parameterName+” value error”);
}
}
}

「Spring」OpenSessionInViewFilterを設定するサンプルコード

<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

「Java」正規表現でIPアドレスのフォーマットをチェックするサンプルコード

Javaコード
public static boolean ipCheck(String text) {
if (text != null && !text.isEmpty()) {
//正規表現
String regex = “^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.”
“(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$”;
// IPアドレスのチェック
if (text.matches(regex)) {
// some code
return true;
} else {
// some code
return false;
}
}
return false;
}

「java入門」java.util.ArrayList.get()のサンプルコード

Javaコード

import java.util.ArrayList;

public class ArrayListGetDemo {

public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList<Integer> cflist = new ArrayList<Integer>(5);

// use add() method to add elements in the list
cflist.add(23);
cflist.add(24);
cflist.add(45);
cflist.add(86);

// let us print all the elements available in list
for (Integer number : cflist) {
System.out.println(“Value = ” + number);
}

// retrieves element at 4th postion
int retval=cflist.get(3);
System.out.println(“Retrieved element is = ” + retval);
}

}

結果

Value = 23
Value = 24
Value = 45
Value = 86
Retrieved element is = 86