Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

35
LINES

< > BotCompany Repo | #1001049 - loadTextFile (function)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2523L/16K).

1  
static S loadTextFile(S fileName) {
2  
  return loadTextFile(fileName, null);
3  
}
4  
5  
static S loadTextFile(File f, S defaultContents, S encoding default "UTF-8") ctex {
6  
  ifndef LeanMode
7  
  checkFileNotTooBigToRead(f);
8  
  endifndef
9  
  if (f == null || !f.exists()) ret defaultContents;
10  
11  
  FileInputStream fileInputStream = new FileInputStream(f);
12  
  InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, encoding);
13  
  ret loadTextFile(inputStreamReader);
14  
}
15  
16  
public static String loadTextFile(File fileName) {
17  
  return loadTextFile(fileName, null);
18  
}
19  
20  
static S loadTextFile(S fileName, String defaultContents) {
21  
  ret fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents);
22  
}
23  
24  
static S loadTextFile(Reader reader) throws IOException {
25  
  new StringBuilder builder;
26  
  try {
27  
    char[] buffer = new char[1024];
28  
    int n;
29  
    while (-1 != (n = reader.read(buffer)))
30  
      builder.append(buffer, 0, n);
31  
  } finally {
32  
    reader.close();
33  
  }
34  
  ret str(builder);
35  
}

Author comment

Began life as a copy of #2000322

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1001049
Snippet name: loadTextFile (function)
Eternal ID of this version: #1001049/10
Text MD5: fc8b2f7fdca4a671ce9dfc4e2d3a238c
Transpilation MD5: 9c115e8428deb9ac2122d9822b2aca91
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-27 17:33:11
Source code size: 1033 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 599 / 8720
Version history: 9 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1001049 & http://1001049.tinybrain.de