1

我试图从带有 react-native-fs readFile 函数的文本文件中读取,结果字符串总是以某些字符结尾。

我已经更改了文本文件的内容,它仍然在 4094 个字符后结束。

  await RNFS.readFile(p, 'utf8')
    .then((readresult) => {
    console.log(readresult);
    res = {success: true, contents: readresult};
  })
  .catch((err) => {
    console.log('ERROR' + err.message);
    res = {success: false, errorMsg: err.message};
  })

日志总是产生前 4094 个字符。

4

1 回答 1

1

您可以尝试使用read逐个读取文件:

// We'll call this function multiple times to read the file in chunks.
// Feel free to append additional error handling and logging to this function.
const readChunk = (file, length, position) => {
  return RNFS.read(file, length, position, 'utf8');
}

// Set the number of character to read in each chunk
const length = 4094;
let chunk = '';
let total = '';

// Add together the chunks as you read them.
// When the next chunk is empty, you've reached the end of the file.
do {
  chunk = await readChunk(p, length, total.length);
  total += chunk;
} while (chunk.length > 0);
于 2019-05-24T01:21:20.970 回答