Cercando di ottenere ogni riga di un file di testo da un ArrayList
-
27-09-2019 - |
Domanda
va bene così sto cercando di leggere un file di testo. e poi dividerlo in linee che effettivamente rappresentano processi in arraylist tabella dei processi. Poi voglio dividere tutti i gettoni nella linea e fare tutti i segni della linea di un nuovo arraylist finora questo è tutto quello che ho:
non il suo lavoro correttamente. sto ottenendo:
processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF]]
processes:[[netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000], [netscape, 1, 00ACF3, 20990, 12DFFE, 000F00, 000000, 000000, 000000, AF, 011356, 000000, 000000, 4FFFFF, 39AB00, 000000, 0A0B92, FFFFFF, textpad, 2, 391BCA, 871BAF, DEA14C, EEFC30, 000000, 000000, 0000AA, AF, 000000, 000000, 000000, 000000, 000000, FFFFFF, B4344D, 000000]]
BufferedReader inputStream = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();
ArrayList<String> tokens = new ArrayList<String>();
/* read the file*/
try {
inputStream = new BufferedReader(new FileReader("p56.txt"));
while (true) {
/* while the file was read*/
/* now. split the file into the lines.*/
String line = inputStream.readLine();
if (line == null) {
break;
}
//if there are no more lines left. break
// split the lines into tokens and make into an arraylist*/
Scanner tokenize = new Scanner(line);
while (tokenize.hasNext()) {
/*while there are still more*/
tokens.add(tokenize.next());
}
lines.add(tokens);
System.out.println("processes:" + lines);
}
}
Soluzione
È necessario spostare la linea
ArrayList<String> tokens = new ArrayList<String>();
a destra prima
while (tokenize.hasNext()) {
Sarà quindi creare un nuovo elenco di token prima di elaborare ogni riga. In caso contrario, vi ritroverete con una lista di tutti i token per tutte le linee del file.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow