有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Java中为新变量分配返回值?

当我用方法getIP()cngDB()修改实例变量RDBFRIPF,然后在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) 个答案