ホームページ >Java >&#&チュートリアル >Java の FileWriter
Java の FileWriter を使用してファイルを作成し、作成したファイルに文字を書き込むことができます。出力ストリーム クラスは、FileWriter クラスから継承されているため、FileWriter クラスの基本クラスであり、このクラスのコンストラクターによって行われる仮定は、デフォルトの文字エンコーディングとデフォルトのバイト バッファ サイズが、これらの値が指定される場合に許可されるというものです。出力ストリーム ライターは、ファイル出力ストリーム上に構築する必要があります。
Java FileWriter クラスの宣言:
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
Java.lang.Object Java.io.Writer Java.io.OutputStreamWriter Java.io.FileWriter
Java の FileWriter クラスは、いくつかのコンストラクターで構成されています。それらは次のとおりです:
Java の FileWriter クラスは、次のようないくつかのメソッドで構成されます。
以下は Java で FileWriter を実装する例です:
FileWriter クラスの作成をデモする Java プログラム。
コード:
import java.io.*; public class Read { public static void main(String args[])throws IOException { File file1 = new File("check.txt"); // A file is created file1.createNewFile(); // Object of FileWriter is created FileWriter writer1 = new FileWriter(file1); // Contents are written to the file writer1.write("Welcome to FileWriter"); writer1.flush(); writer1.close(); // Object of filereader is created FileReader read = new FileReader(file1); char [] a1 = new char[50]; read.read(a1); // array contents are read for(char ch : a1) System.out.print(ch); // characters are printed one by one read.close(); } }
出力:
FileWriter クラスを使用してテキスト ファイルを作成する Java プログラム。
コード:
import java.io.*; public class Example { public static void main(String[] args) { //File constructor is initialized File file1 = new File("C:/Users/shivakumarsh/Desktop/Learning/source.txt"); try { boolean create = file1.createNewFile(); if (create) { System.out.println("File creation is succesful."); }else { System.out.println("There is already a file by this name."); } } catch (IOException e) { e.printStackTrace(); } } }
ファイルの末尾に文字列を追加する方法を示す Java プログラム。
コード:
import java.io.*; public class Example { public static void append(String fileName, String string) { try { // The file is opened in append mode BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName, true)); out1.write(string); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } } public static void main(String[] args) throws Exception { // A sample file is created with some text containing in it String fileName = "Shobha.txt"; try { BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName)); out1.write("Learning appending\n"); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } // The newly created file is appended with the string value passed here String string = "Learning is good"; append(fileName, string); // contents of the modified file are printed here try { BufferedReader in1 = new BufferedReader( new FileReader("Shobha.txt")); String mystr; while ((mystr = in1.readLine()) != null) { System.out.println(mystr); } } catch (IOException e) { System.out.println("Occurance of exception" + e); } } }
出力:
FileWriter を使用してファイルに 1 行ずつ書き込む Java プログラム。
コード:
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class List { public static void main(String[] args) throws Exception { ArrayList<string> list1 = new ArrayList<string>(); // The newly created file is appended with the string value passed here list1.add("Understanding an example in java"); list1.add("second line of the file"); list1.add("third line of the file"); writeFile("C:/Users/shivakumarsh/Desktop/Learning/source.txt", list1); System.out.println("file creation is successful"); } public static void write(String fileName, List<string> list1) throws Exception { FileWriter fwd = null; BufferedWriter bwd = null; try { fwd = new BufferedWriter(fwd); for (int j = 0; list1 != null && j <p><strong>出力:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500344564251.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java の FileWriter" ></p> <h3>結論</h3> <p>このチュートリアルでは、FileWriter の定義、FileWriter の宣言方法、FileWriter のコンストラクター、FileWriter クラスの作成、FileWriter クラスを使用したテキスト ファイルの作成、およびFileWriter クラスを使用してコンテンツで構成される既存のファイルにテキストを追加します。FileWriter クラスを使用して新しいファイルに 1 行ずつ書き込みます。つまり、コンテンツを 1 行ずつ追加します。</p> <h3>おすすめ記事</h3> <p>これは Java の FileWriter のガイドです。ここでは、FileWriter、FileWriter のコンストラクター、およびそのメソッドを宣言する方法とコード実装について説明します。詳細については、他の推奨記事を参照することもできます –</p> <ol> <li>Java のレイアウト</li> <li>Java コンパイラー</li> <li>Java でのマージソート</li> <li>Java パラレル ストリーム</li> </ol></string></string></string>
以上がJava の FileWriterの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。