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

35
LINES

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

JavaX fragment (include)

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

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

  FileInputStream fileInputStream = new FileInputStream(f);
  InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
  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   

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

No comments. add comment

Snippet ID: #1001049
Snippet name: loadTextFile (function)
Eternal ID of this version: #1001049/9
Text MD5: caca461b99f11a2ebbfdedecf3cbdb2f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-11 11:17:25
Source code size: 1004 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 526 / 8541
Version history: 8 change(s)
Referenced in: [show references]

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