A specific example of Java IO streaming files _java

Source: Internet
Author: User
Tags readline wrapper

Introduction:

About Java IO flow is very common, basically every project will be used, each encounter is to find a search on the internet, and tried. Last time a colleague asked me to read a Java file, I suddenly Meng the first reaction is to go online to find, although also can find, but their total feeling is not very practical, so today took time to see some of the Java IO flow operation, feeling or very rewarding, incidentally summed up some information to facilitate further study ...

IO Stream Classification :
1, according to the flow of data objects to divide:
High-end streaming: All of the streams in memory are high-end streams, such as: InputStreamReader
Low-end Flow: All the external devices in the stream are low-end flow, such as Inputstream,outputstream
How to differentiate: All stream object suffixes contain reader or writer are high-end stream, conversely, is basically low-end stream, but there are exceptions, such as PrintStream is the high-end stream

2, according to the data flow to divide:
Output stream: is used to write data, by program (memory)---> External devices
Input stream: is used to read data, by external devices---> program (memory)
How to differentiate: Generally speaking, input stream with input, output stream with outputs

3, according to the format of the flow data:
A stream of binary data, such as a inputstream, that handles sounds or pictures.
A stream of text data (such as a TXT file), such as a inputstreamreader
How to differentiate: use high and low end streams to differentiate, all low-end streams are byte stream, all high-end streams are character streams

4, according to the flow of data packaging process to be divided:
Raw flow: In the process of instantiating an object of a stream, you do not need to pass in another stream as the parameter of the method you are constructing, called the original stream.
Wrapper flow: In the process of instantiating an object of a stream, it is necessary to pass in another stream as the stream of its own method of constructing the parameter, called the wrapper flow.
How to differentiate: so the low-end stream is the original stream, so the high-end stream is the wrapper flow

The inheritance relationship of Io Stream objects (figure below):

Let's look at some specific code examples:

Read files by byte

Copy Code code as follows:

public class ReadFromFile {
/**
* Read files in bytes, often used to read binary files, such as pictures, sounds, images, and other files.
*/
public static void Readfilebybytes (String fileName) {
File File = new file (fileName);
InputStream in = null;
try {
System.out.println (reads the contents of the file in bytes, one byte at a time: ");
Read one byte at a time
in = new FileInputStream (file);
int tempbyte;
while ((Tempbyte = In.read ())!=-1) {
System.out.print (Tempbyte);
}
In.close ();
catch (IOException e) {
E.printstacktrace ();
Return
}
try {
SYSTEM.OUT.PRINTLN (Read the contents of the file in bytes, read multiple bytes at a time: ");
Read multiple bytes at a time
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream (fileName);
Readfromfile.showavailablebytes (in);
Reads multiple bytes into a byte array, byteread the number of bytes read at a time
while ((Byteread = In.read (tempbytes))!=-1) {
System.out.print (tempbytes, 0, Byteread);
}
catch (Exception E1) {
E1.printstacktrace ();
finally {
if (in!= null) {
try {
In.close ();
catch (IOException E1) {
}
}
}
}

Read files by character

Copy Code code as follows:

/**
* Read files in characters, often used to read text, numbers, and other types of files
*/
public static void Readfilebychars (String fileName) {
File File = new file (fileName);
Reader reader = null;
try {
SYSTEM.OUT.PRINTLN (Read the contents of the file in characters, one character at a time: ");
Read one character at a time
reader = new InputStreamReader (new FileInputStream (file));
int Tempchar;
while ((Tempchar = Reader.read ())!=-1) {
For Windows, \ r \ n These two characters together represent a newline.
However, if the two characters are displayed separately, the lines will be changed two times.
So, block \ r, or block \ n. Otherwise, there will be a lot more empty lines.
if ((char) tempchar)!= ' \ R ') {
System.out.print ((char) tempchar);
}
}
Reader.close ();
catch (Exception e) {
E.printstacktrace ();
}
try {
System.out.println ("read the contents of the file in characters, read multiple characters at a time:");
Read multiple characters at a time
char[] Tempchars = new CHAR[30];
int charread = 0;
You need to set up a character stream because you want to read it in characters
reader = new InputStreamReader (fileName) (new FileInputStream);
Reads multiple characters into a character array, charread the number of characters read at a time
while ((Charread = Reader.read (tempchars))!=-1) {
Same shielding. \ r does not display
if ((Charread = = tempchars.length)
&& (tempchars[tempchars.length-1]!= ' \ R ')) {
System.out.print (Tempchars);
} else {
for (int i = 0; i < Charread; i++) {
if (tempchars[i] = = ' \ r ') {
Continue
} else {
System.out.print (Tempchars[i]);
}
}
}
}

catch (Exception E1) {
E1.printstacktrace ();
finally {
if (reader!= null) {
try {
Reader.close ();
catch (IOException E1) {
}
}
}
}

To read a file by row

Copy Code code as follows:

/**
* Read files in behavior units, often used to read line-oriented format files
*/
public static void Readfilebylines (String fileName) {
File File = new file (fileName);
BufferedReader reader = null;
try {
System.out.println ("read the contents of the file in a behavioral unit, read one whole line at a time:");
reader = new BufferedReader (new FileReader (file));
String tempstring = null;
int line = 1;
Read one line at a time until you read NULL to end the file
while ((tempstring = Reader.readline ())!= null) {
Show line Numbers
System.out.println ("line" + Line + ":" + tempstring);
line++;
}
Reader.close ();
catch (IOException e) {
E.printstacktrace ();
finally {
if (reader!= null) {
try {
Reader.close ();
catch (IOException E1) {
}
}
}
}

Write the contents of one file to another file (written in rows)

Copy Code code as follows:

public class Filetest {
public static void Main (string[] args) {
File File=new file ("C:\\Test.txt");
BufferedReader Read=null;
BufferedWriter Writer=null;
try {
Writer=new BufferedWriter (New FileWriter ("C:\\zwm.txt"));
catch (IOException E1) {
E1.printstacktrace ();
}
try {
Read=new BufferedReader (new FileReader (file));
String tempstring = null;
while ((Tempstring=read.readline ())!=null) {
Writer.append (tempstring);
Writer.newline ()/Line Wrap
Writer.flush ()//need to clear out the stream buffer in time, if the file is too large may not be written
}
Read.close ();
Writer.close ();
System.out.println ("File write complete ...");
catch (IOException e) {
E.printstacktrace ();
}

}
}

Related Article

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.