Java Programming Document read-write example detailed _java

Source: Internet
Author: User
Tags flush

This article gives an example of how to read and write files in Java programming. Share to everyone for your reference, specific as follows:

What is the function of file reading and writing in Java?

The answer to this question should be the first to think of Java is just a language, one of our use tools, so the answer is clear, is to write a variety of foreign data to a file to save it, or from the file to read out its data for our use. In the following movie process, download a movie from a Web resource in your computer (write a file) and use the player to open it when you want to see it (read the file).

How to read and write files in Java?

First of all, the Java flow is divided into two, byte stream and character streams, where the two base of the byte stream is InputStream and outputstream; the two base classes for character streams are reader and writer. The so-called file flow, that is, we do not leave the operation of the file flow. So we're going to use one of the four base classes that a class must inherit. Everything in Java is a class, and everything is an object. It's natural to think about the types of file operations:

The following four classes are used directly:

BYTE stream: FileInputStream and FileOutputStream
Character stream: FileReader and FileWriter

It's good to find a class. The rest is to find the way to achieve.

There are two options here, which involves how do we choose the right way to read and write files?

Select the difference between the criteria:

Read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files.
Reads a file in characters, often used to read text, numbers, and other types of files.
As for whether to choose to use the buffer to the file input and output stream encapsulation, it is necessary to look at the size of the file, if the large file read and write, then choose the bucket buffer to provide file reading and writing efficiency.

The following are examples of simple applications:

1. Use byte stream to read and write files directly:

Note: FileOutputStream (file, True); The true parameter indicates that the original file is not overwritten, and the add content is appended directly to the file.

public class Filetest
{
static file = new file ("D:/test.txt");
public static void Main (string[] args
} {
try
{
FileOutputStream out = new FileOutputStream (file, true);
String s = "hello,world!\r\n";
Out.write (S.getbytes ());
Out.flush ();
Out.close ();
FileInputStream in = new FileInputStream (file);
byte [] b = new BYTE[20];
In.read (b, 0, b.length);
System.out.println (New String (b));
In.close ();
} catch (FileNotFoundException e)
{
e.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();}}


2. Use character streams to read and write files directly:

public class File03
{
static file = new file ("D:/test.txt");
public static void Main (string[] args)
{
try
{
FileWriter fw = new FileWriter (file,true);
Fw.write ("hello,world!\r\n");
Fw.flush ();
Fw.close ();
FileReader FR = new FileReader (file);
int i=0;
String s = "";
while ((i = Fr.read ())!=-1)
//{
//s = s + (char) i;
}
//system.out.println (s);
} catch (FileNotFoundException e)
{
e.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();}}


Use of buffer package for file read and write streaming:

1, the byte throttling package after the file to read and write:

Static file File = new file ("D:/test.txt");
public static void Main (string[] args)
{
try
{
//FileOutputStream out = new FileOutputStream (file, True );
Bufferedoutputstream bout = new Bufferedoutputstream (out);
String s = "I have a dream!";
Bout.write (S.getbytes ());
Bout.flush ();
Bout.close ();
FileInputStream in = new FileInputStream (file);
Bufferedinputstream bin = new Bufferedinputstream (in);
Byte[] B = new byte[15];
Bin.read (b);
Bin.close ();
System.out.println (New String (b));
catch (FileNotFoundException e)
{
e.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();}}


2, the character stream package after the file to read and write:

public class File03
{
static file = new file ("D:/test.txt");
public static void Main (string[] args)
{
try
{
//FileWriter FW = new FileWriter (file, true);
BufferedWriter bw = new BufferedWriter (FW);
String nextline = System.getproperty ("Line.separator");
Bw.write ("hello,world!" + nextline);
Bw.flush ();
Bw.close ();
FileReader FR = new FileReader (file);
BufferedReader br = new BufferedReader (FR);
int i = 0;
String s = "";
String temp = null;
while ((Temp=br.readline ())!=null)
{
s = s+temp;
}
System.out.println (s);
} catch (FileNotFoundException e)
{
e.printstacktrace ();
} catch (IOException e)
{
E.printstacktrace ();}}


I hope this article will help you with Java programming.

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.