Category Archives: Java

「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

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

Javaコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class DescendingIteratorDemo {

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

// use add() method to add elements in the deque
cft.add(36);
cft.add(42);
cft.add(55);
cft.add(60);

//iterator() is used to print all the elements
//next() returns the next element on each iteration
System.out.println(“iteratorで要素を出力:”);

for(Iterator<Integer> itr = cft.iterator();itr.hasNext();) {
System.out.println(itr.next());
}

//descendingIterator() is used to print the elements in reverse order
//next() returns the next element on each iteration
System.out.println(“要素を逆順:”);

for(Iterator<Integer> descItr =cft.descendingIterator();descItr.hasNext();) {
System.out.println(descItr.next());
}
}

}

結果

iteratorで要素を出力:
36
42
55
60
要素を逆順:
60
55
42
36

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

javaコード

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayListPeekDemo {

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

// use add() method to add elements in the deque
chang.add(44);
chang.add(37);
chang.add(28);
chang.add(98);

// this will retrieve head of the queue
int retval = chang.peek();
System.out.println(“Retrieved Element is ” + retval);

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

}

結果

Retrieved Element is 44
Value = 44
Value = 37
Value = 28
Value = 98

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

Javaコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class ArrayListIeratorDemo {

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

// use add() method to add elements in the deque
cft.add(20);
cft.add(32);
cft.add(45);
cft.add(29);

// iterator() is used to print all the elements
// next() returns the next element on each iteration
System.out.println(“printing elements using iterator:”);
for(Iterator<Integer> kd = cft.iterator();kd.hasNext();) {
System.out.println(kd.next());
}
}

}

結果

printing elements using iterator:
20
32
45
29

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

Javaコード

import java.util.ArrayList;

public class AddlistAllDemo {

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

// use add() method to add elements in the list
cftlist.add(20);
cftlist.add(30);
cftlist.add(55);

// let us print all the elements available in list1
System.out.println(” list1の要素下記:”);
for (Integer numVal : cftlist) {
System.out.println(“Value = ” + numVal);
}

// create an empty array list2 with an initial capacity
ArrayList<Integer> cftlist2 = new ArrayList<Integer>(5);

// use add() method to add elements in list2
cftlist2.add(15);
cftlist2.add(20);
cftlist2.add(21);
cftlist2.add(75);

// let us print all the elements available in list2
System.out.println(“list2の要素下記:”);
for (Integer numVal : cftlist2) {
System.out.println(“Value = ” + numVal);
}

// inserting all elements, list2 will get printed after list1
cftlist.addAll(cftlist2);

System.out.println(“すべての要素下記:”);
// let us print all the elements available in list1
for (Integer numVal : cftlist) {
System.out.println(“Value = ” + numVal);
}
}

}

結果

list1の要素下記:
Value = 20
Value = 30
Value = 55
list2の要素下記:
Value = 15
Value = 20
Value = 21
Value = 75
すべての要素下記:
Value = 20
Value = 30
Value = 55
Value = 15
Value = 20
Value = 21
Value = 75

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

Javaコード

import java.util.ArrayDeque;

import java.util.Deque;

public class ArrayDequeofferFirssDeno {

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(12);
deque.add(23);
deque.add(28);
deque.add(45);

// this will insert 10 at the front
deque.offerFirst(20);

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

}

実行結果:

Number = 20
Number = 12
Number = 23
Number = 28
Number = 45

「Java」現在の日時を取得するコード

Javaコード
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SeparatorDemo {

/**
* 現在の日時
* @return
*/
public static String getNowTime() {
SimpleDateFormat df = new SimpleDateFormat(“yyyy/MM/dd/HH:mm:ss:SSS”, Locale.JAPAN);

return df.format(new Date());
}
public static void main(String[] args) {
String strTime=getNowTime();
System.out.println(“現在の時間:”+strTime);

}
}
結果
現在の日時:2017/05/24/13:17:10:531