即使在不同的计算机中,我如何能够自动将BufferReader Path定向到桌面。。'''BufferedReader in=新的BufferedReader(新的FileReader("C:\Users\….\".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中,我查看
HKCUSoftwareMicrosoftWindowsCurrentVersionExplorerShell Folders
中的DESKTOP
注册表项 - 在Linux中,我扫描纯文本文件$XFG_CONFIG_HOME/user-dirs.dirs,查找类似外壳的
XDG_DESKTOP_DIR
变量定义 - 在其他系统上,或者如果以上操作失败,请回退到用户主目录中的
Desktop
目录
我的实现如下:
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())) {
// ...
}