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).

static S loadTextFile(S fileName) {
  return loadTextFile(fileName, null);
}

static S loadTextFile(File f, S defaultContents, S encoding default "UTF-8") ctex {
  ifndef LeanMode
  checkFileNotTooBigToRead(f);
  endifndef
  if (f == null || !f.exists()) ret defaultContents;

  FileInputStream fileInputStream = new FileInputStream(f);
  InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, encoding);
  ret loadTextFile(inputStreamReader);
}

public static String loadTextFile(File fileName) {
  return loadTextFile(fileName, null);
}

static S loadTextFile(S fileName, String defaultContents) {
  ret fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents);
}

static S loadTextFile(Reader reader) throws IOException {
  new StringBuilder builder;
  try {
    char[] buffer = new char[1024];
    int n;
    while (-1 != (n = reader.read(buffer)))
      builder.append(buffer, 0, n);
  } finally {
    reader.close();
  }
  ret str(builder);
}

Author comment

Began life as a copy of #2000322

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, 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: 1023 / 9544
Version history: 9 change(s)
Referenced in: #1001110 - x26.java (JavaX)
#1001148 - x27.java (JavaX)
#1001370 - x28.java (JavaX)
#1001600 - x29.java (JavaX)
#1001902 - loadTextFileMandatory
#1002427 - Accellerating 629 (SPIKE)
#1002887 - loadTextFileStartingFrom (function)
#1004305 - x31.java (JavaX)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007055 - loadTextFilePossiblyGZipped (function)
#1009008 - loadTextFileWithEncoding
#1022206 - loadTextFile_trim
#1029307 - loadTextFile_raw (use ISO-8859-1)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)