1A,1B,1C 2A,2B,2C 3A,3B,3C 4A,4B,4C 5A,5B,5C
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(); } }
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 version="1.0" encoding="UTF-8"?> <root> <ヘッダー> <要素>ヘッダーの要素</要素> </ヘッダー> <トレイラー> <要素>トレイラーの要素</要素> </トレイラー> </root>
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()); } }
ヘッダーの要素 トレイラーの要素 -------------------------------------------------------------------------------- ヘッダーの要素 -------------------------------------------------------------------------------- トレイラーの要素
<?xml version="1.0" encoding="UTF-8"?> <ルートノード> <階層一> <階層二> <階層三> <階層四> <階層五>階層五の内容</階層五> </階層四> </階層三> </階層二> </階層一> </ルートノード>
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(); } }
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 version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment></comment> <entry key="key1">キー1</entry> <entry key="key2">キー2</entry> <entry key="key3">キー3</entry> <entry key="key4">キー4</entry> </properties>
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")); } }
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; }
印刷ジョブのプロパティを変更するためのダイアログにページの印刷方向に初期値を設定する方法を以下に示します。 ダイアログで指定した印刷方向でページが印刷されます。
import java.awt.*; import java.awt.print.*; public class DefaultOrientation implements Printable { private static Font fnt = new Font("Helvetica",Font.PLAIN,24); public static void main(String[] args) { PrinterJob job = PrinterJob.getPrinterJob(); PageFormat defaultPage = job.defaultPage(); defaultPage.setOrientation(PageFormat.LANDSCAPE); job.setPrintable(new DefaultOrientation(), defaultPage); // Point-1 if (job.printDialog()) { try { job.print(); } catch (PrinterException e) { e.printStackTrace(); } } } public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { if (5 <= pageIndex) return Printable.NO_SUCH_PAGE; g.setFont(fnt); g.setColor(Color.green); g.drawString("Page " + (pageIndex+1), 100, 100); return Printable.PAGE_EXISTS; } }