java - 如何使用 Java 的文件编写器定向到多台计算机上的公共桌面目录
问题描述
对于这个程序,我使用的是 FileWriter,但是我的程序的要求是能够将文件输出到用户的桌面,并提供所需的信息。这就是我卡住的地方。我不确定如何自动获取该目录,因此用户不必输入任何信息。我已经查看了有关此问题的多个线程,但它们似乎不起作用,除非我做错了。以下是线程的链接供参考: 写入新文件时自动创建整个路径 Java 如何从任何用户的桌面读取/写入文件
这是代码
public static void SortLast(Student[] mystudent, int line) throws IOException {
int StudentId;
int Grade;
String LastName;
String FirstName;
String Gender;
FileWriter creator = new FileWriter("C:\\Users\\JmcIntyre1094\\Desktop\\LastNameSort.txt");
for (int i = 0; i < line; i++) {
StudentId = mystudent[i].StudentId;
Grade = mystudent[i].Grade;
LastName = mystudent[i].LastName;
FirstName = mystudent[i].FirstName;
Gender = mystudent[i].Gender;
creator.write( StudentId + " " + Grade + " " + LastName + " " + FirstName + " " + Gender);
creator.write(System.getProperty("line.separator"));
}//printing out the lines in the new file
creator.close();
}//Creating the file