Java File Write

Source: Internet
Author: User

One, Filewritter write file

Filewritter, character Stream writes characters to a file. By default, it replaces all existing content with the new content, however, when you specify a true (Boolean) value as the second parameter of the Filewritter constructor, it preserves the existing content and appends the new content to the end of the file.

1. Replace all existing content with new content.

New FileWriter (file);

2. Keep the existing content and append the new content at the end of the file.

New FileWriter (file,true);

Append file Example
A text file that is named "Javaio-appendfile.txt" and contains the following content.

ABC Hello append new content FileWriter (file,true)

Copy CodeThe code is as follows:
Package com.yiibai.file;

Import Java.io.File;
Import Java.io.FileWriter;
Import Java.io.BufferedWriter;
Import java.io.IOException;

public class Appendtofileexample
{
public static void Main (string[] args)
{
try{
String data = "This content would append to the end of the file";

File file =new file ("Javaio-appendfile.txt");

If file doesnt exists, then create it
if (!file.exists ()) {
File.createnewfile ();
}

true = Append file
FileWriter filewritter = new FileWriter (File.getname (), true);
BufferedWriter bufferwritter = new BufferedWriter (filewritter);
Bufferwritter.write (data);
Bufferwritter.close ();

System.out.println ("Done");

}catch (IOException e) {
E.printstacktrace ();
}
}
}

Results
The text file "Javaio-appendfile.txt" content is now updated as follows:

ABC Hello This content would append to the end of the file


Two, bufferedwriter write file

The buffered character (BufferedWriter) is a character stream class that handles the characters data. Unlike byte streams (which convert data into bytes), you can write strings, arrays, or character data to a file directly.

Copy CodeThe code is as follows:
Package com.yiibai.iofile;

Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileWriter;
Import java.io.IOException;

public class Writetofileexample {
public static void Main (string[] args) {
try {

String content = "The content to write into file";

File File = new file ("/users/mkyong/filename.txt");

If file doesnt exists, then create it
if (!file.exists ()) {
File.createnewfile ();
}

FileWriter FW = new FileWriter (File.getabsolutefile ());
BufferedWriter bw = new BufferedWriter (FW);
Bw.write (content);
Bw.close ();

System.out.println ("Done");

} catch (IOException e) {
E.printstacktrace ();
}
}
}

Three, FileOutputStream write file


The file output stream is a byte stream class used to process raw binary data. In order to write data to a file, the data must be converted to bytes and saved to a file. Please see the complete example below.

Copy CodeThe code is as follows:
Package Com.yiibai.io;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;

public class Writefileexample {
public static void Main (string[] args) {

FileOutputStream FOP = null;
File file;
String content = "This is the text content";

try {

File = new file ("C:/newfile.txt");
FOP = new FileOutputStream (file);

If file doesnt exists, then create it
if (!file.exists ()) {
File.createnewfile ();
}

Get the content in bytes
byte[] contentinbytes = Content.getbytes ();

Fop.write (contentinbytes);
Fop.flush ();
Fop.close ();

System.out.println ("Done");

} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
if (FOP! = null) {
Fop.close ();
}
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Updated JDK7 For example, use the new "Try resource off" method to easily work with files.

Package Com.yiibai.io;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;

public class Writefileexample {
public static void Main (string[] args) {

File File = new file ("C:/newfile.txt");
String content = "This is the text content";

Try (fileoutputstream fop = new FileOutputStream (file)) {

If file doesn ' t exists, then create it
if (!file.exists ()) {
File.createnewfile ();
}

Get the content in bytes
byte[] contentinbytes = Content.getbytes ();

Fop.write (contentinbytes);
Fop.flush ();
Fop.close ();

System.out.println ("Done");

} catch (IOException e) {
E.printstacktrace ();
}
}
}

Java File Write

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.