ScannerでファイルとWEBページを一気に読み込む
ファイルを読み込む
public class TextScanner { public static void main(String[] args) { readFile("dir/Test.txt"); } // \\Zで最後まで一気に読み込み private static void readFile(String fileName) { try { Scanner sc = new Scanner(new File(fileName)); System.out.println(sc.useDelimiter("\\Z").next()); sc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }
webページを読み込む
public class WebPageScanner { public static void main(String[] args) { webScan("http://www.google.com/"); } // \\Zで最後まで一気に読み込み public static void webScan(String pageURL) { try { URLConnection conn = new URL(pageURL).openConnection(); String text = new Scanner(conn.getInputStream()).useDelimiter("\\Z").next(); System.out.println(text); } catch (IOException e) { e.printStackTrace(); } } }