Java input and output and file read and write (i)

Source: Internet
Author: User
Talent Technology 2017-01-09 08:56

According to the transmission direction of the data, the flow is raw binary data without processing, and it can be processed by some code to conform to some form regulation.

A particular data, i.e. a stream is a sequence of bits (bits) or a character (character), such as a sequence of character streams and a sequence of numeric streams.

The flow of data from peripheral or external memory (such as keyboards, mice, files) to an application is called an input stream, and a stream that passes data from an application to the external memory or peripheral (screen, printer) is called an output stream That is, the input stream reads the data (in this case the application reads the data), and the output stream writes the data (this refers to the application writing the data).

Characteristics of streaming input and output:

1, is the acquisition and transmission of data along the sequence of data sequences, each data must wait for the data in front of it to be read or sent before being read or written, and each read and write operation is the first of the remaining unread data in the sequence, not the location of the input output with the selection.

2, the use of data flow to process input and output so that the application of input and output operations independent of the relevant equipment, each application can be used for multiple input, without any need to make any changes to the source code. Enhances the portability of programs.

The input and output streams are divided into: byte streams (Bate stream) and character streams (character stream) according to the type of processing data. Every time a byte stream reads and writes a 8-bit binary number, because it can only read and write data in a binary way, and cannot decompose, reorganize, and understand the data, it is possible to transform and revert to the original meaningful state, so the byte stream is called a binary stream (binary byte stream) or a bit flow. The character stream reads and writes a 16-bit binary number at a time and processes it as a character instead of a bits.

Byte stream: Processing bytes data (base type InputStream, OutputStream)

Character streams: processing characters data (basic type Reader, Writer)

Files class: File, for managing disk files and folders. That is: file is the focus on the storage of files, such as file last modified time, storage location and so on, and flow is the focus of the file content. This is the difference between the two.

Example 1: Create a text file file.txt in the program, write a string of characters entered from the keyboard, and then read the file and display the contents of the text file on the screen.

Filename:FileTest.java
import java.io.*;
Class filetest{public
  static void Main (string[] args) {
     char ch;
     int data;
     try{
 FileInputStream fis = new FileInputStream (filedescriptor.in);//declare and create a FileInputStream type object that initializes the object to keyboard input
 FileOutputStream fos = new FileOutputStream ("D:\\myfile.txt")//Declare and create FileOutputStream type Object, Initialize object
 System.out.println ("Please enter a string of strings and end with #:");
 while (Ch= (char) fis.read)!= ' # ')//force type conversion to char and Judge
 Fos.write (CH);
 Fin.close;
 Fos.close;
 FileInputStream fi = new FileInputStream ("D:\\myfile.txt")//Declare and create FileInputStream type Object, Initialize object
 fileoutstream fo = New FileOutputStream (Filedescriptor.out)//declares and creates FileOutputStream type object, initializes object while
 (fi.avaliable > 0)
 {
 data = Fi.read;
 Fo.write (data);
 }
 Fi.close;
 fo.close;
     }
     catch (FileNotFoundException e) {
 System.out.println ("File not Found");}}


Example 2: Using FileInputStream and FileOutputStream to make a copy of binary image files

Filename:ImageFileTest.java
import java.io.*;
Public Class imagefiletest{public
    static void Main (string[] args) throws ioexception{FileInputStream
        fi = new fi Leinputstream ("d:\\scenery.jpg);" Declares and creates an object of type FileInputStream, initializes the object
        fileoutputstream fo = new FileOutputStream (" D:\\copyscenery.jpg ")//declares and creates an object of type FileOutputStream, initializes the object
        byte  b = new Byte[fi.available ()];//creates an array of byte types
        Fi.read (b);//Read the graphic file into the B array
        fo.write (b);//write the data for the B array to the new file ' Copyscenery.jpg '
        System.out.println ("The file has been copied and renamed ");
        Fi.close;
        Fo.close        
    }
}

This article, published by the headline author, does not represent today's headline position.

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.