从Text文件中读取两行并将它们放在一个字符串中

时间:2016-08-25 06:15:45

标签: java string file

您好我想从文本文件中读取2行并将它们放在2个不同的字符串中,以便我可以在与DB的某些连接中使用该字符串。我想阅读字符串1中的oracle.jdbc.OracleDriver和字符串2中的jdbc:oracle:thin:@localhost:1521/XEsatdalalsatdalal

try{
          File file = new File("D:\\WalletManagementSystem\\WalletManagementSystem\\config.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuffer stringBuffer = new StringBuffer();
    String line;
        String line1;
    while ((line = bufferedReader.readLine()) == null) {
        stringBuffer.append(line);
        stringBuffer.append("\n");
        }
        fileReader.close();

          //Class.forName("oracle.jdbc.OracleDriver");
            Class.forName(/*String 1 to come here*/);
            Connection conn = DriverManager.getConnection(/*String 2 to come here*/);
          //Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","satdalal","satdalal");
          return conn;

      } 
      catch (Exception e){
          JOptionPane.showMessageDialog(null, e);
          return null;
}`

2 个答案:

答案 0 :(得分:0)

为什么不创建属性文件并在其中放入2行,每行分隔为' equals'登录。

例如 -

DECLARE Hyphen;
SPECIAL.ct == "-"{-> Hyphen};

DECLARE HyphenizationWord, PreHyphenizationWords, PosHyphenixationWords;
DECLARE HyphenizationWord ThreeHyphenizationWord;

(W @Hyphen{-PARTOF(HyphenizationWord)} W Hyphen W){-> ThreeHyphenizationWord};
(W{-> PreHyphenizationWords} @Hyphen{-PARTOF(HyphenizationWord)} W{-> PosHyphenixationWords}){-> HyphenizationWord};

STRINGLIST hyphenizationWordList;
STRING mt;
HyphenizationWord{-> MATCHEDTEXT(mt), ADD(hyphenizationWordList, replaceAll(mt, "[- ]", ""))};

DECLARE ComplexWord;
MARKFAST(ComplexWord,hyphenizationWordList);

尝试 - https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

drivername=oracle.jdbc.OracleDrive
url="jdbc:oracle:thin:@localhost:1521/XE","satdalal","satdalal"

答案 1 :(得分:0)

你可以导入java.util.Properties。

Env.class:


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by light on 2016/6/17.
 */
public class Env {
    private static Properties prop = new Properties();
    private static Env instance = new Env();

    private Env(){
        InputStream in = this.getClass().getResourceAsStream("/application.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Env getInstance() {
        return instance;
    }

    public String getProperty(String key){
        return (String) prop.get(key);
    }
}
你应该像这样设置

和application.properties:

url = jdbc:oracle:thin:@10.80.0.189:1521:dhybzxztk
name = oracle.jdbc.driver.OracleDriver
user = *****
password = *****

使用上述连接信息时,可以使用以下方法:

Env env =  Env.getInstance();
String url = env.getProperty("url");
String name = env.getProperty("name");
String user = env.getProperty("user");
String password = env.getProperty("password");
相关问题