Ich lese Daten über Sitze für ein Reservierungssystem aus einer Textdatei und in eine ArrayList. Eine Zeile wird in eine Variable FileLine eingefügt. Die Dateizeile wird von jedem "," getrennt und in ein Array namens Komponenten eingefügt. Dann werden die Komponenten in Objekte eingefügt.Ersetzen Sie bestimmte Zeile in TXT-Datei
Beispiel für txt-Datei
1a, first, window, notable, forward, noeoa, false, null
1b, first, nowindow, notable, forward, noeoa, false, null
2a, first, window, notable, forward, eoa, false, null
2b, first, nowindow, notable, forward, eoa, false, null
3a, first, window, table, forward, noeoa, false, null
3b, first, nowindow, table, forward, noeoa, false, null
Das geht dann in eine if-Anweisung zu überprüfen, ob der Sitz geeignet ist, und wenn es ich will, ist falsch wahr ändern und Beifahrer Namen auf null, sondern nur auf dem Sitz das ist geeignet. Ich möchte wissen, ob es irgendeine Möglichkeit gibt, dass ich eine bestimmte Zeile in einer Textdatei überschreiben kann. Die Zeilennummer wird in einer Variablen namens Linecounter
Scanner inFile = new Scanner(
new FileReader("seats.txt"));
ArrayList<seat> seats = new ArrayList<>();
Boolean var = false;
while (inFile.hasNext()) {
String fileLine = inFile.nextLine();
seats.add(new seat(fileLine));
if (seat.classs.equals(seatClass) && (seat.window.equals(seatLocation) || seat.both != null)
&& seat.table.equals(seatTable) && seat.face.equals(seatFace) && seat.EOA.equals(seatEOA)
&& seat.resevered == false) {
System.out.print("Seat " + seat.seat + " Booked");
var = true;
seat.resevered = true;
seat.resName = emailSelect;
}
gespeichert Wo fileLine aufgeteilt
public seat(String fileLine)
{
String[] components = fileLine.split(",");
if (components[7].equals("null"))
this.resName = null;
else
this.resName = components[7];
if (components[2].equals(" both"))
this.both = "notnull";
else
this.both = null;
this.seat = (components[0].trim());
this.classs = (components[1].trim());
this.window = (components[2].trim());
this.table = (components[3].trim());
this.face = (components[4].trim());
this.EOA = (components[5].trim());
this.resevered = Boolean.parseBoolean(components[6].trim());
}
Mögliche Duplikate von [Java-Datei - Öffnen Sie eine Datei und schreiben Sie daran] (http://stackoverflow.com/questions/10667734/java-file-open-a-file-and-write-to-it) – Natecat