SoftwareEngineering
CSV†
CSVを読み込む†
- data.csv
1A,1B,1C
2A,2B,2C
3A,3B,3C
4A,4B,4C
5A,5B,5C
- CSVReader.java
import java.io.*;
import java.util.StringTokenizer;
public class CSVReader {
public static void main(String args[]) throws Exception {
String fileName = "data.csv";
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String lineText = null;
while ((lineText = reader.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(lineText, ",");
while (tokenizer.hasMoreTokens()) {
System.out.print(tokenizer.nextToken());
if (tokenizer.hasMoreTokens()) System.out.print(":");
}
System.out.println();
}
reader.close();
}
}
CSVを書きこむ†
- CSVWriter.java
import java.io.*;
public class CSVWriter {
public static void main(String args[]) throws Exception {
String fileName = "data.csv";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write("1D,1E,1F");
writer.newLine();
writer.write("2D,2E,2F");
writer.newLine();
writer.write("3D,3E,3F");
writer.newLine();
writer.write("4D,4E,4F");
writer.newLine();
writer.write("5D,5E,5F");
writer.newLine();
writer.close();
}
}
XML†
XMLを読み込む†
- XMLReader.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XMLReader {
public static void main(String args[]) throws Exception {
String fileName = "data.xml";
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fileName);
for (int index = 0; index < xmlDoc.getElementsByTagName("要素").getLength(); index++) {
System.out.println(xmlDoc.getElementsByTagName("要素").item(index).getFirstChild().getNodeValue());
}
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("--------------------------------------------------------------------------------");
NodeList nodeList = (NodeList)xPath.evaluate("/root/ヘッダー/要素", xmlDoc, XPathConstants.NODESET);
System.out.println(nodeList.item(0).getFirstChild().getNodeValue());
System.out.println("--------------------------------------------------------------------------------");
nodeList = (NodeList)xPath.evaluate("/root/トレイラー/要素", xmlDoc, XPathConstants.NODESET);
System.out.println(nodeList.item(0).getFirstChild().getNodeValue());
}
}
絶対ロケーションパスを取得する†
- AbsoluteLocationPath.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class AbsoluteLocationPath {
public static void main(String args[]) throws Exception {
String fileName = "data.xml";
Document xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(fileName);
Node targetNode = xmlDoc.getElementsByTagName("階層五").item(0);
System.out.println(targetNode.getFirstChild().getNodeValue());
String absoluteLocationPath = getAbsoluteLocationPath(targetNode);
System.out.println(String.format("絶対ロケーションパス:[%s]", absoluteLocationPath));
System.out.println(targetNode.getFirstChild().getNodeValue());
}
private static String getAbsoluteLocationPath(Node node) {
StringBuffer path = new StringBuffer();
while (node.getParentNode() != null) {
path.insert(0, "/" + node.getNodeName());
node = node.getParentNode();
}
return path.toString();
}
}
ファイル名から拡張子を除いたベース名を取得する†
- FileNameWithoutExtension.java
public class FileNameWithoutExtension {
public static void main(String args[]) throws Exception {
System.out.printf("%s:[%s]\r\n", "sample.dat", getFileNameWithoutExtension("sample.dat"));
System.out.printf("%s:[%s]\r\n", ".dat", getFileNameWithoutExtension(".dat"));
System.out.printf("%s:[%s]\r\n", "sample_dat", getFileNameWithoutExtension("sample_dat"));
}
private static String getFileNameWithoutExtension(String fileName) {
int startExtensionIndex = fileName.lastIndexOf('.');
if (startExtensionIndex < 0) return fileName;
if (startExtensionIndex == 0) return "";
return fileName.substring(0, startExtensionIndex);
}
}
プロパティファイル†
XML版†
- XMLProperties.java
import java.util.Properties;
import java.io.*;
public class XMLProperties {
public static void main(String args[]) throws Exception {
String fileName = "XMLProperties.properties";
InputStream inputStream = new FileInputStream(fileName);
Properties configuration = new Properties();
configuration.loadFromXML(inputStream);
System.out.printf("%s:[%s]\r\n", "key1", configuration.getProperty("key1"));
System.out.printf("%s:[%s]\r\n", "key2", configuration.getProperty("key2"));
System.out.printf("%s:[%s]\r\n", "key3", configuration.getProperty("key3"));
System.out.printf("%s:[%s]\r\n", "key4", configuration.getProperty("key4"));
}
}
String配列†
歯抜けになっている中身を前詰する†
public class RemoveBlankItems{
public static void main(String args[]) throws Exception {
String items[] = new String[30];
for (int i = 0; i < items.length; i++) {
items[i] = "";
}
items[5] = "5番目のデータ";
items[10] = "10番目のデータ";
items[15] = "15番目のデータ";
items[20] = "20番目のデータ";
items[25] = "25番目のデータ";
System.out.println("---------------------------------------------------------------------------");
System.out.println("-- 未処理");
for (int i = 0; i < items.length; i++) {
System.out.println("items[" + Integer.toString(i) + "]:[" + items[i] + "]");
}
removeBlankItems(items);
System.out.println("---------------------------------------------------------------------------");
System.out.println("-- 処理後");
for (int i = 0; i < items.length; i++) {
System.out.println("items[" + Integer.toString(i) + "]:[" + items[i] + "]");
}
}
private static void removeBlankItems(String items[]) {
String buffer[] = new String[items.length];
// 配列の中身を退避
for (int index = 0; index < items.length; index++) {
buffer[index] = items[index];
}
// 前方に詰めたい配列の中身をクリア
for (int index = 0; index < items.length; index++) {
items[index] = "";
}
// 前方に詰める
int itemIndex = 0;
for (int bufferIndex = 0; bufferIndex < buffer.length; bufferIndex++) {
if (buffer[bufferIndex] == null) continue;
if (buffer[bufferIndex].equals("")) continue;
items[itemIndex] = buffer[bufferIndex];
itemIndex++;
}
}
}
画像を回転させる†
private Image rotate(Image sourceImage, int rotation) {
int sourceWidth = sourceImage.getWidth(this); // 幅(変換前の画像)
int sourceHeight = sourceImage.getHeight(this); // 高さ(変換後の画像)
double angle = 0; // 角度
int targetWidth = sourceWidth; // 幅(変換後の画像)
int targetHeight = sourceHeight; // 高さ(変換後の画像)
double posX = 0; // 中心点 X座標
double posY = 0; // 中心点 Y座標
switch (rotation) {
case 1: {
// 右に90度回転する
angle = Math.toRadians(90d);
posX = (sourceWidth / 2) - ((sourceWidth - sourceHeight) / 2);
posY = sourceHeight / 2;
targetWidth = sourceHeight;
targetHeight = sourceWidth;
break;
}
case 2: {
// 右に180度回転する
angle = Math.toRadians(180d);
posX = sourceWidth / 2;
posY = sourceHeight / 2;
targetWidth = sourceWidth;
targetHeight = sourceHeight;
break;
}
case 3: {
// 右に270度回転する
angle = Math.toRadians(270d);
posX = sourceWidth / 2;
posY = (sourceHeight / 2) + ((sourceWidth - sourceHeight) / 2);
targetWidth = sourceHeight;
targetHeight = sourceWidth;
break;
}
}
BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
java.awt.geom.AffineTransform affin
= java.awt.geom.AffineTransform.getRotateInstance(angle, posX, posY);
Graphics2D g2 = (Graphics2D)targetImage.createGraphics();
g2.drawImage(sourceImage, affin, null);
return targetImage;
}