ååã®è¨äºï¼Javaã®ãã¼ã¸ã§ã³å¥ã1è¡ãã¤ãã¡ã¤ã«ãèªãæ¹æ³ã¾ã¨ãï¼ã¸ã®ææ³ã§ããã®ãããªè©±ãããã¾ããã
丸ã èªã¿è¾¼ãæ¹æ³ã®ã¾ã¨ãããé¡ããã¾ãï¼
— ã¦ã¼ã¹ã± (å±±æ¬è£ä») (@yusuke) May 3, 2021
ã¨ããããã§ãä»åã¯ããã¹ããã¡ã¤ã«ãä¸æ¬ã§èªã¿è¾¼ãæ¹æ³ãã¾ã¨ãã¾ããã
ï¼ååã¨è¢«ã£ã¦ããç¹ã¯çç¥ãã¦ããã®ã§ãã¾ã èªãã§ãªã人ã¯å
ã«ååã®è¨äºãã©ããï¼
Java 1.1, 1.2, 1.3
private static String readString(File file) throws IOException { Reader reader = null; try { reader = new InputStreamReader(new FileInputStream(file), "UTF-8"); StringBuffer sb = new StringBuffer(); int len; char[] buffer = new char[1024 * 8]; while ((len = reader.read(buffer)) != -1) { sb.append(buffer, 0, len); } return sb.toString(); } finally { if (reader != null) { reader.close(); } } }
ã¾ã ãã®ããã¯ããã©ãããã§ãã
FileInputStream
ã§ãã¡ã¤ã«ãèªã¿è¾¼ã¿ãInputStreamReader
ã§æå®ããæåã³ã¼ãã«ãã³ã¼ãããããã«ãã¦ãã¾ãã
èªã¿è¾¼ãéã¯ãchar[]
ã§ãããã¡ã㦠StringBuffer
ã«è¿½å ãã¦ãã£ã¦ãã¾ãã
ã¡ãªã¿ã«ãchar[]
ã§ãããã¡ããªããèªã¿è¾¼ãã§ããã®ã§ã BufferedReader
ã§ã©ããããå¿
è¦ã¯ããã¾ããã
Java 1.4, 1.5, 6
private static String readString(File file) throws IOException { CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder(); try (FileInputStream stream = new FileInputStream(file)) { FileChannel channel = stream.getChannel(); ByteBuffer bb = channel.map(MapMode.READ_ONLY, 0, channel.size()); CharBuffer cb = decoder.decode(bb); return cb.toString(); } }
ã«ã¼ãããªããªã£ã¦ãã ãã¶å¦çããã£ãããã¾ããã
ãã®ãã¼ã¸ã§ã³ã§è¿½å ããã Charset
ã¯ã©ã¹ãããCharsetDecoder
ã¨ãããã³ã¼ããåå¾ã§ãã¾ãã
ããã« FileChannel#mapâ(FileChannel.MapMode mode, long position, long size)
ã§åå¾ãã MappedByteBuffer
ã渡ããã¨ã§ããã¡ã¤ã«ãèªã¿è¾¼ãã¾ãã
MappedByteBuffer
ã¯ãã¡ã¤ã«ã®å
容ãã¡ã¢ãªã«ãããã³ã°ãã¦æ±ãï¼ã¡ã¢ãªãããããã¡ã¤ã«ï¼ã®ã§ã大ããªãã¡ã¤ã«ã®æã«å¹ççã«å¦çãè¡ããã¾ãã
éã«è¨ãã¨ãå°ãããã¡ã¤ã«ãèªã¿è¾¼ãå ´åã¯æ§è½ãæªãã§ãã
ãã®å ´åã¯ãä»ã¾ã§éã InputStream
ã§èªã¿è¾¼ã¿ã¾ãããã
ã»ã¨ãã©ã®ãªãã¬ã¼ãã£ã³ã°ã»ã·ã¹ãã ã§ã¯ããã¡ã¤ã«ãã¡ã¢ãªã¼ã«ãããã³ã°ããã»ãããé常ã®readã¡ã½ããã¾ãã¯writeã¡ã½ããã使ã£ã¦æ°åãããã¤ãã®ãã¼ã¿ã®èªã¿è¾¼ã¿ã¾ãã¯æ¸è¾¼ã¿ãè¡ããããè² è·ã大ãããªãã¾ãã æ§è½ãéè¦ãããªããæ¯è¼ç大ããã®ãã¡ã¤ã«ã ãããããã³ã°ãããã¨ããè¦ããã¾ãã FileChannel (Java SE 15 & JDK 15))
ï¼ï¼ï¼ï¼
ã¡ãªã¿ã«ããã® FileChannel
ã® map
ã¡ã½ããã使ãããæ¹ã¯ã²ãã ã¾ããã®è¨äºãèªãã§ç¥ãã¾ããï¼
Javaãããã¡ã¼ã¯ã©ã¹ã¡ã¢(Hishidama's Java Buffer Memo)
Java 7, 8, 9, 10
private static String readString(Path path) throws IOException { byte[] bytes = Files.readAllBytes(path); return new String(bytes, StandardCharsets.UTF_8); }
Files
ã¯ã©ã¹ã追å ãããããã«ä¸æ¬ã§ãã¡ã¤ã«ã byte[]
ã¨ãã¦èªã¿è¾¼ãã¡ã½ãããç¨æããã¾ããã
ããã使ã£ã¦ãã¡ã¤ã«ãèªã¿è¾¼ãã°ããã¨ã¯ String
ã¯ã©ã¹ã new
ããã ãã§ãã
ã¡ãªã¿ã«ããã®ãã¼ã¸ã§ã³ã§ãã¡ã¤ã«ãããã¹ã¦ã®è¡ãèªã¿åã Files#readAllLinesâ(Path, Charset)
ã追å ããã¾ããã
ãã®æ»ãå¤ã¯ List<String>
ã§ããè¡ãã¨ã«å¦çããããå ´åã¯ãã¡ãã便å©ã§ãã
Java 11ï½
private static String readString(Path path) throws IOException { return Files.readString(path, StandardCharsets.UTF_8); }
ã²ã¨ã¤ã®ã¡ã½ããã§ãã¡ã¤ã«ããã¹ã¦èªã¿è¾¼ããããã«ãªãã¾ããã 1
ãã®ã¡ã½ããã®å®è£
ã§ã¯ StringCoding
ã¨ããã¤ã³ã¿ã¼ãã«ãªã¯ã©ã¹ã使ã£ã¦ããã®ã§ãnew String(bytes, StandardCharsets.UTF_8)
ã¨ãããããå¹ççãªå¦çã«ãªã£ã¦ãã¾ãã
ã¾ã¨ã
ããã©ãããã£ãå¦çããJava ã®ãã¼ã¸ã§ã³ãä¸ãããã¨ã«ç°¡åã«æ¸ããããã«ãªã£ã¦ãã¾ãã
ãªã®ã§ãç©æ¥µçã«æ°ãã Java ã使ã£ã¦ããã¾ãããï¼