programming:java
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
programming:java [2013/07/28 03:35] – dot | programming:java [2015/06/17 05:20] (現在) – 外部編集 127.0.0.1 | ||
---|---|---|---|
行 1: | 行 1: | ||
====== Java ====== | ====== Java ====== | ||
- | ===== 環境変数 ===== | + | ===== 環境変数の参照 |
<file java Getenv.java> | <file java Getenv.java> | ||
行 27: | 行 27: | ||
</ | </ | ||
- | ===== プロパティファイル ===== | + | ===== ファイル入出力 ===== |
+ | |||
+ | <file java FileInputOutput.java> | ||
+ | import java.io.FileInputStream; | ||
+ | import java.io.InputStreamReader; | ||
+ | import java.io.BufferedReader; | ||
+ | |||
+ | import java.io.FileOutputStream; | ||
+ | import java.io.OutputStreamWriter; | ||
+ | import java.io.BufferedWriter; | ||
+ | import java.io.PrintWriter; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | |||
+ | public class FileInputOutput { | ||
+ | public static void main(String[] args) { | ||
+ | try { | ||
+ | String input = " | ||
+ | String inputEncoding = " | ||
+ | |||
+ | String output = " | ||
+ | String outputEncoding = " | ||
+ | |||
+ | BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(input), | ||
+ | PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output), | ||
+ | |||
+ | String line = ""; | ||
+ | while ((line = br.readLine()) != null) { | ||
+ | pw.println(line); | ||
+ | } | ||
+ | br.close(); | ||
+ | pw.close(); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== プロパティファイルの読み込み | ||
<file java PropertyFile.java> | <file java PropertyFile.java> | ||
+ | import java.util.Properties; | ||
+ | import java.io.FileInputStream; | ||
+ | |||
public class PropertyFile{ | public class PropertyFile{ | ||
public static void main(String args[]){ | public static void main(String args[]){ | ||
try { | try { | ||
- | | + | Properties prop = new Properties(); |
- | prop.load(new | + | prop.load(new FileInputStream(" |
for (String propertyName : prop.stringPropertyNames()) { | for (String propertyName : prop.stringPropertyNames()) { | ||
System.out.println(propertyName); | System.out.println(propertyName); | ||
行 46: | 行 88: | ||
</ | </ | ||
- | + | < | |
- | < | + | |
hoge=fuga | hoge=fuga | ||
hello=world | 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=" | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ===== 正規表現を用いたパターンマッチ ===== | ||
+ | |||
+ | <file java RegexPatternMatch.java> | ||
+ | import java.util.regex.Matcher; | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegexPatternMatch{ | ||
+ | public static void main(String[] args) { | ||
+ | String str = " | ||
+ | |||
+ | Matcher matcher = Pattern.compile(" | ||
+ | if (matcher.find()){ | ||
+ | System.out.println(matcher.group(1)); | ||
+ | System.out.println(matcher.group(2)); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 正規表現を用いた文字列置換 ===== | ||
+ | |||
+ | <file java RegexReplaceString.java> | ||
+ | import java.util.regex.Pattern; | ||
+ | |||
+ | public class RegexReplaceString{ | ||
+ | public static void main(String[] args) { | ||
+ | String str = " | ||
+ | |||
+ | // unko -> World | ||
+ | System.out.println(Pattern.compile(" | ||
+ | |||
+ | // unko -> World | ||
+ | System.out.println(str.replaceAll(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 拡張子指定ファイル一覧 ===== | ||
+ | |||
+ | <file java ListFiles.java> | ||
+ | import java.io.File; | ||
+ | import java.io.FilenameFilter; | ||
+ | |||
+ | public class ListFiles{ | ||
+ | public static void main(String args[]){ | ||
+ | File directory = new File(args[0]); | ||
+ | for(File file : directory.listFiles(getRegexFilter(" | ||
+ | System.out.println(file.getAbsolutePath()); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static FilenameFilter getRegexFilter(String regex) { | ||
+ | final String regex_ = regex; | ||
+ | return new FilenameFilter() { | ||
+ | public boolean accept(File dir, String name) { | ||
+ | return name.matches(regex_); | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== シャローコピー(参照のコピー)による配列長の拡張 ===== | ||
+ | |||
+ | <file java ArrayCopy.java> | ||
+ | public class ArrayCopy{ | ||
+ | public static void main(String args[]){ | ||
+ | String[] array = new String[3]; | ||
+ | array[0] = " | ||
+ | array[1] = " | ||
+ | array[2] = " | ||
+ | |||
+ | String[] new_array = new String[array.length+1]; | ||
+ | | ||
+ | // | ||
+ | System.arraycopy(array, | ||
+ | | ||
+ | // | ||
+ | new_array[array.length] = " | ||
+ | |||
+ | // | ||
+ | //0 | ||
+ | //1 | ||
+ | //2 | ||
+ | //3 | ||
+ | for(String str : new_array){ | ||
+ | System.out.println(str); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 出力改行コードの指定 ===== | ||
+ | |||
+ | <file java NewLineCode.java> | ||
+ | public class NewLineCode{ | ||
+ | public static void main(String args[]){ | ||
+ | // | ||
+ | //Windows : " | ||
+ | System.setProperty(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 型変換 ===== | ||
+ | |||
+ | <file java TypeCast.java> | ||
+ | public class TypeCast { | ||
+ | public static void main(String args[]){ | ||
+ | int intType = 10; | ||
+ | String stringType = " | ||
+ | |||
+ | // int -> String | ||
+ | String a = Integer.toString(intType); | ||
+ | |||
+ | // String -> int | ||
+ | int b = Integer.parseInt(stringType); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ |
programming/java.1374982505.txt.gz · 最終更新: 2015/06/17 05:09 (外部編集)