Category Archives: Java

「java」BufferedReaderを使ってテキストを読むコード

Javaコード:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileUtilOperate {

public void readLinesFromFile(String filename) {
BufferedReader br = null;
try {
// BufferedReaderオブジェクト作成
br = new BufferedReader(new FileReader(filename));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {

// BufferedReader閉じる
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

public static void main(String[] args) {
FileUtilOperate util = new FileUtilOperate();
util.readLinesFromFile(“startnewsdemo.txt”);

}

}

「Java」BeanUtils.copyPropertiesの使い方

Javaコード:
Product srcProduct = new Product();
srcProduct.setName(“iphone 7”);
Product destProduct = new Product();
PropertyUtils.copyProperties(destProduct, srcProduct);
System.out.println(destProduct.getName());

「Java」文字列のエスケープ処理コード

Javaコード:
public static String escape(String target) {
if (target == null) {
return “”;
}
String afterEscape = target.replace(“&”, “&amp;”).replace(“\””, “&quot;”).replace(“<“, “&lt;”)
.replace(“>”, “&gt;”).replace(“‘”, “&#39;”);
return afterEscape;
}
}

「java開発」System.getProperty(“line.separator”)の使い方

Javaコード
public String toStrFunc() {
String sptr = System.getProperty(“line.separator”);
StringBuffer sb = new StringBuffer();
for(条件) {
//some code
}
return sb.toString();
}

「Java開発」csv文字列の改行コードCRLFを削除するコード

Javaコード
private String sampleFunc(String strCsv){
String strCft = null;
if(strCsv.length() < 2)
strCft = strCsv;
else if ((strCsv.charAt(csv.length() – 2) == (char)13) && (strCsv.charAt(strCsv.length() – 1) == (char)10))
strCft =cstrCsv.substring(0, csv.length()-2);
else
strCft = strCsv;
return strCft;
}

「Spring MVC開発」DispatcherServletの使い方

Javaコード:
public interface HandlerAdapter {
public boolean supports(Object handler);
public void handle(Object handler);
}

public interface Controller {

}

public class HttpController implements Controller{
public void doHttpHandler(){
System.out.println(“http…”);
}
}

public class SimpleController implements Controller{
public void doSimplerHandler(){
System.out.println(“simple…”);
}
}

public class AnnotationController implements Controller{
public void doAnnotationHandler(){
System.out.println(“annotation…”);
}
}

public class SimpleHandlerAdapter implements HandlerAdapter {
public void handle(Object handler) {
((SimpleController)handler).doSimplerHandler();
}

public boolean supports(Object handler) {
return (handler instanceof SimpleController);
}

}
public class HttpHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((HttpController)handler).doHttpHandler();
}

public boolean supports(Object handler) {
return (handler instanceof HttpController);
}

}
public class AnnotationHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((AnnotationController)handler).doAnnotationHandler();
}

public boolean supports(Object handler) {

return (handler instanceof AnnotationController);
}

}
//DispatcherServlet
import java.util.ArrayList;
import java.util.List;
public class DispatchServlet {

public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();

public DispatchServlet(){
handlerAdapters.add(new AnnotationHandlerAdapter());
handlerAdapters.add(new HttpHandlerAdapter());
handlerAdapters.add(new SimpleHandlerAdapter());
}
public void doDispatch(){

SimpleController controller = new SimpleController();
HandlerAdapter adapter = getHandler(controller);
adapter.handle(controller);

}

public HandlerAdapter getHandler(Controller controller){
for(HandlerAdapter adapter: this.handlerAdapters){
if(adapter.supports(controller)){
return adapter;
}
}
return null;
}

public static void main(String[] args){
new DispatchServlet().doDispatch();
}

}

「Spring MVC開発」静的リソースファイルを設定するコード

サンプルコード
<mvc:resources mapping=”/css/**” location=”/WEB-INF/resources/css/” />
<mvc:resources mapping=”/js/**” location=”/WEB-INF/resources/js/” />
<mvc:resources mapping=”/img/**” location=”/WEB-INF/resources/img/” />
<mvc:resources mapping=”/upload/**” location=”/WEB-INF/resources/upload/” />
<mvc:resources mapping=”/export/**” location=”/WEB-INF/resources/export/” />

「Java入門」バイト配列を16進数の文字列に変換するコード

Javaコード
public static String byte2hexFunc(byte[] bytes) {
final String strHex = “0123456789abcdef”;
StringBuilder sb = new StringBuilder(bytes.length * 2);
for (byte b : bytes) {
sb.append(strHex.charAt((b >> 4) & 0x0f));
sb.append(strHex.charAt(b & 0x0f));
}

return sb.toString();
}

「org.apache.struts2.dispatcher.SessionMapにセッションを取得する方法

Javaコード
public V get(Object key) {
if(this.session == null) {
return null;

} else {
synchronized(this.session.getId().intern()) {
return this.session.getAttribute(key.toString());
}
}
}

「Apache Commons Lang」パッケージにhashCode()とequals()のコード

Javaコード
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class Employee
{
private Integer id;
private String firstname;
private String lastName;
private String department;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
@Override
public int hashCode()
{
final int PRIME = 31;
return new HashCodeBuilder(getId()%2==0?getId()+1:getId(), PRIME).
toHashCode();
}
@Override
public boolean equals(Object o) {
if (o == null)
return false;
if (o == this)
return true;
if (o.getClass() != getClass())
return false;
Employee e = (Employee) o;
return new EqualsBuilder().
append(getId(), e.getId()).
isEquals();
}
}