Category Archives: Java

「Java開発」正規表現式で括弧の文字列を取得するコード

javaコード
package com.arkgame.study;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KakoSeki {

static List<String> getTeacherInfoLst(String teachers) {
List<String> ls = new ArrayList<String>();
Pattern pattern = Pattern.compile(“(?<=\\()(.+?)(?=\\))”);
Matcher matcher = pattern.matcher(teachers);
while (matcher.find())
ls.add(matcher.group());
return ls;
}

public static void main(String[] args) {
String t1 = “employee2(0012) employee3(0013) employee4(0015) employee6(0016)”;
List<String> ab = new ArrayList<String>();
ab = getTeacherInfoLst(t1);
for (String s : ab) {
System.out.println(s);
}

}

}

結果
0012
0013
0015
0016

「Java入門」半角数字を全角数字に変換するサンプル

Javaコード
public class Hankaku2Zenkaku {

/**
* 全角数字を半角数字に変換
*
* @param str
*/
public static String full2Half(String strDigit) {
if (strDigit == null) {
throw new IllegalArgumentException();
}
StringBuffer sb = new StringBuffer(strDigit);
for (int i = 0; i < strDigit.length(); i++) {
char c = strDigit.charAt(i);
if (‘0’ <= c && c <= ‘9’) {
sb.setCharAt(i, (char) (c – ‘0’ + ‘0’));
}
}
return sb.toString();
}

/**
* 半角数字を全角数字に変換
*
* @param str
*/
public static String half2Full(String strDigit) {
if (strDigit == null) {
throw new IllegalArgumentException();
}
StringBuffer sb = new StringBuffer(strDigit);
for (int i = 0; i < strDigit.length(); i++) {
char c = strDigit.charAt(i);
if (‘0’ <= c && c <= ‘9’) {
sb.setCharAt(i, (char) (c – ‘0’ + ‘0’));
}
}
return sb.toString();
}

public static void main(String[] args) {

String aa = half2Full(“123456”);
System.out.println(“結果1:” + aa);
String bb = full2Half(“98755”);
System.out.println(“結果2:” + bb);
}

}

java.io.BufferedReader.readLine() でCSVファイルを読み込む方法

Javaコード
package itstudy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class CsvReadDemo {

public static void main(String[] args) {
try {
File cnt = new File(“C:\\study\\test.csv”);
InputStreamReader zd = new InputStreamReader(new FileInputStream(cnt), “Windows-31J”);
BufferedReader breader = new BufferedReader(zd);

String dataLine = “”;
while ((dataLine = breader.readLine()) != null) {
String[] ttArr = dataLine.split(“,”, 0);

for (String k : ttArr) {
System.out.println(k);
}
}
breader.close();

} catch (IOException e) {
System.out.println(e);
}
}
}

java.io.BufferedReader.BufferedReader()のサンプル

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

public class BufferDemoAk {

public static void main(String[] args) {
try {

String cft;
FileReader fr = new FileReader(“C:\\study\\20180228.txt”);
BufferedReader breader = new BufferedReader(fr);
cft = breader.readLine();
while (cft != null) {
System.out.println(cft);
cft = breader.readLine();
}
breader.close();
fr.close();
} catch (IOException e) {
System.out.println(“エラーメッセージ:” + e);
}

}

}

「Java入門」java.util.ArrayList.add()のサンプル

Javaコード
import java.util.ArrayList;

public class ArrayStrdemo {

public static void main(String[] args) {
ArrayList<String> rt = new ArrayList<String>();
int i = 0;
while (i < 10) {
rt.add(“msg” + i);
i++;
}
String cnt = rt.get(3);
System.out.println(cnt);

}

}

結果

msg3

「Java入門」java.lang.System.currentTimeMillis()のサンプル

Javaコード
package itstudy;
import java.util.Random;

public class RandSample {

public static void main(String[] args) {
long seed = System.currentTimeMillis(); // 現在時刻のミリ秒
Random r = new Random(seed);
int sec = r.nextInt(8000);
System.out.println(sec);
}

}
結果
1672

「Java入門」java.lang.String.format()のサンプル

Javaコード
package com.itstudy;
public class StrfmtDemo {

public static void main(String[] args) {
String ctn = String.format(“%s-> %s-> %s”, “it”, “change”, “world”);
String ctn2 = String.format(“%dx%dx%d”, 11, 22, 33);
String ctn3 = String.format(“%2d”, 8);
String ctn4 = String.format(“result->%2s:%2d”, “test11”, 85);
System.out.println(ctn);
System.out.println(ctn2);
System.out.println(ctn3);
System.out.println(ctn4);

}

}
結果:
it-> change-> world
11x22x33
8
result->test11:85

「Java開発」com.google.zxingでQR コードを生成(解析)するコード

1.jarを設定
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>

2.QRコードを生成
public static void main(String[] args) throws IOException {
String text=”www.google.com”;
int width=100;
int height=100;
String format=”png”;
Hashtable hints=new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hints.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix=new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height,hints);
Path file=new Java.io.File(“C:/qrcode.png”).toPath();
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

3.QRコードの解析
public static void main(String[] args) throws NotFoundException {
MultiFormatReader formatReader=new MultiFormatReader();
File file =new File(“C:/qrcode.png”);
BufferedImage image=null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BinaryBitmap binaryBitmap =new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
Hashtable hints=new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);
Result result=formatReader.decode(binaryBitmap,hints);
System.err.println(“解析結果:”+result.toString());
System.out.println(result.getBarcodeFormat());
System.out.println(result.getText());
}

java.lang.System.currentTimeMillis()のサンプル

Javaコード
package sample;
import java.sql.Timestamp;
public class TimeStamapDemo {
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); System.out.println(“現在時刻:\n”  +   timestamp);
}
}

結果
現在時刻:
2018-02-15 14:11:15.034

「Java入門」System.arraycopy()のサンプル

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

public class ArraycopyDemo {

public static void main(String[] args) {
int[] ctn = { 12, 24, 56, 78, 51 };
System.out.println(Arrays.toString(ctn));
System.arraycopy(ctn, 0, ctn, 3, 2);
System.out.println(Arrays.toString(ctn));

int[] ctn2 = new int[6];
System.arraycopy(ctn, 1, ctn2, 0, 3);
System.out.println(Arrays.toString(ctn2));

try {
System.arraycopy(ctn, 0, ctn2, 0, ctn.length + 1);
} catch (IndexOutOfBoundsException ex) {

System.out.println(“occur IndexOutOfBoundsException “);
}
System.out.println(Arrays.toString(ctn2));
}

}
実行結果
[12, 24, 56, 78, 51]
[12, 24, 56, 12, 24]
[24, 56, 12, 0, 0, 0]
occur IndexOutOfBoundsException
[24, 56, 12, 0, 0, 0]