2017-03-29 10 views
0

私はJavaにはかなり慣れていないので、この問題が発生しました。 Javaコードが既に存在しない場合はtxtファイルを作成しますが、そうであれば、FileWriterを使用してPrintWriterを追加します。ここに私のコードです: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

[既存のファイルにテキストを追加する方法](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) ) –

+0

ありがとうございましたが、コードが既に存在しない場合は、txtファイルを作成しようとしているので、私はそう思わなかったのです。 – FakeOwl96

+0

リンクされた質問には多くの回答があり、欠落したファイルの作成も含まれています。よく見ると、この質問に関する多くの回答は、リンクされた質問の回答と同等であることがわかります。これが私がこの質問を__possible__ duplicateとして提案した理由です。 –

答えて

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; 
    } 
} 

Iが行わのみ変更が 文字列のファイル名である= keyboard.next()。 fromkeyboard.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が空になるため、空のファイルを作成しようとしていますファイルif(!myFile.exists()){myFile.createNewFile();} これはIOExceptionの原因です。 –

+0

@ FakeOwl96他に質問がありますか?あなたが私の説明に満足すれば、私の答えを受け入れることはできますか? –

0

myFileを初期化した後、次の行を追加します。

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

これは、ファイルのappend行の例であり、ファイルがない存在する場合は、新しいファイルを作成します。

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より優れている理由はありますか?私はいつもPrintWriterを使用するのではなく、BufferedWriterを使うべきでしょうか? – FakeOwl96

+0

また、あなたのソリューションでコードを修正しようとしましたが、今はIOExceptionが発生しています。私は間違って何をしていますか? – FakeOwl96

0

ファイル追加行のもう1つの例で、ファイルが存在しない場合は新しいファイルを作成します。

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(); 
     } 
    } 
} 
関連する問題