2011-05-23 73 views
2

我一直在使用的actionListener从众多TextFields文本传递到一个名为writetouser.txt这工作得很好,虽然我已经注意到,这段文字是刚刚倾倒而不是格式的文本文件数组。为了这个应用程序的目的,我有必要搜索这个文本文件,并显示基于输入的值的值。我有一个文件从文本文件读取数据,并试图将数据转换为CSV's,因此它可以作为数组读取。我想知道必须输入什么,而不是用户,为了从文件writetouser.txt中取文本。将文本转换在文本文件转储到CSV和阵列

附录:

public void ReadUser() 
{ 
    try{ 
// Open the file 
FileInputStream fstream = new FileInputStream("writetouser.txt"); 
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
String strLine; 
//Read File Line By Line 
while ((strLine = br.readLine()) != null) { 
    // Print the content on the console 
    //System.out.println (strLine); 

    String[] items = strLine.split(","); 
    String[][] usersArray = new String [10][2]; 

    for (String item : items) { 
     System.out.println(item); 
    } 
} 
     //Close the input stream 
in.close(); 
}catch (Exception e){ 
    //Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
} 

} }

这里是我试图分离的数据的一个例子。它输入到文本文件作为

inputUser = user + ", " + pass; 
writetouser.WriteToUser(inputUser); 

dburgess,2345

+0

这将有很大的帮助,如果你能为我们提供一个你想读取数据的一个例子。 – Joe 2011-05-23 17:00:59

+1

上面的附录显示了一个数据的例子,以及它将文章传递给文本文件 – 2011-05-23 17:22:36

回答

0

在审查我的代码后,我注意到下面的两段代码做的是完全相同的事情,这就是为什么我每次都会看到每条信息两次。

一个

for (String item : items) {...} 

for (int i = 0; i < items.length; i++) { 
    String item = items[i]; 
    .... 
    } 
1

我想你想要的是:

String[] items = strLine.split(","); 
+0

的方法,我之前曾尝试过这样做,但我仅以我的文本文件的顶部行作为用户名,但没有密码分隔。也会捕获异常并显示错误消息。 – 2011-05-23 17:16:25

1

我注意到,这条线被注释掉:

//System.out.println (strLine); 

这表明你可能正试图确保你获得了你期望从你的文件中获得的线。如果你得到你所期望的,我们很清楚继续前进。如果没有,那么在读取文件的方式上存在问题,或者数据的格式不符合您的期望。

如果我们现在还不错,我们已经到了解析和显示文件内容的问题。我假设你正在尝试将文件的内容写入终端,并期待一种格式,但是会获得另一种格式。

我想你会看到现在的问题是这样的:

Username: dburgess 
Password: 2345 

(印有不超过1个用户名/密码对)。

而且我猜你想是这样的:

Username: dburgess  Password: 2345 
Username: someOtherUser Password: SomeOtherpass 
Username: blahblah  Password: etcPass 
.... 
Username: thelastOne  Password: Icanhazpassword? 

有很多的假设存在的(我还不能完全确定你想要做什么) - 如果这些假设是正确的,让我知道;我会发布一些格式化提示。

+0

你的假设确实是正确的。我已经删除了一些你可以在上面附录中看到的代码,现在我以完整数组的形式提供了一个没有用户名或密码的列表:尽管你的布局显得更好,所以我们将会感激提示。 – 2011-05-23 18:11:13