これを使用して、ホームディレクトリに3つの空のファイルを作成しようとしました:
this.mainpath = System.getenv("HOME"+"/");
this.single = new File(mainpath + "sin.r");
this.complete = new File (mainpath + "com.r");
this.ward = new File (mainpath+"w.r");
これで必要なファイルが得られるという印象を受けました。しかし、ホーム ディレクトリまたは他のディレクトリでこのファイルを検索しても、どれも存在しません。私は何を間違っていますか?
編集: ファイルを取得しますが、ホーム ディレクトリにはありませんが、ファイルへのパスは /home/myname/NetBeansProjects/myorojectname/nullsin.r になります。
ただし、特に自宅でファイルを作成したかったのです。
さて、私のコードは次のようになります。
this.mainpath = System.getenv("user.home");
this.mainpath = this.mainpath + "/";
this.single = new File(mainpath + "sin.r");
this.single.createNewFile();
System.out.println(this.single.getAbsolutePath());
this.complete = new File (mainpath + "comp.r");
this.complete.createNewFile();
this.ward = new File (mainpath+"w.r");
this.ward.createNewFile();
ただし、これの「成功」は、最初の createNeWFile(): File not found で IOException が発生することです。
これらのファイルに sth を書き込もうとしたコードについては、次のとおりです。
FileWriter writer1 = null;
FileWriter writer2 = null;
FileWriter writer3 = null;
try {
writer1 = new FileWriter(single);
writer2 = new FileWriter(complete);
writer3 = new FileWriter(ward);
writer1.write("x = cbind(1,2,3)");
writer2.write("x = cbind(1,2,3)");
writer3.write("x = cbind(1,2,3)");
writer1.flush();
writer2.flush();
writer3.flush();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
} finally {
try {
writer1.close();
writer2.close();
writer3.close();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}