1 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
2 | if (!new File(fileName).exists()) |
3 | return defaultContents; |
4 | |
5 | FileInputStream fileInputStream = new FileInputStream(fileName); |
6 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
7 | return loadTextFile(inputStreamReader); |
8 | } |
9 | |
10 | public static String loadTextFile(Reader reader) throws IOException { |
11 | StringBuilder builder = new StringBuilder(); |
12 | try { |
13 | BufferedReader bufferedReader = new BufferedReader(reader); |
14 | String line; |
15 | while ((line = bufferedReader.readLine()) != null) |
16 | builder.append(line).append('\n'); |
17 | } finally { |
18 | reader.close(); |
19 | } |
20 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
21 | } |
Began life as a copy of #2000321
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #2000322 |
Snippet name: | loadTextFile (function) |
Eternal ID of this version: | #2000322/1 |
Text MD5: | 2f3bd295d7702aa21fa2e766e0531b22 |
Author: | stefan |
Category: | javax |
Type: | New Tinybrain snippet |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-28 20:15:40 |
Source code size: | 841 bytes / 21 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 679 / 5354 |
Referenced in: | [show references] |