1

これを使用して、ホームディレクトリに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());

        }
4

2 に答える 2

3

getProperty()代わりに使用する必要があります

System.getProperty("user.home");

また、/ディレクトリ パスを取得した後に を追加する必要があります。

this.mainpath = System.getProperty("user.home");
this.single = new File(mainpath + "/sin.r");
this.complete = new File (mainpath + "/com.r");
this.ward = new File (mainpath+"/w.r");
于 2013-09-08T10:13:27.757 に答える
1

宣言したオブジェクトごとに「createNewFile」メソッドを呼び出して、実際に作成することができます。

于 2013-09-08T10:15:37.323 に答える