java中直接到桌面的绝对路径,即使在不同的计算机中也是如此
我如何能够自动将BufferReader路径定向到桌面,即使是在不同的计算机中。。 ''BufferedReader in=new BufferedReader(新文件读取器(“C:\Users\…\”.txt”);''您可以使用java中直接到桌面的绝对路径,即使在不同的计算机中也是如此,java,bufferedreader,bufferedwriter,Java,Bufferedreader,Bufferedwriter,我如何能够自动将BufferReader路径定向到桌面,即使是在不同的计算机中。。 ''BufferedReader in=new BufferedReader(新文件读取器(“C:\Users\…\”.txt”);''您可以使用 public static void main(String[] args) throws IOException { String path = System.getProperty("user.home") + "/Desktop/test.txt";
public static void main(String[] args) throws IOException {
String path = System.getProperty("user.home") + "/Desktop/test.txt";
BufferedReader in = new BufferedReader(new FileReader(path));
String line;
while ((line = in.readLine()) != null) {
System.out.println("line = " + line);
}
in.close();
}
获取桌面路径的步骤
然后从中读取文本文件以下是获取桌面目录的方法:
- 在Windows中,我查看
HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell文件夹中的
注册表项DESKTOP
- 在Linux中,我扫描纯文本文件$XFG\u CONFIG\u HOME/user-dirs.dirs,查找
变量的类似shell的定义XDG\u DESKTOP\u DIR
- 在其他系统上,或者如果上述操作失败,请返回用户主目录中的
目录桌面
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.regex.Pattern;
public class DesktopDirectoryLocator {
public static Path getDesktopDirectory()
throws IOException,
InterruptedException {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
String dir = null;
ProcessBuilder builder = new ProcessBuilder("reg.exe", "query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
+ "\\Shell Folders", "/v", "DESKTOP");
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process regQuery = builder.start();
try (BufferedReader outputReader = new BufferedReader(
new InputStreamReader(regQuery.getInputStream()))) {
String line;
while ((line = outputReader.readLine()) != null) {
if (dir == null) {
String replaced =
line.replaceFirst("^\\s*DESKTOP\\s+REG_SZ\\s+", "");
if (!replaced.equals(line)) {
dir = replaced.trim();
}
}
}
}
int exitCode = regQuery.waitFor();
if (exitCode != 0) {
throw new IOException(
"Command " + builder.command() + " returned " + exitCode);
}
if (dir != null) {
return Paths.get(dir);
}
} else if (os.contains("Linux")) {
// Reference:
// https://www.freedesktop.org/wiki/Software/xdg-user-dirs/
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Path configDir;
String configHome = System.getenv("XDG_CONFIG_HOME");
if (configHome != null) {
configDir = Paths.get(configHome);
} else {
configDir = Paths.get(
System.getProperty("user.home"), ".config");
}
Path userDirsFile = configDir.resolve("user-dirs.dirs");
if (Files.isRegularFile(userDirsFile)) {
try (BufferedReader userDirs =
Files.newBufferedReader(userDirsFile)) {
String line;
while ((line = userDirs.readLine()) != null) {
if (line.trim().startsWith("XDG_DESKTOP_DIR=")) {
String value =
line.substring(line.indexOf('=') + 1);
if (value.startsWith("\"") &&
value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
Pattern varPattern = Pattern.compile(
"\\$(\\{[^}]+\\}|[a-zA-Z0-9_]+)");
value = varPattern.matcher(value).replaceAll(r -> {
String varName = r.group(1);
if (varName.startsWith("{")) {
varName = varName.substring(1,
varName.length() - 1);
}
String varValue = System.getenv(varName);
return (varValue != null ?
varValue : r.group());
});
return Paths.get(value);
}
}
}
}
}
return Paths.get(System.getProperty("user.home"), "Desktop");
}
public static void main(String[] args)
throws IOException,
InterruptedException {
System.out.println(getDesktopDirectory());
}
}
要读取该目录中的文件,请使用:
Path desktopDir = getDesktopDirectory();
Path file = desktopDir.resolve("example.txt");
try (BufferedReader reader = Files.newBufferedReader(file,
Charset.defaultCharset())) {
// ...
}
如何在bufferReader((System.getProperty(“user.home”)+“/Desktop”+…txt))上应用它似乎不起作用…对于这样一个noob问题,我要用完整的代码片段编辑答案