gpt4 book ai didi

java - 缓冲文本写入文件

转载 作者:行者123 更新时间:2023-12-02 03:25:47 26 4
gpt4 key购买 nike

所以我试图返回一个长度为200的大字符串数组,但是我使用的代码只返回15/在执行期间当它开始保留200个空格作为数组的大小时将数组长度设置为15可能会发生变化,因此我将其放大以尝试适应不同的尺寸。

public static String[] populateArray(String fileName)
{
String [] record = new String [200]; // array length is 200
try
{
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
int counter = 0;
int arrayLength = 0; // using this to check length during execution
while((line = bufferedReader.readLine())!= null && counter < 200)
{
record[counter] = line;
counter +=1;
arrayLength = record[0].length(); // the file contains 20 lines to input here but
only does 15
}
bufferedReader.close();

}
catch (IOException ex)
{
System.out.println("There was an error while trying to access the file");
}
return record;
}

最佳答案

文件的第一行是否有 15 个字符长?看起来您正在查找该字符串的长度,而不是读入的行数。此外,您在每次循环迭代中将 arrayLength 变量设置为相同的值。

如果你想计算读取的行数,我建议只打印计数器变量。

您发布的代码似乎可以工作,读取文件的所有行(使用我的测试文件)。因此,我猜测您的问题是查看错误的变量以验证读入的内容。

关于java - 缓冲文本写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38937861/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com