Simple Java implementation of a read/write text file example _java

Source: Internet
Author: User
Tags readline stringbuffer
/*
* Example of a simple read/write text file
* Here are three examples of
* 1. Example of reading a file into memory (here is StringBuffer)
* 2. Write text in content to a file
* 3. Read the contents of one file and write it to another file
* It also shows that if the content is read from the input stream to the output stream (text flow only)
* Three examples can exist independently, so just look at one of them on demand.
*/

Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.PrintStream;
Import Java.io.PrintWriter;

Public final class Accesstextfile {

/**
* 1. Demonstrates reading text in a stream into a stringbuffer
* @throws IOException
*/
public void Readtobuffer (StringBuffer buffer, InputStream is)
Throws IOException {
String Line; Used to hold content read per row
BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
line = Reader.readline (); Read the first line
while (line!= null) {//If line is empty description finished reading
Buffer.append (line); Add read content to buffer
Buffer.append ("\ n"); Add line break
line = Reader.readline (); Read Next line
}
}

/**
* 2. Demonstrates reading the contents of a stringbuffer into a stream
*/
public void Writefrombuffer (stringbuffer buffer, OutputStream OS) {
The PrintStream can be used to output the content to the output stream conveniently.
The use of its objects is the same as System.out
(System.out itself is the PrintStream object)
PrintStream PS = new PrintStream (OS);
Ps.print (Buffer.tostring ());
}

/**
* 3*. Copy content from the input stream into the input stream
* @throws IOException
*/
public void CopyStream (InputStream is, OutputStream os) throws IOException {
This read process can refer to the comments in Readtobuffer
String Line;
BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
PrintWriter writer = new PrintWriter (new OutputStreamWriter (OS));
line = Reader.readline ();
while (line!= null) {
Writer.println (line);
line = Reader.readline ();
}
Writer.flush (); Finally, I'm going to write out what's in the output stream.
The writer is not closed here because the OS is coming in from the outside.
Since it's not opened from here, it's not closed from here.
If the writer is closed, the OS that is encapsulated inside is closed.
}

/**
* 3. Call the CopyStream (InputStream, OutputStream) method to copy a text file
*/
public void Copytextfile (string infilename, String outfilename)
Throws IOException {
Generate the corresponding input/output stream based on the input/output file first
InputStream is = new FileInputStream (infilename);
OutputStream OS = new FileOutputStream (outfilename);
CopyStream (is, OS); Copy content with CopyStream
Is.close (); is open here, so you need to close
Os.close (); The OS is open here, so it needs to be turned off
}

public static void Main (string[] args) throws IOException {
int SW = 1; Selection switches for three kinds of tests
Accesstextfile test = new Accesstextfile ();

Switch (SW) {
Case 1://test Read
{
InputStream is = new FileInputStream ("E:\\test.txt");
StringBuffer buffer = new StringBuffer ();
Test.readtobuffer (buffer, is);
SYSTEM.OUT.PRINTLN (buffer); Write out what you read in the buffer.
Is.close ();
Break
}
Case 2://Test Write
{
StringBuffer buffer = new StringBuffer ("Only a test\n");
Test.writefrombuffer (buffer, system.out);
Break
}
Case 3://Test copy
{
Test.copytextfile ("E:\\test.txt", "e:\\r.txt");
}
Break
}
}

}

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.