Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中键入setProperty的全半角冒号(:)_Java_Escaping - Fatal编程技术网

在Java中键入setProperty的全半角冒号(:)

在Java中键入setProperty的全半角冒号(:),java,escaping,Java,Escaping,我正在尝试设置我的数据库属性,以便将来用户如果想要更改数据库的路由,可以对其进行更改。问题是,当我尝试设置带有全宽冒号的内容时,它总是添加反斜杠转义字符 我试过正常和双重逃跑,但都没用 import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class SetProps { public stati

我正在尝试设置我的数据库属性,以便将来用户如果想要更改数据库的路由,可以对其进行更改。问题是,当我尝试设置带有全宽冒号的内容时,它总是添加反斜杠转义字符

我试过正常和双重逃跑,但都没用

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class SetProps {

public static void SetDefaultProps(){
    Properties prop = new Properties();
OutputStream output = null;

try {

    output = new FileOutputStream("./build/classes/configuracion.properties");

    // Set the database property
    prop.setProperty("url", "jdbc:mysql://192.168.1.192:3306/ordenestaller");


// Save Properties
    prop.store(output, null);

} catch (IOException io) {
    io.printStackTrace();
} finally {
    if (output != null) {
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
}
public static void main(String[] args) {
        SetDefaultProps();
    }
}

在java代码中硬编码db url不是一个好的编码实践。 我建议您将这些URL放在属性文件中,并在需要时使用ResourceBundle读取它们

您可以在类路径中的某个位置拥有information.properties文件,该文件的内容可以是

dbURL:jdbc:mysql://192.168.1.192:3306/ordenestaller
现在,在您的代码中,您可以使用ResourceBundle获得此信息

ResourceBundle bundle = ResourceBundle.getBundle("information");
String dbURL= bundle.getString("dbURL");
这将有一个额外的优势,即如果DB URl被更改,您将不需要再次重新编译java类


希望它有帮助

您不需要转义mySQL URL中的任何内容,冒号是一个常规字符函数setProperty始终添加到:反斜杠/:Ist不是“setProperty”方法。这是存储方法,将添加//你给我点@从这里得到的解决方案是:反斜杠没有问题,属性文件考虑全宽度冒号作为特殊字符,所以它是需要的。此外,properties.load返回的信息没有反斜杠。感谢您的帮助非常感谢,但我需要键入全半角冒号,因为我还要设置目录路径以及C:/Documents和Settings。。。。再次增加齿隙C\:/