2017-03-29 18 views
0

我對Java很新,而且遇到了這個問題。如果它不存在,我想讓java代碼生成一個txt文件,但如果它確實存在,我希望PrintWriter使用FileWriter將其附加到它。這裏是我的代碼:Java - 有沒有辦法打開一個文本文件,如果它不存在,並附加到它,如果它確實存在?

編輯:我試圖解決我的代碼,但現在我得到IOException錯誤。我在這裏做錯了什麼? 編輯2:我認爲我的代碼是獨一無二的,因爲我試圖在文件不存在的情況下創建一個新文件,並且在現有文件已經存在的情況下添加它。

import java.util.Scanner; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.BufferedWriter; 
import java.io.IOException; 

/** 
* Created by FakeOwl96 on 3/28/2017. 
*/ 
public class AreaOfCircle { 
    private static double PI = Math.PI; 
    private double radius; 
    private static double area; 
    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 
     AreaOfCircle a = new AreaOfCircle(); 
     System.out.print("Type in the radius of circle: "); 
     a.radius = keyboard.nextDouble(); 
     getArea(a.radius); 
     System.out.print("Name of the txt file you want to create:"); 
     String fileName = keyboard.nextLine(); 
     keyboard.nextLine(); 
     try { 
      File myFile = new File(fileName); 
      if (!myFile.exists()) { 
       myFile.createNewFile(); 
      } 
      FileWriter fw = new FileWriter(myFile, true); 
      BufferedWriter bw = new BufferedWriter(fw); 
      bw.write("The area of the circle is " + area + ".\n"); 
      bw.close(); 
     } 
     catch (IOException e) { 
      System.out.println("IOException Occured"); 
      e.printStackTrace(); 
     } 
    } 

    public static void getArea(double n) { 
     area = n * PI; 
    } 
} 
+0

[如何將文本追加到Java中的現有文件]可能的重複(http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java ) –

+0

謝謝,但我不這麼認爲,因爲我試圖讓代碼創建txt文件,如果它不存在。 – FakeOwl96

+0

鏈接的問題提供了很多答案,還包含缺失文件的創建。如果仔細觀察,你會發現,這個問題上的很多答案與鏈接問題中的答案相同。這就是爲什麼我提出這個問題,因爲__possible__重複。 –

回答

0
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.util.Scanner; 

public class AreaOfCircle { 
    private static double PI = Math.PI; 
    private double radius; 
    private static double area; 
    public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 
     AreaOfCircle a = new AreaOfCircle(); 
     System.out.print("Type in the radius of circle: "); 
     a.radius = keyboard.nextDouble(); 
     getArea(a.radius); 
     System.out.print("Name of the txt file you want to create:"); 
     String fileName = keyboard.next(); 
     keyboard.nextLine(); 
     try { 
      File myFile = new File(fileName); 
      if (!myFile.exists()) { 
       myFile.createNewFile(); 
      } 
      FileWriter fw = new FileWriter(myFile, true); 
      BufferedWriter bw = new BufferedWriter(fw); 
      bw.write("The area of the circle is " + area + ".\n"); 
      bw.close(); 
     } 
     catch (IOException e) { 
      System.out.println("IOException Occured"); 
      e.printStackTrace(); 
     } 
    } 

    public static void getArea(double n) { 
     area = n * PI; 
    } 
} 

我所做的唯一變化是 字符串文件名= keyboard.next();來自//keyboard.nextLine() 上面的代碼適用於我。希望這可以幫助。

+0

謝謝。將keyboard.nextLine()更改爲keyboard.next()後,它也適用於我。你能提供一個簡短的解釋,爲什麼nextLine()觸發IOException? – FakeOwl96

+0

nextLine()使此掃描器前進到當前行並返回被跳過的輸入。 https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html#nextLine-- 使用keyboard.nextLine()使fileName爲空,因此它試圖創建一個空的文件如果(!myFile.exists()){myFile.createNewFile();} 這是造成IOException。 –

+0

@ FakeOwl96你有任何其他問題。如果你對我的解釋感到滿意,你介意接受我的答案嗎? –

0

添加以下初始化myFile後線:

myFile.createNewFile(); // if file already exists will do nothing 
0

這是文件附加行的例子,並創建新的文件,如果文件不存在。

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
public class AppendFileDemo { 

    public static void main(String[] args) { 
     try { 
      String content = "This is my content which would be appended " 
        + "at the end of the specified file"; 
      //Specify the file name and path here 
      File file = new File("myfile.txt"); 

      /* This logic is to create the file if the 
     * file is not already present 
      */ 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 

      //Here true is to append the content to file 
      FileWriter fw = new FileWriter(file, true); 
      //BufferedWriter writer give better performance 
      BufferedWriter bw = new BufferedWriter(fw); 
      bw.write(content); 
      //Closing BufferedWriter Stream 
      bw.close(); 

      System.out.println("Data successfully appended at the end of file"); 

     } catch (IOException ioe) { 
      System.out.println("Exception occurred:"); 
      ioe.printStackTrace(); 
     } 
    } 
} 
+0

非常感謝你!順便說一下,BufferedWriter比PrintWriter更好嗎?我應該開始使用BufferedWriter而不是始終使用PrintWriter嗎? – FakeOwl96

+0

此外,我試圖通過您的解決方案修復我的代碼,現在我得到了IOException。我究竟做錯了什麼? – FakeOwl96

0

這是文件追加行的另一個示例,如果文件不存在,則創建新文件。

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
public class AppendFileDemo2 { 

    public static void main(String[] args) { 
     try { 
      File file = new File("myfile2.txt"); 
      if (!file.exists()) { 
       file.createNewFile(); 
      } 
      FileWriter fw = new FileWriter(file, true); 
      BufferedWriter bw = new BufferedWriter(fw); 
      PrintWriter pw = new PrintWriter(bw); 
      //This will add a new line to the file content 
      pw.println(""); 
      /* Below three statements would add three 
      * mentioned Strings to the file in new lines. 
      */ 
      pw.println("This is first line"); 
      pw.println("This is the second line"); 
      pw.println("This is third line"); 
      pw.close(); 

      System.out.println("Data successfully appended at the end of file"); 

     } catch (IOException ioe) { 
      System.out.println("Exception occurred:"); 
      ioe.printStackTrace(); 
     } 
    } 
} 
相關問題