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, charsetForTextFiles); |
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 | } |
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: | #2000321 |
Snippet name: | loadTextFile (function) |
Eternal ID of this version: | #2000321/1 |
Text MD5: | 07ae8ed01b326e85b2cf99af3706acb0 |
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:09:17 |
Source code size: | 853 bytes / 21 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 691 / 161 |
Referenced in: | [show references] |