将文本文件中转储的文本转换为 Csv 和数组

发布于 2024-11-09 16:17:14 字数 1158 浏览 0 评论 0原文

我一直在使用 actionListener 将文本从多个 TextFields 传递到名为 writetouser.txt 的文本文件中,尽管我注意到这个,但效果很好文本只是转储并且不是数组的格式。出于此应用程序的目的,我有必要能够搜索此文本文件并根据输入的搜索显示值。我有一个文件,它从文本文件中读取数据,并尝试将数据转换为 CSV,以便它可以作为数组读取。我想知道必须输入什么而不是 user, pass 才能从文件 writetouser.txt 中获取文本。

附录:

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

i have been using an actionListenerto pass text from numerous TextFields into a text file named writetouser.txtthis works fine though i have noticed that this text is just dumped and is not in the format of an array. For the purposes of this application it is necessary that i am able to search this text file and show values based on searches entered. I have a file which reads the data from the text file and have attempted to convert the data to CSV's so it is is readable as an array. i am wondering what must be entered instead of user, pass in order to take the text from inside the filewritetouser.txt.

Addendum:

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

}
}

here is an example of the data which i am trying to separate. it is entered into textfile as

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

dburgess, 2345

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请爱~陌生人 2024-11-16 16:17:14

我想你想要的是:

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

I think what you want is:

String[] items = strLine.split(",");
那伤。 2024-11-16 16:17:14

我注意到这一行被注释掉了:

//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?  

那里有很多假设(我仍然不完全确定你想要做什么)——如果这些假设是正确的,让我知道;我将发布一些格式化提示。

I notice that this line is commented out:

//System.out.println (strLine);

Which suggests to me that you were probably trying to ensure that you're getting the line you expect from your file. If you are getting what you expect, we're clear to move on. If not, there's a problem in how the file is being read, or the data isn't formatted how you expect.

If we're good so far, we've arrived at the problem of parsing and displaying the content of the file. I'm assuming that you're trying to write the contents of the file to your terminal and expecting one format, but getting another.

I think what you'll see right now is something like this:

Username: dburgess
Password: 2345

(with no more than 1 username/password pair printed).

And I'm guessing you want something like this:

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

There are a lot of assumptions there (I'm still not entirely sure what you're trying to do) -- if those assumptions are correct, let me know; I'll post some formatting tips.

扛起拖把扫天下 2024-11-16 16:17:14

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

甲乙

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

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

Upon reviewing my code i noticed that the two pieces of code below were doing the exact same thing which is why i was being presented with each piece of info twice.

A

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

B

for (int i = 0; i < items.length; i++) {
    String item = items[i];
    ....
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文