Category Archives: Java

「JSTL入門」c:setタグとc:ifタグのサンプル

JSPコード
<c:set var=”age” value=”18″/>
<b>年齢:</b>
<c:out value=”${age}”/>
<br/>

<c:if test=”${(age >= 19) && (age <= 29)}”>
<b>20代の人です。</b><br/>
</c:if>
<c:if test=”${(age < 19) || (age > 29}”>
<b>20代以外の人です!</b><br/>
</c:if>

「Java入門」synchronized(object)で排他制御を行うサンプル

Javaコード
class Demo {
private Object obj1 = new Object();
private Object obj2 = new Object();

public void methodA() {
//some code
synchronized(obj1) {
i ++;
}
//略
}

 

public void methodB() {
//some code
synchronized(obj2) {
j ++;
}
//略
}

「Java入門」Integer.parseInt()で文字列を整数に変換する

Javaコード
/**
*
* @param cityStr
* @return
*/
public static CityType changeValFunc(String cityStr)
{
Integer type;
try {
type = Integer.parseInt(cityStr);
}
catch(Exception e)
{
xxxx;
}
xxx;
}

「Java入門」Tools.class.getClassLoader().getResource()で絶対パスを取得する

Javaコード
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource(“/”).getPath();
String rootPath = “”;
//windowsの場合
if(“\\”.equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf(“/WEB-INF/classes”));
rootPath = rootPath.replace(“/”, “\\”);
}
//linuxの場合
if(“/”.equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf(“/WEB-INF/classes”));
rootPath = rootPath.replace(“\\”, “/”);
}
return rootPath;
}

「Java入門」Cookieを利用する方法

1.Cookieの保存方法
Cookie cookie = new Cookie(“EmployeeInfo”, “EmployeeInfo_Val”);
cookie.setMaxAge(60*60*2);
response.addCookie(cookie);

2.Cookieの取得方法
String value = null;
Cookie cookie[] = request.getCookies();

if(cookie != null){
for(int i = 0; i < cookie.length; i++){
if(cookie[i].getName().equals(“EmployeeInfo”)){
value = cookie[i].getValue();
}
}
}

java.io.FileOutputStream.FileOutputStream()の使い方

サンプルコード
package com.arkgame.study;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileOutputStreamDemo {

public static void main(String[] args) {
String fileName = “D:\\soft” + File.separator + “world.txt”;
File f = new File(fileName);
OutputStream out;
try {
out = new FileOutputStream(f);
String str = “test data”;
byte[] b = str.getBytes();
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}

「Java入門」java.util.StringTokenizerの使い方

Javaコード
import java.util.StringTokenizer;

public class TokenizerDemo {

public static void main(String[] args) {
String dig = “Tokyo/Fukuoka/Oosaka/Okinawa”;
String delim = “/”;
StringTokenizer st = new StringTokenizer(dig, delim);
int count = st.countTokens();
String[] cft = new String[count];
for (int i = 0; i < count; i++) {
cft[i] = st.nextToken();
System.out.println(cft[i]);
}
}

}

結果
Tokyo
Fukuoka
Oosaka
Okinawa

「Java入門」java.util.Arrays.toString(binarySearch)の使い方

Javaコード
package com.arkgame.study;
import java.util.Arrays;

public class ChangfaTuDemo {

public static void main(String[] args) {

int cft[] = { 13, 24, 35, 47, 69, 16, 62, 76, 98 };
Arrays.sort(cft);
System.out.println(“after sort:”);
System.out.println(Arrays.toString(cft));
int point = Arrays.binarySearch(cft, 47);
System.out.println(“find element 47 position:” + point);
Arrays.fill(cft, 99);
System.out.println(“fill element:”);
System.out.println(Arrays.toString(cft));

}

}

結果
after sort:
[13, 16, 24, 35, 47, 62, 69, 76, 98]
find element 47 position:4
fill element:
[99, 99, 99, 99, 99, 99, 99, 99, 99]

java.lang.String.getBytes()でファイルを書き込むサンプル

Javaコード
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BytesFileDemo {

public static void main(String[] args) {
String fileName = “C:\\arkgame” + File.separator + “hello.txt”;
File f = new File(fileName);

try {
OutputStream out = new FileOutputStream(f);

String str = “this is a test”;
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}