为什么react-native-fs的readFile只读取文本文件的一部分?

时间:2019-05-24 00:44:23

标签: react-native readfile react-native-fs

我试图使用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个字符。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用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);