Programming Tutorials

(追記) (追記ここまで)

Write to a file in Java - Sample Program

By: Dorris in Java Tutorials on 2010年12月27日 [フレーム]

This sample Java program demonstrates how to write to a file in Java. For this, the following two classes FileWriter and BufferedWriter are used. This program can be used to write to any text file using a Java Program.

FileWriter

The FileWriter is a class used for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream.

BufferedWriter

The BufferWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

import java.io.*;
class FileWrite 
{
 public static void main(String args[])
 {
 try{
 // Create file 
 FileWriter fstream = new FileWriter("out.txt");
 BufferedWriter out = new BufferedWriter(fstream);
 out.write("Hello Java");
 //Close the output stream
 out.close();
 }catch (Exception e){//Catch exception if any
 System.err.println("Error: " + e.getMessage());
 }
 }
}



(追記) (追記ここまで)


Add Comment

JavaScript must be enabled for certain features to work
* Required information
1000

Comments

No comments yet. Be the first!
(追記) (追記ここまで)
(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /