我想将我想用GUI打开的文件目录存储到记事本中。我能够正确读取文件,但是当我想引用它时,我无法打开文件 我的代码:
File desktop = new File(System.getProperty("user.home"), "Desktop");
File sharepoint = new File(System.getProperty("user.home"), "SharePoint");
try {
File file= new File("C:/Users/user/Desktop/File.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
StringBuffer str = new StringBuffer();
String line;
while((line=br.readLine())!=null) {
str.append(line);
str.append("\n");
}
fr.close();
// Desktop.getDesktop().open(new File(sharepoint + "/Open Project Transition Check - Doc/Project Status Summary/a.xlsb"));
//Scanner scan = new Scanner(file);
System.out.println(str.toString());
Desktop.getDesktop().open(new File(str.toString()));
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Please check that you have SharePoint access, contact your admin for more assistance", "Access error", JOptionPane.WARNING_MESSAGE);
// Logger.getLogger(HPform.class.getName()).log(Level.SEVERE, null, ex);
}
答案 0 :(得分:0)
这是你的问题
while((line=br.readLine())!=null){
str.append(line);
str.append("\n"); <-- what this new line for? if it's not used, delete it
}
即使你的txt文件只有一个文件。问题仍然存在,因为这不是打开文件的有效参数(您的最终结果将是C:\Users\user\SharePoint\Open Project Transition Check - Doc\Project Status Summary\a.xlsb\n
)