1. File Read line by line
1) Read Line with BufferedReader (JDK1.1) Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileReadLineBr { public static void main(String[] args) { String fileName = "d:/source.txt"; try { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
2) Read Line with Scanner (JDK1.5) Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileReadLineScanner { public static void main(String[] args) { String fileName = "d:/source.txt"; try { Scanner scanner = new Scanner(new File(fileName)); while (scanner.hasNext()) { System.out.println(scanner.nextLine()); } } catch (IOException e) { e.printStackTrace(); } } } |
3) Read Line with readAllLines (JDK1.7) Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class FileReadLineAllBytes { public static void main(String[] args) { String fileName = "d:/source.txt"; try { List<String> lines = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset()); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
4) Read Line with BufferedReader + Stream (JDK1.8) Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; public class FileReadLineStreamBr { public static void main(String[] args) { String fileName = "d:/source.txt"; try { BufferedReader br = Files.newBufferedReader(Paths.get(fileName)); List<String> list = br.lines().collect(Collectors.toList()); list.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
5) Read Line with Stream (JDK1.8) Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class FileReadLineStream { public static void main(String[] args) { String fileName = "d:/source.txt"; try { Stream<String> stream = Files.lines(Paths.get(fileName)); List<String> list = stream .filter(line -> !line.contains("public")) .map(String::toUpperCase) .collect(Collectors.toList()); list.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } |
2. File Read to String
1) Read to String with readAllBytes Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; public class FileReadToStringAllBytes { public static void main(String[] args) { String fileName = "d:/source.txt"; try { byte[] b = Files.readAllBytes(Paths.get(fileName)); String s = new String(b, StandardCharsets.UTF_8); System.out.println(s); } catch (IOException e) { e.printStackTrace(); } } } |
2) Read to String with Scanner Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.io.File; import java.io.IOException; import java.util.Scanner; public class FileReadToStringScanner { public static void main(String[] args) { String fileName = "d:/source.txt"; try { Scanner scanner = new Scanner( new File(fileName), "UTF-8" ); String s = scanner.useDelimiter("\\A").next(); scanner.close(); // Put this call in a finally block System.out.println(s); } catch (IOException e) { e.printStackTrace(); } } } |
3. InputStream to String
1) InputStream read() to ByteArrayOutputStream Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static String InputStreamToStringByRead(InputStream is) { //1343 try { ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) != -1) { result.write(buffer, 0, length); } return result.toString("UTF-8"); } catch (IOException e) { e.printStackTrace(); } return null; } |
2) InputStreamReader read() to StringBuilder Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public static String InputStreamToStringByStringBuilder1(InputStream is) { //6980 final int bufferSize = 1024; final char[] buffer = new char[bufferSize]; final StringBuilder out = new StringBuilder(); Reader in = null; try { in = new InputStreamReader(is, "UTF-8"); for (; ; ) { int rsz = in.read(buffer, 0, buffer.length); if (rsz < 0) { break; } out.append(buffer, 0, rsz); } return out.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } |
3) InputStream read() to StringBuilder Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static String InputStreamToStringByStringBuilder(InputStream is) { //8977 try { int ch; StringBuilder sb = new StringBuilder(); while((ch = is.read()) != -1) { sb.append((char) ch); } is.reset(); return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } |
4) InputStream Scanner Example
1 2 3 4 | public static String InputStreamToStringByScanner(InputStream is) { Scanner s = new Scanner(is).useDelimiter("\\A"); return s.hasNext() ? s.next() : ""; } |
5) BufferedInputStream read() to ByteArrayOutputStream Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public static String InputStreamToStringByByteArray(InputStream is) { BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream buf = new ByteArrayOutputStream(); int result = 0; try { result = bis.read(); while(result != -1) { buf.write((byte) result); result = bis.read(); } return buf.toString("UTF-8"); } catch (IOException e) { e.printStackTrace(); } return null; } |
댓글 없음:
댓글 쓰기