{"id":5719,"date":"2012-10-18T23:28:51","date_gmt":"2012-10-18T14:28:51","guid":{"rendered":"http:\/\/arison.jp\/blog\/?p=5719"},"modified":"2017-02-25T22:27:20","modified_gmt":"2017-02-25T13:27:20","slug":"java-file-class-is-immutable","status":"publish","type":"post","link":"https:\/\/arison.jp\/blog\/2012\/10\/18\/java-file-class-is-immutable\/","title":{"rendered":"\u3010Java\u3011File\u30af\u30e9\u30b9\u306f\u4e0d\u5909\uff08immutable\uff09\u30af\u30e9\u30b9"},"content":{"rendered":"<p>File\u30af\u30e9\u30b9\u306f\u4e0d\u5909(immutable)\u306a\u30af\u30e9\u30b9\u3067\u3042\u308a\u3001\u4e00\u5ea6\u751f\u6210\u3057\u305f\u3089\u72b6\u614b\u304c\u6c7a\u3057\u3066\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u3044\u8a2d\u8a08\u3002<br \/>\n\u300cFile\u30af\u30e9\u30b9\u304cJDK1.0\u306e\u6642\u4ee3\u304b\u3089\u3042\u308b\u53e4\u3044\u30af\u30e9\u30b9\u3060\u304b\u3089\u304b\u300d\u3068\u30ea\u30f3\u30af\u5148\u306b\u306f\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>Java\u306eFile\u30af\u30e9\u30b9\u306f\u4e0d\u5909\uff08immutable\uff09\u30af\u30e9\u30b9\u3068\u3044\u3046\u70b9\u306b\u95a2\u3059\u308b\u6ce8\u610f\u70b9 &#8211; \u9054\u4eba\u30d7\u30ed\u30b0\u30e9\u30de\u30fc\u3092\u76ee\u6307\u3057\u3066<br \/>\n<a href=\"http:\/\/d.hatena.ne.jp\/ryoasai\/20110711\/1310387741\" target=\"_blank\">http:\/\/d.hatena.ne.jp\/ryoasai\/20110711\/1310387741<\/a><\/p>\n<p><!--more--><\/p>\n<p>[java]<br \/>\npackage jp.arison;<\/p>\n<p>import java.io.File;<br \/>\nimport java.io.IOException;<\/p>\n<p>public class FileTest {<\/p>\n<p>    \/**<br \/>\n     * File\u30af\u30e9\u30b9\u306f\u4e0d\u5909(immutable)\u306a\u30af\u30e9\u30b9\u3063\u3066\u3053\u3068\u3092\u78ba\u8a8d\u3059\u308b<br \/>\n     *<br \/>\n     * @throws IOException \u4f8b\u5916<br \/>\n     *\/<br \/>\n    public void renameTest() throws IOException {<br \/>\n        File srcFile = new File(&quot;temp.txt&quot;);<br \/>\n        File targetFile = new File(&quot;temp2.txt&quot;);<\/p>\n<p>        if (srcFile.createNewFile()) {<br \/>\n            System.out.println(srcFile.getName() + &quot;\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u306b\u4f5c\u6210\u3055\u308c\u3066\u308b)&quot;);<br \/>\n        }<\/p>\n<p>        if (srcFile.renameTo(targetFile)) {<br \/>\n            \/\/ File\u30af\u30e9\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u305f\u30d5\u30a1\u30a4\u30eb\u306e\u540d\u524d\u304c\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u5909\u66f4\u3055\u308c\u307e\u3059\u3002<br \/>\n            System.out.println(targetFile.getName() + &quot;\u30ea\u30cd\u30fc\u30e0\u3057\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u5909\u66f4\u3055\u308c\u3066\u308b)&quot;);<br \/>\n        }<br \/>\n        System.out.println(&quot;&quot;);<br \/>\n        System.out.println(&quot;\u3053\u3053\u3067File\u30af\u30e9\u30b9\u306egetName()\u3092\u78ba\u8a8d&quot;);<br \/>\n        System.out.println(&quot;srcFile.getName() :&quot; + srcFile.getName());<br \/>\n        System.out.println(&quot;targetFile.getName() :&quot; + targetFile.getName());<\/p>\n<p>        System.out.println(&quot;&quot;);<br \/>\n        System.out.println(&quot;srcFile.delete() \u3067\u524a\u9664\u3057\u3066\u307f\u308b&quot;);<br \/>\n        if (srcFile.delete()) {<br \/>\n            System.out.println(srcFile.getName() + &quot;\u6d88\u305b\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u524a\u9664\u3055\u308c\u3066\u308b)&quot;);<br \/>\n        } else {<br \/>\n            System.out.println(srcFile.getName() + &quot;\u6d88\u305b\u307e\u305b\u3093\u3002&quot;);<br \/>\n        }<\/p>\n<p>        System.out.println(&quot;&quot;);<br \/>\n        System.out.println(&quot;targetFile.delete() \u3067\u524a\u9664\u3057\u3066\u307f\u308b&quot;);<br \/>\n        if (targetFile.delete()) {<br \/>\n            System.out.println(targetFile.getName() + &quot;\u6d88\u305b\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u524a\u9664\u3055\u308c\u3066\u308b)&quot;);<br \/>\n        } else {<br \/>\n            System.out.println(targetFile.getName() + &quot;\u6d88\u305b\u307e\u305b\u3093\u3002&quot;);<br \/>\n        }<br \/>\n        System.out.println(&quot;&quot;);<br \/>\n        System.out.println(&quot;\u306a\u308b\u307b\u3069\u3001renameTo()\u3057\u3066\u3082\u3001 getName()\u306e\u5024\u306f\u5909\u308f\u3089\u306a\u3044&quot;);<br \/>\n        System.out.println(&quot;File\u30af\u30e9\u30b9\u306f\u4e0d\u5909(immutable)\u306a\u30af\u30e9\u30b9\u3063\u3066\u3053\u3068\u3067&quot;);<br \/>\n        System.out.println(&quot;\u4e00\u5ea6\u751f\u6210\u3057\u305f\u3089\u72b6\u614b\u304c\u6c7a\u3057\u3066\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u3044\u8a2d\u8a08\u3063\u3066\u308f\u3051\u304b&quot;);<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u3010\u5b9f\u884c\u7d50\u679c\u3011<\/p>\n<p>temp.txt\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u306b\u4f5c\u6210\u3055\u308c\u3066\u308b)<br \/>\ntemp2.txt\u30ea\u30cd\u30fc\u30e0\u3057\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u5909\u66f4\u3055\u308c\u3066\u308b)<\/p>\n<p>\u3053\u3053\u3067File\u30af\u30e9\u30b9\u306egetName()\u3092\u78ba\u8a8d<br \/>\nsrcFile.getName() :temp.txt<br \/>\ntargetFile.getName() :temp2.txt<\/p>\n<p>srcFile.delete() \u3067\u524a\u9664\u3057\u3066\u307f\u308b<br \/>\ntemp.txt\u6d88\u305b\u307e\u305b\u3093\u3002<\/p>\n<p>targetFile.delete() \u3067\u524a\u9664\u3057\u3066\u307f\u308b<br \/>\ntemp2.txt\u6d88\u305b\u307e\u3057\u305f\u3002(\u30d5\u30a1\u30a4\u30eb\u30b7\u30b9\u30c6\u30e0\u4e0a\u3067\u524a\u9664\u3055\u308c\u3066\u308b)<\/p>\n<p>\u306a\u308b\u307b\u3069\u3001renameTo()\u3057\u3066\u3082\u3001 getName()\u306e\u5024\u306f\u5909\u308f\u3089\u306a\u3044<br \/>\nFile\u30af\u30e9\u30b9\u306f\u4e0d\u5909(immutable)\u306a\u30af\u30e9\u30b9\u3063\u3066\u3053\u3068\u3067<br \/>\n\u4e00\u5ea6\u751f\u6210\u3057\u305f\u3089\u72b6\u614b\u304c\u6c7a\u3057\u3066\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u3044\u8a2d\u8a08\u3063\u3066\u308f\u3051\u304b<\/p>\n<hr \/>\n<p>Java6\u307e\u3067\u306e\u74b0\u5883\u3067\u306f\u5fc5\u8981\u306b\u5fdc\u3058\u3066commons-io\u306a\u3069\u306e\u30e9\u30a4\u30d6\u30e9\u30ea<br \/>\n\u3000<a href=\"http:\/\/commons.apache.org\/io\/\" title=\"http:\/\/commons.apache.org\/io\/\">http:\/\/commons.apache.org\/io\/<\/a><br \/>\nJava7\u304b\u3089\u306f<br \/>\n\u3000<a href=\"http:\/\/d.hatena.ne.jp\/nowokay\/20110707\" title=\"http:\/\/d.hatena.ne.jp\/nowokay\/20110707\">http:\/\/d.hatena.ne.jp\/nowokay\/20110707<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>File\u30af\u30e9\u30b9\u306f\u4e0d\u5909(immutable)\u306a\u30af\u30e9\u30b9\u3067\u3042\u308a\u3001\u4e00\u5ea6\u751f\u6210\u3057\u305f\u3089\u72b6\u614b\u304c\u6c7a\u3057\u3066\u5909\u66f4\u3055\u308c\u308b\u3053\u3068\u304c\u306a\u3044\u8a2d\u8a08\u3002 \u300cFile\u30af\u30e9\u30b9\u304cJDK1.0\u306e\u6642\u4ee3\u304b\u3089\u3042\u308b\u53e4\u3044\u30af\u30e9\u30b9\u3060\u304b\u3089\u304b\u300d\u3068\u30ea\u30f3\u30af\u5148\u306b\u306f\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002 Java\u306eF [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":17178,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,9,4],"tags":[703],"class_list":["post-5719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-work","category-diary","tag-java"],"_links":{"self":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/5719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/comments?post=5719"}],"version-history":[{"count":0,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/posts\/5719\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media\/17178"}],"wp:attachment":[{"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/media?parent=5719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/categories?post=5719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arison.jp\/blog\/wp-json\/wp\/v2\/tags?post=5719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}