Ich wollte, dass dieses Programm UTF-8-Buchstaben in. TXT-Datei schreiben, aber als Ergebnis habe ich Nummern und andere non-sense Zeichen. Könnte mir jemand helfen, es zu lösen? Ich habe keine Ahnung, wie man es in Druckschriftsteller macht. Ich werde jede Hilfe zu schätzen wissen. Hier ist mein Code.PrintWriter schreibt etwas seltsam zu txt
//all imports
public class Main extends Application {
Stage okno;
TableView<Produkt> tabela;
TextField nazwaWejscie, iloscWejscie;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
okno = primaryStage;
okno.setTitle("Stwórz listę zakupów");
// Kolumna nazwa
TableColumn<Produkt, String> kolumnaNazwa = new TableColumn<>("Nazwa");
kolumnaNazwa.setMinWidth(200);
kolumnaNazwa.setCellValueFactory(new PropertyValueFactory<>("nazwa"));
// Kolumna ilosc
TableColumn<Produkt, Double> kolumnaIlosc = new TableColumn<>("Ilość");
kolumnaIlosc.setMinWidth(100);
kolumnaIlosc.setCellValueFactory(new PropertyValueFactory<>("ilosc"));
// Wprowadź nazwę
nazwaWejscie = new TextField();
nazwaWejscie.setPromptText("Nazwa (string)");
nazwaWejscie.setMinWidth(100);
// Wprowadź ilość
iloscWejscie = new TextField();
iloscWejscie.setPromptText("Ilość (double)");
// Przyciski
Button dodaj = new Button("Dodaj");
dodaj.setOnAction(e -> kliknijDodaj());
Button usun = new Button("Usuń");
usun.setOnAction(e -> kliknijUsun());
Button stworzListe = new Button("Zapisz listę");
stworzListe.setOnAction(e -> zapiszListe());
HBox pojemnik = new HBox();
pojemnik.setPadding(new Insets(10, 10, 10, 10));
pojemnik.setSpacing(10);
// wstaw pola i przyciski w jeden rząd
pojemnik.getChildren().addAll(nazwaWejscie, iloscWejscie, dodaj, usun, stworzListe);
tabela = new TableView<>();
tabela.setItems(getProdukt());
tabela.getColumns().addAll(kolumnaNazwa, kolumnaIlosc);
VBox vBox = new VBox();
vBox.getChildren().addAll(tabela, pojemnik);
Scene scena = new Scene(vBox);
okno.setScene(scena);
okno.show();
}
// Funkcja dodania produktu
public void kliknijDodaj() {
Produkt Produkt = new Produkt();
Produkt.setNazwa(nazwaWejscie.getText());
Produkt.setIlosc(Double.parseDouble(iloscWejscie.getText()));
tabela.getItems().add(Produkt);
nazwaWejscie.clear();
iloscWejscie.clear();
}
// Funkcja usunięcia produktu
public void kliknijUsun() {
ObservableList<Produkt> ProduktWybrany, wszystkieProdukty;
wszystkieProdukty = tabela.getItems();
ProduktWybrany = tabela.getSelectionModel().getSelectedItems();
ProduktWybrany.forEach(wszystkieProdukty::remove);
}
public ObservableList<Produkt> getProdukt() {
ObservableList<Produkt> produkty = FXCollections.observableArrayList();
return produkty;
}
// Funkcja dodania listy do pliku
public void zapiszListe() {
ArrayList<Produkt> produkty = new ArrayList<Produkt>(tabela.getItems());
int licznik = 1;
try {
File plik = new File("C:\\Users\\Sebastian\\Desktop\\lista.txt");
if (!plik.exists()) {
plik.createNewFile();
}
FileWriter fw = new FileWriter(plik, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
for (int i = 0; i < produkty.size(); i++) {
pw.println(licznik + ". " + produkty.get(i));
licznik++;
}
pw.close();
} catch (IOException ex) {
System.out.println("Błąd: ");
ex.printStackTrace();
}
}
}
Sie müssen Code oder Beispiel Code teilen Sie verwenden für mehr Verständnis für Ihr Problem. – darkomen