如何在Java中为新变量赋值?

时间:2016-11-08 15:56:18

标签: java

当我使用方法RDBFRIPF修改实例变量getIP()cngDB()时,我在rsltUPDT()中对它们求和,我确实得到了属性文件我需要,但是当我只需要修改rsltUPDT()方法中的两个值的一个值时,在prp.setProperty("db.connection", RIPF + RDBF);我可以修改一个值,但另一个值被设置为null

我该如何解决这个问题?

import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;

public class TztDB {

    private static String RIPF;
    private static String RDBF;

    public void getNewFile() throws Exception {

        Properties prp = new Properties();

        File flix = new File("default.properties");
        FileOutputStream fileu = new FileOutputStream(flix);
        prp.setProperty("db.connection", "");
        prp.store(fileu, "Si vez esto, el archivo se creo exitosamente");
        fileu.close();  
    }


    public String changeDB() {
        Scanner c = new Scanner(System.in);
        System.out.println("OK!. Especifique la base de datos: ");

        RDBF = c.nextLine();

        return RDBF;
    }

    public void getCKT() throws Exception {
        TztDB tzt = new TztDB();
        File f = new File("./default.properties");
        if (f.exists() && !f.isDirectory()) { 

            System.out.println("Cargado");

        } else {

            JOptionPane.showMessageDialog(null, "Error. Archivo no encontrado o directorio incorreto. Creando archivo nuevo.", "Error de Aplicacion", JOptionPane.ERROR_MESSAGE);
            tzt.getNewFile();       
        }
    }


    public String getIP() throws IOException, InputMismatchException {

        Scanner scnIP = new Scanner(System.in);

        System.out.println("Hola! Buena Eleccion.");
        System.out.print("IP: ");
        RIPF = scnIP.nextLine();    

        return RIPF;
    }


    public void getUserOP() {

        TztDB obk = new TztDB();
        Scanner input1 = new Scanner(System.in);
        int OP;

        System.out.print("Hola, Usuario! ¿Que deseas hacer? \n\n ");
        System.out.println("1-Cambiar Direccion IP. \n 2-Cambiar Base de Datos. ");

        try { 
            OP = input1.nextInt();

            if (OP == 1) {

                try {

                    obk.getIP(); 

                } catch (Exception e) {

                    System.out.println(e.getMessage());
                }
            }  else if (OP ==2) {

                try {

                    obk.changeDB();

                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }

            } else if(OP == 3) {
                System.out.println("404 :)!");
            } else {
                System.out.println("Opcion invalida. Reinice el programa...");
            }

       } catch (Exception x) {
            System.out.println(x.getMessage());
       }
    }

    public void rsltUPDT() throws Exception {
        TztDB c = new TztDB();
        Scanner CIN = new Scanner(System.in);

        Properties prp = new Properties();
        prp.setProperty("db.connection", RIPF + RDBF);

        File flix = new File("default.properties");
        FileOutputStream fileu = new FileOutputStream(flix);

        prp.store(fileu, "Si vez esto, la operacion fue un exito!");
        fileu.close();
    }

    public static void main(String [] args) throws Exception {

        TztDB cole = new TztDB();
        cole.getCKT();
        cole.getUserOP();
        cole.rsltUPDT();
    }
}

0 个答案:

没有答案
相关问题