Java 将文件读取到字符串数组并显示每条记录
我的代码应该读取文件并将每一行(每条记录)存储到一个字符串数组中 我的txt文件是:Java 将文件读取到字符串数组并显示每条记录,java,arrays,file,Java,Arrays,File,我的代码应该读取文件并将每一行(每条记录)存储到一个字符串数组中 我的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
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();
}
}
公共类ReadfileIntoArray{
字符串[]列=新字符串[]{“FName”、“Lname”、“Number”};
字符串[]数据=新字符串[100];
public void read()引发IOException{
FileReader fr=新的FileReader(“D:\\AllUserRecords.txt”);
BufferedReader br=新的BufferedReader(fr);
弦线;
而((line=br.readLine())!=null){
for(int i=0;ibr.read()
从每行开头读取一个字符,剩下的字符由br.readLine()
读取
这种内部循环毫无意义
for(int i=0;i<=br.read();i++){
data[i] = br.readLine();
System.out.println(data[i]);
}
如果不知道需要多少行,还应该尝试使用动态大小的数据结构来存储字符串(例如,ArrayList
),然后可以使用myList.size()
来获取行数
List myList = new ArrayList<String>();
while((line=br.readLine())!=null){
myLine.add(line);
System.out.println(line);
}
System.out.println(myList.size());
//Retrieve the data as a String[].
String[] data = (String[]) myList.toArray();
List myList=new ArrayList();
而((line=br.readLine())!=null){
添加(行);
系统输出打印项次(行);
}
System.out.println(myList.size());
//以字符串[]的形式检索数据。
字符串[]数据=(字符串[])myList.toArray();
br.read()
从每行开头读取一个字符,剩下的字符由br.readLine()
读取
这种内部循环毫无意义
for(int i=0;i<=br.read();i++){
data[i] = br.readLine();
System.out.println(data[i]);
}
如果不知道需要多少行,还应该尝试使用动态大小的数据结构来存储字符串(例如,ArrayList
),然后可以使用myList.size()
来获取行数
List myList = new ArrayList<String>();
while((line=br.readLine())!=null){
myLine.add(line);
System.out.println(line);
}
System.out.println(myList.size());
//Retrieve the data as a String[].
String[] data = (String[]) myList.toArray();
List myList=new ArrayList();
而((line=br.readLine())!=null){
添加(行);
系统输出打印项次(行);
}
System.out.println(myList.size());
//以字符串[]的形式检索数据。
字符串[]数据=(字符串[])myList.toArray();
您的数据
数组的大小始终为100,因为当您对其进行实例时(String[]data=new String[100]
)会创建一个包含100个索引的空数组。而不是使用String[]
,您可以使用列表
您的数据
数组的大小始终为100,因为当您实例它时(String[]data=new String[100]
)会创建一个包含100个索引的空白数组。而不是使用String[]
,您可以使用列表
,因为您已在第二行中声明数组大小为100。因此,您基本上有两个选项,如果文件中的行数不变,则将数组大小声明为5。如果它将发生变化,则我建议您使用例如
List data=new ArrayList();
//在while循环中
data.add(br.readLine());
因为您在第二行中已将数组大小声明为100。因此,您基本上有两个选项,如果文件中的行数不变,则将数组大小声明为5。如果它将发生变化,则我建议您使用例如
List data=new ArrayList();
//在while循环中
data.add(br.readLine());
在读取行的循环内部,使用String方法split
处理每行。不要从读取器中绘制,因为它在读取时会向前移动文件指针。您可以使用
String [] parts = stringName.split("\\s");
然后您可以完全访问每行中的所有三项。在读取行的循环内部,使用字符串方法split
处理每行。不要从读取器中绘制,因为它在读取时会向前移动文件指针。您可以使用
String [] parts = stringName.split("\\s");
然后您就可以完全访问每行中的所有三项。为什么这么复杂?
下面是我的解决方案,供参考
String line;
int cnt;
cnt = 0;
while((line = br.readLine()) != null){
System.out.println(line);
cnt++;
}
br.close();
System.out.println("Data length: "+cnt);
为什么这么复杂?
下面是我的解决方案,供参考
String line;
int cnt;
cnt = 0;
while((line = br.readLine()) != null){
System.out.println(line);
cnt++;
}
br.close();
System.out.println("Data length: "+cnt);
您的代码已修改:
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;
int i = 0;
while ((line = br.readLine()) != null) {
data[i] = line;
System.out.println(data[i]);
i++;
}
br.close();
// This is for resize the data array (and data.length reflect new size)
String[] dataNew = new String[i];
System.arraycopy(data, 0, dataNew, 0, i);
data = dataNew;
System.out.println("Data length: " + data.length);
}
public static void main(String[] args) throws IOException {
ReadfileIntoArray rfta = new ReadfileIntoArray();
rfta.read();
}
}
您的代码已修改:
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;
int i = 0;
while ((line = br.readLine()) != null) {
data[i] = line;
System.out.println(data[i]);
i++;
}
br.close();
// This is for resize the data array (and data.length reflect new size)
String[] dataNew = new String[i];
System.arraycopy(data, 0, dataNew, 0, i);
data = dataNew;
System.out.println("Data length: " + data.length);
}
public static void main(String[] args) throws IOException {
ReadfileIntoArray rfta = new ReadfileIntoArray();
rfta.read();
}
}
list是Arraylist吗?还是它不同?是的。list是一个接口。使用这种类型的变量是一个很好的做法。它可以指向Arraylist对象。我可以在表模型构造函数中使用Arraylist吗?如何使用?list是Arraylist?还是它不同?是的。list是一个接口。使用这种类型的变量是一个很好的做法。它可以指向ArrayList对象。我可以在表模型构造函数中使用ArrayList吗?如何使用?这些行将更多地使用ArrayList,正如我所建议的,它是一个集合,这意味着您不必关心它的大小,它将根据需要动态更改。我可以在表模型构造函数中使用ArrayList吗?如何使用?使用.toArray()
在完成读取后获取字符串数组。(即,在循环后使用data=(String[])myList.toArray();
)@user1945649:什么是表模型构造函数?你可以在任何地方使用它,只需创建一个列表,然后使用它的方法添加到新字符串-正如我在上面的几行中所写的,我们将继续使用ArrayList,正如我所建议的,它是一个集合,这意味着你不必关心它的大小,它将根据你的需要进行动态更改se ArrayList在表模型构造函数中?如何?在完成读取后使用.toArray()
获取字符串数组。(即,在循环后使用data=(String[])myList.toArray();
)@user1945649:什么是表模型构造函数?您可以在任何地方使用它,只需创建一个列表,然后使用其方法添加到新字符串-正如我上面所写的,我可以在表模型构造函数中使用ArrayList吗?我不相信,但列表确实有.toArray(…)
。在ArrayList中收集字符串,然后使用.toArray(…)
在需要时获取字符串[]。我可以在表模型构造函数中使用ArrayList吗?我不这么认为,但列表中确实有.toArray(…)
。在ArrayList中收集字符串,然后在需要时使用.toArray(…)
获取字符串[]。