Wątek przeniesiony 2017-07-23 21:26 z Java przez Patryk27.

Zapisywanie się cały czas nulli do pliku

0

Witam, Aby przećwiczyć kolejny materiał postanowiłem napisać program, który będzie zapisywał dane do pliku. Ale mam problem, gdyż zamiast danych w pliku mam wartości "null". W klasie WriteFile próbowałem już różnych sposobów, ale żaden nie działa.

Prosiłbym o jakieś wskazówki co robię źle.

A więc, mam klasę w której zdefiniowane mam prywatne pola oraz settery i gettery do tych pól.

package test.files;

import java.util.Date;

public class ClientCard {

    private String name;
    private Date dateOfBirthday;
    private String street;
    private int numberOfHome;
    private String city;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDateOfBirthday() {
        return dateOfBirthday;
    }

    public void setDateOfBirthday(Date dateOfBirthday) {
        this.dateOfBirthday = dateOfBirthday;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public int getNumberOfHome() {
        return numberOfHome;
    }

    public void setNumberOfHome(int numberOfHome) {
        this.numberOfHome = numberOfHome;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Name: " + getName() + "\nDate: " + getDateOfBirthday() +
                "\nStreet: " + getStreet() + " " + getNumberOfHome() + "\nCity: " +
                getCity();
    }
}

W następnej klasie mam wczytywanie danych:

package test.files;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class GetClient {

    ClientCard clientCard = new ClientCard();
    Scanner scanner = new Scanner(System.in);

    public void getData() {
        System.out.print("Name: ");
        String name = scanner.nextLine();
        clientCard.setName(name);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
        Date date = null;

        boolean dateGood = false;
        do{
            try{
                System.out.print("Give date of birthday in format (yyyy.mm.dd): ");
                String dateOfBirthday = scanner.nextLine();
                date = sdf.parse(dateOfBirthday);
                clientCard.setDateOfBirthday(date);
                dateGood = true;
            }catch (ParseException pe) {
                System.out.println("Wrong date");
            }
        }while(dateGood == false);

        System.out.print("Street: ");
        clientCard.setStreet(scanner.nextLine());

        System.out.print("Number home: ");
        clientCard.setNumberOfHome(scanner.nextInt());
        scanner.nextLine();
        System.out.print("City: ");
        clientCard.setCity(scanner.nextLine());
    }
}

Klasa do zapisu oraz klasa testowa:

package test.files;

import java.io.*;

public class WriteFile{

    File file = new File("card.txt");
    ClientCard clientCard = new ClientCard();

    public void writeFiles(String filePath) throws IOException {

        if(file.createNewFile())
            System.out.println("File was created");
        else
            System.out.println("File exist");

        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath, true));

        try{
            bufferedWriter.append(clientCard.getName());
            bufferedWriter.newLine();
            bufferedWriter.append(clientCard.getCity());
        }finally {
            bufferedWriter.close();
        }
    }

}


package test.files;


import java.io.IOException;

public class TestClass {
    public static void main(String[] args) throws IOException {

        String filePath = "D:\\Java-Project\\src\\test\\files\\card.txt";
        GetClient getClient = new GetClient();
        WriteFile writeFile = new WriteFile();

        getClient.getData();
        writeFile.writeFiles(filePath);
    }
}

0
ClientCard clientCard = new ClientCard();

W tym momencie tworzysz nową instancję ClientCard, zupełnie niepowiązaną z Twoją zmienną lokalną.

1

Nigdzie nie zwracasz, ani nie przekazujesz do zapisu stworzonego w klasie GetClient obiektu ClientCard. Najłatwiej teraz byłoby chyba przekazanie do metody writeFiles oprócz ścieżki, również obiektu do zapisu.
Możesz się pozbyć pola clientCard z klasy WriteFile i korzystać ze zmiennej lokalnej w metodzie.

Nazwa metody getData sugeruje że zwracasz jakiś obiekt, podczas gdy ty tego nie robisz. W klasie GetClient też bym zrezygnował z pola typu ClientCard, tworzyłbym obiekt w metodzie i później go zwracał.
Później w mainie byłoby

GetClient getClient = new GetClient();
ClientCard clientCard = getClient.getData();

WriteFile wf = new WriteFile();
wf.writeFiles(path, clientCard);
0

Dzięki, zastosowałem się do Twoich zmian i wszystko jest ok.
Patrząc na świeżo na mój kod, który wrzuciłem to faktycznie zamieszałem :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1