programming:java
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| programming:java [2013/08/20 06:07] – dot | programming:java [2015/06/17 05:20] (現在) – 外部編集 127.0.0.1 | ||
|---|---|---|---|
| 行 64: | 行 64: | ||
| } | } | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | ===== プロパティファイルの読み込み ===== | ||
| + | |||
| + | <file java PropertyFile.java> | ||
| + | import java.util.Properties; | ||
| + | import java.io.FileInputStream; | ||
| + | |||
| + | public class PropertyFile{ | ||
| + | public static void main(String args[]){ | ||
| + | try { | ||
| + | Properties prop = new Properties(); | ||
| + | prop.load(new FileInputStream(" | ||
| + | for (String propertyName : prop.stringPropertyNames()) { | ||
| + | System.out.println(propertyName); | ||
| + | System.out.println(prop.getProperty(propertyName)); | ||
| + | } | ||
| + | } catch (Exception e){ | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file ini application.ini> | ||
| + | hoge=fuga | ||
| + | hello=world | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== XPathでXMLの読み込み ===== | ||
| + | |||
| + | <file java UseXPath.java> | ||
| + | |||
| + | import javax.xml.xpath.XPath; | ||
| + | import javax.xml.xpath.XPathConstants; | ||
| + | import javax.xml.xpath.XPathExpressionException; | ||
| + | import javax.xml.xpath.XPathFactory; | ||
| + | |||
| + | import org.w3c.dom.Node; | ||
| + | import org.w3c.dom.NodeList; | ||
| + | import org.xml.sax.InputSource; | ||
| + | |||
| + | public class UseXPath{ | ||
| + | public static void main(String args[]){ | ||
| + | XPath xpath = XPathFactory.newInstance().newXPath(); | ||
| + | InputSource inputSource = new InputSource(" | ||
| + | String expression = "// | ||
| + | |||
| + | try { | ||
| + | NodeList nodes = (NodeList) xpath.evaluate(expression, | ||
| + | for (int i = 0; i < nodes.getLength(); | ||
| + | Node node = nodes.item(i); | ||
| + | String str = node.getTextContent(); | ||
| + | System.out.println(str); | ||
| + | } | ||
| + | } catch (XPathExpressionException e) { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <file xml test.xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | <unko id=" | ||
| + | <unko id=" | ||
| + | </ | ||
| </ | </ | ||
| 行 102: | 行 171: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | |||
| - | ===== プロパティファイルの読み込み ===== | ||
| - | |||
| - | <file java PropertyFile.java> | ||
| - | import java.util.Properties; | ||
| - | import java.io.FileInputStream; | ||
| - | |||
| - | public class PropertyFile{ | ||
| - | public static void main(String args[]){ | ||
| - | try { | ||
| - | Properties prop = new Properties(); | ||
| - | prop.load(new FileInputStream(" | ||
| - | for (String propertyName : prop.stringPropertyNames()) { | ||
| - | System.out.println(propertyName); | ||
| - | System.out.println(prop.getProperty(propertyName)); | ||
| - | } | ||
| - | } catch (Exception e){ | ||
| - | e.printStackTrace(); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | |||
| - | <file ini application.ini> | ||
| - | hoge=fuga | ||
| - | hello=world | ||
| - | </ | ||
| - | |||
| ===== 拡張子指定ファイル一覧 ===== | ===== 拡張子指定ファイル一覧 ===== | ||
| 行 201: | 行 240: | ||
| </ | </ | ||
| - | ===== XPathでXMLの読み込み ===== | ||
| - | <file java UseXPath.java> | + | ===== 型変換 ===== |
| - | import javax.xml.xpath.XPath; | + | <file java TypeCast.java> |
| - | import javax.xml.xpath.XPathConstants; | + | public class TypeCast |
| - | import javax.xml.xpath.XPathExpressionException; | + | |
| - | import javax.xml.xpath.XPathFactory; | + | |
| - | + | ||
| - | import org.w3c.dom.Node; | + | |
| - | import org.w3c.dom.NodeList; | + | |
| - | import org.xml.sax.InputSource; | + | |
| - | + | ||
| - | public class UseXPath{ | + | |
| public static void main(String args[]){ | public static void main(String args[]){ | ||
| - | | + | |
| - | InputSource inputSource = new InputSource(" | + | String |
| - | String | + | |
| - | | + | |
| - | NodeList nodes = (NodeList) xpath.evaluate(expression, | + | String |
| - | for (int i = 0; i < nodes.getLength(); | + | |
| - | Node node = nodes.item(i); | + | // String -> int |
| - | | + | |
| - | | + | |
| - | } | + | |
| - | | + | |
| - | e.printStackTrace(); | + | |
| - | } | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | <file xml test.xml> | ||
| - | <?xml version=" | ||
| - | < | ||
| - | <unko id=" | ||
| - | <unko id=" | ||
| - | </ | ||
| - | </ | ||
programming/java.1376978864.txt.gz · 最終更新: (外部編集)
