Edycja danych w pliku properties.

0

Witam,
Mam taką strukturę projektu:
Capture.PNG
próbuję zmienić zawartość tego pliku z poziomu javy, nie usuwając reszty parametrów. Jak uzyskać ścieżkę do tego pliku? Próbowałem System.getProperty("user.dir") +//db.properties; ale to nie znajduję.

 
    private void loadPropertiesDb(){
        try{
            FileInputStream in = new FileInputStream("db.properties");
            Properties props = new Properties();
            props.load(in);
            in.close();


            FileOutputStream out = new FileOutputStream("db.properties");
            props.setProperty("javax.persistence.jdbc.user", tFUserDb.getText());
            props.setProperty("javax.persistence.jdbc.password", PFDb.getText());
            props.setProperty("javax.persistence.jdbc.url", tFUrlDb.getText());

            props.store(out, null);
            out.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }{{Image:Capture.PNG}}
javax.persistence.jdbc.driver=com.mysql.jdbc.Driver
javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/mojabaza
javax.persistence.jdbc.user=marcin
javax.persistence.jdbc.password=marcin
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.hbm2ddl.auto=update
hibernate.connection.useUnicode=true
hibernate.connection.characterEncoding=UTF-8
 
java.io.FileNotFoundException: db.properties (The system cannot find the file specified)
0

Teraz nie dostaję żadnego błędu, ale dane się nie zmieniają w pliku, ponieważ jak ustawię usera i hasło, którego nie mam w mysql to dalej aplikacja się włącza.

    private void loadPropertiesDb() {
        try {
            InputStream is = getClass().getResourceAsStream("/db.properties");
            System.out.println(is);

            Reader reader = new InputStreamReader(is);
            //FileInputStream in = new FileInputStream(String.valueOf(getClass().getResourceAsStream("/db.properties")));
            Properties props = new Properties();
            props.load(is);
            is.close();
            reader.close();

            //Reader out = new InputStreamReader(is);

            FileOutputStream out = new FileOutputStream(String.valueOf(getClass().getResourceAsStream("/db.properties")));

            props.setProperty("javax.persistence.jdbc.user", tFUserDb.getText());
            props.setProperty("javax.persistence.jdbc.password", PFDb.getText());
            props.setProperty("javax.persistence.jdbc.url", tFUrlDb.getText());

            props.store(out, null);
            out.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    } 

Capture2.PNG

1

a czy Twój setProperty nie zmieni wartości tylko dla backendującej jej Hashtable ?

http://crunchify.com/java-properties-files-how-to-update-config-properties-file-in-java/

zresztą chcesz zapisać do streama który zamknąłeś... robisz tu niezły dafuq

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