我的代码应该读取文件并将每行(每条记录)存储到一个字符串数组中。将文件读取到字符串数组并显示每条记录
我的txt文件是:
FName Lname Number
second secondsecond 22
thired thithird 33
fourth fourfourr 44
fifth fiffif 55
但是,当我运行我的代码,我的程序不显示每一行的第一个字符! 显示是这样的:
econd secondsecond 22
hired thithird 33
ourth fourfourr 44
ifth fiffif 55
我的代码:
public class ReadfileIntoArray {
String[] columns=new String[] {"FName","Lname","Number"};
String[] data=new String[100];
public void read() throws IOException{
FileReader fr=new FileReader("D:\\AllUserRecords.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null){
for(int i=0;i<=br.read();i++){
data[i]=br.readLine();
System.out.println(data[i]);
}
}
br.close();
System.out.println("Data length: "+data.length);
}
public static void main(String[] args) throws IOException{
ReadfileIntoArray rfta=new ReadfileIntoArray();
rfta.read();
}
}
,我想看到的数据长度:5(因为我有五点一线),但我看到100!
(我想为抽象表模型提供此信息)
谢谢。
我可以使用表模型构建的ArrayList? – user1945649
我不这么认为,但是List有'.toArray(...)'。将你的字符串收集到ArrayList中,然后在需要时使用'.toArray(...)'获取一个String []。 – Mike
非常感谢你! – user1945649