자바 fileGetContents()

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:49 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

자바 fileGetContents()
public static String fileGetContents(String filename) {
	try {
		if( new UrlValidator().isValid(filename) ) {
			Scanner scanner = new Scanner(new URL(filename).openStream(), "UTF-8");
			scanner.useDelimiter("\\A");
			String content = scanner.next();
			scanner.close();
			return content;
		}
		return new String(Files.readAllBytes(Paths.get(filename)));
	}
	catch (MalformedURLException e) { e.printStackTrace(); }
	catch (IOException e) { e.printStackTrace(); }
	return null;
}
...
System.out.println( Util.fileGetContents("C:\\Windows\\win.ini") );
System.out.println( Util.fileGetContents("http://example.zetawiki.com/txt/utf8test.txt") );

2 같이 보기[ | ]