ååã«ç¶ããä»å㯠GDK ã java.io.File ãªãã¸ã§ã¯ãã«è¿½å ããæååã»æåã¹ããªã¼ã é¢é£ã®ã¡ã½ãããè¦ã¦ããã¾ãï¼ç®æ¬¡ï¼ããååã®ãã¤ãé åã»ãã¤ãã¹ããªã¼ã é¢é£ã®ã¡ã½ãããããã使ããã¨æãã¾ãã
æåã¯ãã¼ã¿çã«è¦ãã°ããã¤ã + æåã»ããï¼ã¨ã³ã³ã¼ãã£ã³ã°ï¼ããªã®ã§ãååã®ãã¤ãé åã»ãã¤ãã¹ããªã¼ã é¢é£ã®ã¡ã½ããã¨æ¯ã¹ã¦æåã»ããï¼ã表ãæååï¼ãã¨ãã¡ã½ããå¼æ°ã追å ã§å®ç¾©ããã¦ãã¾ã*1ããã¨ã³ã³ã¼ãã£ã³ã°ãæå®ãã¦ããªãå ´åã¯ï¼ãã¶ãï¼å OS ã®ããã©ã«ãã»ã¨ã³ã³ã¼ãã£ã³ã°ã使ããã¾ãããæåã»ããã®æå®ã« java.nio.charset.Charset ãªãã¸ã§ã¯ãã¯ï¼ç´æ¥ã¯ï¼ä½¿ãã¾ããã
æåå
ã¾ãã¯æååãããªãã¡ String ãªãã¸ã§ã¯ãã使ã£ã¦ãã¡ã¤ã«å 容ãèªã¿æ¸ãããã¡ã½ãããè¦ã¦ããã¾ãããããåé¡ã¯ååã®ãã¤ãé åã®ã¨ãã¨åãã«ãã¦ãã¾ãï¼ã¡ã½ãã | è¿ãå¤ | since | |
---|---|---|---|
ä¸æ¬èªã¿è¾¼ã¿ | getText() getText(String cs) |
String | 1.0 |
å復èªã¿è¾¼ã¿ | 次åã®è¡å¦çåç § | ||
å¤ã®ä¸æ¸ã | write(String text) write(String text, String cs) setText(String text) setText(String text, String cs) |
void | 1.0 1.0 1.5.1 1.7.3 |
å¤ã®è¿½å æ¸ã込㿠| append(Object text) append(Object text, String cs) leftShift(Object text) |
void void File |
1.0 |
æåã»ãããæå®ããå¼æ°ã®è²ãå¤ãã¦ããã®ã¯ãåãªãè¦ãããã®ããã§ããããã¤ãé åã®å ´åã®å復èªã¿è¾¼ã¿ã«å¯¾å¿ãããæåã1æåãã¤èªã¿è¾¼ãã§å¦çããã¡ã½ããã¯ããã¾ãããã代ããã« BufferedReader ã®ããã«1è¡ãã¤èªã¿è¾¼ãã§å¦çããã¡ã½ãããããã¾ããããã ããããã§ã¯é·ããªãã®ã§æ¬¡åãè¡å¦çãã«æã¡è¶ãã
ãã®ä»ã®æ³¨æç¹ã¯
- setText() ã¡ã½ãã㯠write() ã¡ã½ããã¨åã
- leftShift() ã¯ãããã append() ã®å¦çãè¡ã£ã¦ File ãªãã¸ã§ã¯ããè¿ãã®ã¨åãã§ãï¼å®è£ ã¯ç¥ãã¾ãããï¼ãããã ã leftShift() ã¯é常 << æ¼ç®åã¨ãã¦ä½¿ãã®ã§ãæåã»ãããæå®ããã¡ã½ããã¯å®ç¾©ããã¦ãã¾ãã
- append(), leftShift() ã¯å 容ãæå®ããå¼æ°ã®åã String åã§ã¯ãªã Object åã¨ãªã£ã¦ãã¾ãããå 容ã¨ãªãæååã¯ãå¤ãã®ãªãã¸ã§ã¯ãã«å¯¾ãã¦ã¯åã«ãã®ãªãã¸ã§ã¯ãã® toString() ãå¼ã³åºããè¿ãå¤ã«ãªãã¾ãããReader ãªãã¸ã§ã¯ãããã¨ã§è¦ã groovy.lang.Writable ãªãã¸ã§ã¯ãã®å ´åã¯é©åãªå 容ãæ¸ãåºãã¦ããã¾ãï¼ã¡ãªã¿ã« ObjectOutputStream ã¨ãã¯é¢ä¿ããã¾ããï¼ã
ã§ã¯ãµã³ãã«ã³ã¼ãã
// *****æºå***** // å 容ããGroovyãã®ãã¡ã¤ã«ãä½è£½ def src = new File('source.txt') src.text = 'Groovy' def srcUTF8 = new File('src-utf8.txt') srcUTF8.setText('åãã³', 'UTF-8') // ä¸æ¬èªã¿è¾¼ã¿ getText() assert src.text == 'Groovy' assert srcUTF8.getText('UTF-8') == 'åãã³' // å¤ã®ä¸æ¸ã setText() src.text = 'Grooovy' assert src.text == 'Grooovy' srcUTF8.setText('åãã³', 'UTF-8') assert srcUTF8.getText('UTF-8') == 'åãã³' // å¤ã®è¿½å æ¸ã込㿠append(), leftShift(), << src.text = 'Gr' src.append('oo') src << 'v' << 'y' assert src.text == 'Groovy' srcUTF8.setText('å', 'UTF-8') srcUTF8.append('ãã³', 'UTF-8') assert srcUTF8.getText('UTF-8') == 'åãã³'
ã¾ãããã¤ãé åã®å ´åã¨åããã¨ããããã£ã¨åããããããã¨æãã¾ãããæåã»ãããæå®ããªã getText() / setText() 㯠text ããããã£ã¸ã®ã¢ã¯ã»ã¹ã®ããã«æ¸ããã®ã§ä¾¿å©ã§ãããæ¥æ¬èªãªã©ã使ãå ´åã¯æåã»ãããæå®ãã¦ããæ¹ãç¡é£ã§ãããã
æåã¹ããªã¼ã
次ã¯æåã¹ããªã¼ã ãã¤ã¾ã Reader / Writer é¢é£ã®ã¡ã½ãããã¡ã½ãã | è¿ãå¤ | since | |
---|---|---|---|
å
¥åºåãªãã¸ã§ã¯ã ã®åå¾ |
newReader() newReader(String cs) newWriter() newWriter(boolean append) newWriter(String cs) newWriter(String cs, boolean append) newPrintWriter() newPrintWriter(String cs) |
BufferedReader BufferedReader BufferedWriter BufferedWriter BufferedWriter BufferedWriter PrintWriter PrintWriter |
1.0 |
èªã¿è¾¼ã¿ | withReader(Closure c) withReader(String cs, Closure c) |
Object | 1.5.2 1.6.0 |
æ¸ãåºã | withWriter(Closure c) withWriter(String cs, Closure c) withWriterAppend(Closure c) withWriterAppend(String cs, Closure c) |
Object | 1.5.2 |
ããªã³ãåºå | withPrintWriter(Closure c) withPrintWriter(String cs, Closure c) |
Object | 1.5.2 |
- withWriter() 㨠withWriterAppend() ã®éãã¯ãä¸æ¸ãããã追å æ¸ãè¾¼ã¿ããã§ãã
- ããªã³ãåºå withPrintWriter() ã¯æ¸ãåºã withWriter ã¨åããããªãã®ã§ãããå¼æ°ã® Closure ã«æ¸¡ãããã®ã (Buffered)Writer ã§ã¯ãªã PrintWriter ã§ãããã¨ãéãã¾ãããWriter 㨠PrintWriter ã®éãã¯ã»ã»ã» JavaDoc ã¨ãè¦ã¦ãã ãã*2ã
withXxxx() ã¡ã½ããã§æåã»ãããæå®ããå ´åã
src.withReader('UTF-8', { println it.readLine() })
ã®ããã«ãã¦ãåä½ãã¾ãããé常ã¯
src.withReader('UTF-8'){ println it.readLine() }
ã®ããã«ããæ¹ãè¦æãã§ããã以ä¸ãè¸ã¾ãã¦ãµã³ãã«ã³ã¼ãï¼
// *****æºå***** // æç« ã¯ Groovy ã® HP (http://groovy.codehaus.org/) ãã def src = new File('source.txt') src.text = '''\ Groovy is an agile and dynamic language for the Java Virtual Machine Groovy builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk''' def srcUTF8 = new File('utf8.txt') srcUTF8.setText('''\ Groovy makes modern programming features available to Java developers with almost-zero learning curve Groovy provides the ability to statically type check and statically compile your code for robustness and performance''', 'UTF-8') // withReader() src.withReader{ reader -> assert reader instanceof LineNumberReader assert reader.readLine() ==~ /Groovy is an agile and dynamic.*/ // ãGroovy is ...ã ã§å§ã¾ã assert reader.readLine() ==~ /Groovy builds upon the strengt.*/ } srcUTF8.withReader('UTF-8'){ reader -> assert reader instanceof BufferedReader assert reader.readLine() ==~ /Groovy makes modern programmin.*/ assert reader.readLine() ==~ /Groovy provides the ability to.*/ } // withWriter() src.withWriter{ writer -> assert writer instanceof BufferedWriter writer.write 'Groovy supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain' } assert src.text ==~ /Groovy supports Domain-Specifi.*/ srcUTF8.withWriter('UTF-8'){ writer -> assert writer instanceof BufferedWriter writer.write 'Groovy makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL' } assert srcUTF8.getText('UTF-8') ==~ /Groovy makes writing shell and.*/ // withWriterAppend() src.withWriterAppend{ writer -> assert writer instanceof BufferedWriter writer.write 'Groovy increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications' } assert src.text ==~ /Groovy supports Doma.*console applications/ srcUTF8.withWriterAppend('UTF-8'){ writer -> assert writer instanceof BufferedWriter writer.write 'Groovy makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL' } assert srcUTF8.getText('UTF-8') ==~ /Groovy makes writing.*ities and an Ant DSL/ // withPrintWriter() src.withPrintWriter{ writer -> assert writer instanceof PrintWriter writer.printf '%s simplifies testing by supporting unit testing and mocking out-of-the-box', 'Groovy' } assert src.text ==~ /Groovy simplifies testing by s.*/ srcUTF8.withPrintWriter('UTF-8'){ writer -> assert writer instanceof PrintWriter writer.printf '%s seamlessly integrates with all existing Java classes and libraries', 'Groovy' } assert srcUTF8.getText('UTF-8') ==~ /Groovy seamlessly integrates w.*/ // ãã¡ã¤ã«å 容ã®ã³ãã¼ def src = new File('source.txt') src.text = '''\ Groovy... is an agile and dynamic language for the Java Virtual Machine builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk makes modern programming features available to Java developers with almost-zero learning curve provides the ability to statically type check and statically compile your code for robustness and performance supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications simplifies testing by supporting unit testing and mocking out-of-the-box seamlessly integrates with all existing Java classes and libraries compiles straight to Java bytecode so you can use it anywhere you can use Java''' def dest = new File('dest.txt') src.withReader{ reader -> dest.withPrintWriter{ writer -> for(line in reader) writer.println(line) } }
ã¡ãã£ã¨åããã¥ãããµã³ãã«ã ã£ãããªã
ãªãã¸ã§ã¯ãå¤æã¨Writable ã¤ã³ã¿ã¼ãã§ã¼ã¹
GDK 㯠File ã¯ã©ã¹ã«å¯¾ã㦠groovy.lang.Writable ãªãã¸ã§ã¯ãã«å¤æããã¡ã½ããã追å ãã¦ãã¾ãããWritable ã¯ã©ã¹ã¯ writeTo() ã¡ã½ãããæã¡ãèªèº«ã®å 容ã«å¯¾å¿ããé©åãªå 容ãå¼æ°ã® Writer ãªãã¸ã§ã¯ãã«æ¸ãåºãã¾ãï¼package groovy.lang public interface Writable{ java.io.Writer writeTo(java.io.Writer out) }
File ã¯ã©ã¹ã«è¿½å ããã¦ããããªãã¸ã§ã¯ãå¤æã«é¢ããã¡ã½ããã¯ä»¥ä¸ã®éãï¼
ã¡ã½ãã | è¿ãå¤ | since | |
---|---|---|---|
asWritable() asWritable(String encoding) |
File | 1.0 | |
asType(Class c) | Object | 1.0 |
asType(Class) ã¯å¼æ°ã® Class ãªãã¸ã§ã¯ãã Writable.class ãªã asWritable() ã§è¿ããã Writable ãªãã¸ã§ã¯ããè¿ãã¾ã*3ã
ãã¾ãæ示çã«ä½¿ããã®ãªã®ãã©ããç¥ãã¾ãããã使ãå ´å㯠<< æ¼ç®åï¼ããã㯠append(), leftShift()ï¼ã¨ä¸ç·ã«ä½¿ãã®ãããã§ãããï¼
def src = new File('source.txt') src.text = '...' def dest = new File('dest.txt') dest << src.asWritable() // ãsource.txtãã®å 容ãæ¸ãåºããã //dest << (src as Writable) // ä¸ã«åã //dest << src // ãsource.txtããæ¸ãè¾¼ã¾ãããï¼
次åã¯æååã»æåã¹ããªã¼ã é¢é£ã§ä»åæ±ããªãã£ãè¡å¦çã«é¢é£ããã¡ã½ãããè¦ã¦ããäºå®ã
- ä½è : é¢è°·åæ,ä¸å潤äº,é æ±ä¿¡æ´,ä¸ééæ²»
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 2011/07/06
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 6人 ã¯ãªãã¯: 392å
- ãã®ååãå«ãããã° (155件) ãè¦ã
Groovyã¤ã³ã»ã¢ã¯ã·ã§ã³
- ä½è : Dierk Konig,Andrew Glover,Paul King,Guillaume Laforge,Jon Skeet,æ浦å,æ«»äºæ£æ¨¹,é æ±ä¿¡æ´,é¢è°·åæ,ä½éå¾¹é,寺沢å°å²
- åºç社/ã¡ã¼ã«ã¼: æ¯æ¥ã³ãã¥ãã±ã¼ã·ã§ã³ãº
- çºå£²æ¥: 2008/09/27
- ã¡ãã£ã¢: åè¡æ¬ï¼ã½ããã«ãã¼ï¼
- è³¼å ¥: 5人 ã¯ãªãã¯: 146å
- ãã®ååãå«ãããã° (121件) ãè¦ã
*1:ã¡ã½ããã®ããã©ã«ãå¤ä½¿ã£ã¦å®ç¾©ã§ããªãã£ããããªï¼
*2:使ãåã«ã¯ print(), println(), printf(), format() ãªã©ã®ã¡ã½ããã使ãããã¨ããã®ã大ããªéãã§ããããããä»ã«ã¯èªåãã©ãã·ã¥ã®ã¿ã¤ãã³ã°ãä¾å¤ãæããããªããªã©ã®éããããããã§ã
*3:file.asType(Writable) 㯠as ãã¼ã¯ã¼ãã使ã£ã¦ãsimeFile as Writableãã®ããã«æ¸ãã¦ãå¼ã³åºããã¾ãã