IO operations in Java (i)

Source: Internet
Author: User
Tags coding standards

I. Introduction to Java IO

IO also writes "I/O", which can be understood as in and out, that is, input and output. Therefore, the basic function of the IO system is: Read and write.

IO is the core of Io stream, function: Read and write data on the device, hard disk file, memory, keyboard, network ...

Classification:

According to the trend of data, Io stream can be divided into: input stream and output stream;

Depending on the type of data processed, the IO stream can be divided into: byte stream and character stream;

The difference between a byte stream and a character stream:

byte streams can handle all types of data, such as MP3, pictures, text, video, and so on. When read, a byte is returned when the read occurs.

The corresponding classes in Java End with "Stream".


character streams can only handle plain text data, such as txt text. When reading, reads one or more bytes, finds the specified encoding table first, and then returns the detected characters.

The corresponding classes in Java End with "Reader" or "writer".


Characters, Bytes and encodings

bytes (Byte): The unit that transmits information over the network or stores information on a hard disk or in memory is a unit of measurement in which computer information technology is used to measure storage capacity and transmission capacity.

1 bytes =8 is a binary, that is, a 8-bit binary number, is a very specific storage space.

such as: 0x01,0x45,0xfa,......


character (Char): a symbol that is used by people in an abstract sense.

such as: ' 1 ', ' Medium ', ' a ', ' $ ',......


Character Set (Charset): Also known as "encoding." The different ANSI coding standards specified in each country and region specify only the "characters" required for their respective languages.

For example: The Chinese character standard (GB2312) does not specify how Korean characters are stored. The content of these ANSI coding standards contains two levels of meaning:

1. Which characters to use. In other words, the characters, letters and symbols are in the income standard. The set of "characters" contained is called "character set";

2. Specify that each "character" is stored in one byte or multiple bytes, with which bytes are stored, which is called "encoding".

Each country and region in the development of coding standards, "character collection" and "coding" are generally developed at the same time. Therefore, what we call "character sets", such as Gb2312,gbk,jis and so on, in addition to the meaning of "set of characters", also contains the meaning of "coding". This is why the character set is often called encoding .


ANSI : String in memory, if "character" is in ANSI encoded form, one character may be represented by one byte or more bytes, then we call this string an ANSI string or a multibyte string. The standards set by different ANSI encodings are not the same, so for a given multibyte string, we must know which encoding rule it uses to know what "characters" it contains.


Unicode: Strings are in memory, and if "characters" are present in Unicode, then we call this string a Unicode string or a wide-byte string. For a Unicode string, the "character" content that it represents is always the same, regardless of the environment. There are a number of criteria used to encode Unicode character sets, such as Utf-8,utf-7,utf-16,unicodelittle,unicodebig.


Iii. reading and writing data using a byte stream

import java.io.fileinputstream;import  java.io.filenotfoundexception;import java.io.ioexception;public class readbytestream { Public static void main (String[] args)  {try {//Create a "read file" byte stream fileinputstream  Fis = new fileinputstream ("Text.txt");//reads data into the input array byte[] input = new  byte[22];//Specifies the length of the byte array, the number of bytes does not exceed the size of the file can be fis.read (input);//To put the currently read data into the input array/* *  read the data into the input array, Because the target of the read is a text document, it is possible to convert the currently read data into a string  *  that is, use text to decode the current byte stream  *///decode string inputstring =  new string (input);//Create a new string System.out.println (InputString) on a byte array basis; Fis.close ();//Close input stream}  catch  (filenotfoundexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {e.printstacktrace ();}} 
import java.io.filenotfoundexception;import  Java.io.fileoutputstream;import java.io.ioexception;public class writebytestream {public  static void main (String[] args)  {try {// FileOutputStream Create a byte output stream Fileoutputstream fos = new fileoutputstream ("Textw.txt") of the file; string outstring =  "write 123456 write Data"; Byte[] output = outstring.getbytes (" UTF-8 ");//gets its byte array fos.write (output) from the current string object,//writes out a byte array, passes the output array out of fos.close ();//Turns off} catch  ( filenotfoundexception e)  {e.printstacktrace ();}  catch  (ioexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();}}} 
Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;public class Copybybytestream {public static void main (string[] args) {try {// Create file input stream FileInputStream fis = new FileInputStream ("08a58piceyc_1024.jpg");//create file output stream FileOutputStream fos = new FileOutputStream ("08a58piceyc_1024_new.jpg"); byte[] input = new Byte[50];fis.read (input); while (Fis.read (input)! =-1 ) {fos.write (input);} Fis.close (); Fos.close (); System.out.println ("Done");} catch (FileNotFoundException e) {e.printstacktrace ()} catch (IOException e) {//TODO auto-generated catch blocke.printst Acktrace ();}}}





IO operations in Java (i)

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.