如何从 .ini 文件中读取路径
How can I read a path from an .ini file
我想导入 Java 中的文件路径。由于路径可以改变,我希望它在代码之外,所以它是可变的。我读过可以用 INI 文件解决。好吧,我试过了。我有以下 Java 代码:
import java.util.*;
import java.io.*;
class readIni {
public static void main(String args[]) {
readIni ini = new readIni();
ini.doit();
}
public void doit() {
try{
Properties p = new Properties();
p.load(new FileInputStream("user.ini"));
p.list(System.out);
}
catch (Exception e) {
System.out.println(e);
}
}
}
我的 Ini 文件:
file = H:/
现在,控制台准确地显示了 Ini 文件,而不是目录的内容....怎么了?
如果您只想保存文件路径,请考虑使用以下代码:
File file = new File("H:\whatever.txt");
// Write to the file
FileWriter fw = new FileWriter(file);
fw.write("your path goes here");
fw.close();
// Read from the file
BufferedReader br = new BufferedReader(new FileReader(file));
String path = br.readLine();
br.close();
我想导入 Java 中的文件路径。由于路径可以改变,我希望它在代码之外,所以它是可变的。我读过可以用 INI 文件解决。好吧,我试过了。我有以下 Java 代码:
import java.util.*;
import java.io.*;
class readIni {
public static void main(String args[]) {
readIni ini = new readIni();
ini.doit();
}
public void doit() {
try{
Properties p = new Properties();
p.load(new FileInputStream("user.ini"));
p.list(System.out);
}
catch (Exception e) {
System.out.println(e);
}
}
}
我的 Ini 文件:
file = H:/
现在,控制台准确地显示了 Ini 文件,而不是目录的内容....怎么了?
如果您只想保存文件路径,请考虑使用以下代码:
File file = new File("H:\whatever.txt");
// Write to the file
FileWriter fw = new FileWriter(file);
fw.write("your path goes here");
fw.close();
// Read from the file
BufferedReader br = new BufferedReader(new FileReader(file));
String path = br.readLine();
br.close();