Converting text dumped in textfile to Csv and Array
i have been using an actionListener
to pass text from numerous TextFields
into a text file named writetouser.txt
this 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开发者_C百科) {
// 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
I think what you want is:
String[] items = strLine.split(",");
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.
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];
....
}
精彩评论