2013-01-05 31 views
0

我的代码应该读取文件并将每行(每条记录)存储到一个字符串数组中。将文件读取到字符串数组并显示每条记录

我的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!

(我想为抽象表模型提供此信息)

谢谢。

回答

1

您的代码修改:

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(); 
    } 
} 
0

br.read()从每行的开头读取一个字符,剩下的部分用br.readLine()读取。

这个内循环没什么意义。

for(int i=0;i<=br.read();i++){ 
    data[i] = br.readLine(); 
    System.out.println(data[i]); 
} 

这应该是你需要的一切。如果您不想要第一行,请在循环前添加对br.readLine()的额外呼叫。

int i = 0; 
while((line=br.readLine())!=null){ 
    data[i] = line; 
    System.out.println(line); 
    i++; 
} 

你也应该尝试使用dynamicaly大小的数据结构来存储的字符串(例如ArrayList<String>),如果你不知道有多少行期望。然后您可以使用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(); 
+0

我可以使用表模型构建的ArrayList? – user1945649

+0

我不这么认为,但是List有'.toArray(...)'。将你的字符串收集到ArrayList中,然后在需要时使用'.toArray(...)'获取一个String []。 – Mike

+0

非常感谢你! – user1945649

1

data阵列将永远是100的尺寸,因为当你比如它(String[] data = new String[100])创建一个具有100个指数空白阵列。您可以使用String[]而不是使用

+0

列表是Arraylist?或者它不同? – user1945649

+0

是的。列表是一个接口。使用这种类型的变量是很好的做法。它可以指向一个ArrayList 对象。 – ncmathsadist

+0

我可以在表模型构造函数中使用ArrayList吗?怎么样? – user1945649

2

因为您在第二行声明了数组大小为100。所以,如果基本上有两个选项,如果文件中的行数不会改变,则将数组的大小声明为5.如果要改变,那么我建议您使用例如ArrayList

List<String> data = new ArrayList<String>(); 
//in the while loop 
data.add(br.readLine()); 
+0

行数将会更多 – user1945649

+0

然后按照我的建议使用ArrayList,它是一个集合,这意味着您不必关心它的大小,它会根据需要更改动态性 –

+0

我可以在表模型构造函数中使用ArrayList吗?怎么样? – user1945649

0

在读取行的循环内部,使用String方法split来处理每一行。不要从阅读器中提取,因为它在读取文件时将文件指针向前移动。您可以使用空格分割

String [] parts = stringName.split("\\s"); 

然后您可以完全访问每行中的所有三个项目。

0

为什么这么复杂?下面是 是我的解决方案,供参考。

String line; 
int cnt; 

cnt = 0; 
while((line = br.readLine()) != null){ 
    System.out.println(line); 
    cnt++; 
} 

br.close(); 
System.out.println("Data length: "+cnt); 
相关问题