Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

filewriter - How to direct to a common desktop directory on multiple computers using Java's File Writer

for this program I am using the FileWriter, however a requirement for my program is to be able to output a file to a user's desktop with the required information. This is where I get stuck. I'm not sure how to obtain that directory automatically so the user doesn't have to input any information. I've already looked at multiple threads regarding this problem but they don't seem to work, unless im doing it incorrectly. For reference here are the links to the threads: Create whole path automatically when writing to a new file Java How to read/write files to/from any user's desktop

Here is the code

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...