FileInputStream and FileOutputStream class

Source: Internet
Author: User
Tags control characters

    • The FileInputStream and FileOutputStream classes are used to create the input and output stream objects for disk files, using their constructors to specify the file path and file name.
    • When you create an FileInputStream instance object, the specified file should be present and readable. When you create an FileOutputStream instance object, the original content in this file is overwritten if the specified file already exists.
    • Two ways to create a FileInputStream object on the same disk file: (1) FileInputStream inone = new FileInputStream ("Hello.test"); (2)      File F = new file ("Hello.test"); FileInputStream intwo = new FileInputStream (f);
    • When you create an FileOutputStream instance object, you can specify a file name that does not already exist, but you cannot specify a file that has been opened by another program.
  Think: To write the contents of a file to B file, in the program code, is the output class object, or the input class object to connect a file and complete the operation of a file?   (remember: the concept of input and output here is relative to this application, not relative to the file.) such as) so we should create an input class to read the contents of a file, and then create an output class to output the content to the B file.   Programming Example: Write a string character to a file with the FileOutputStream class and read the written content with FileInputStream.  //filestream.java import Java.io.*; public class filestream{   public static void main ( String[] args)  throws exception {      fileoutputstream out = new FileOutputStream ("Hello.txt");      out.write ("www.sina.com.cn". GetBytes ());  //converts a string into a byte array and writes to the stream       out.close ();       byte[] buf = new byte[1024];       file F = new file ("Hello.txt");      fileinputstream in = new FileInputStream (f);      int len = In.read (BUF);  //reads content into byte array        System.out.println (new string (Buf,0,len));  //string constructor converts a byte array to a string       in.close ();  }}   reader class and writer class  
    • Reader and writer are abstract base classes for all character stream classes that simplify the input and output programming of strings, which is used to read and write text data.
    • The difference between a binary file and a text file. Strictly speaking, each file in the file system is a binary file. Various text characters are made up of one or several bytes, where the data for each byte cannot be arbitrary. If each byte in a file or data in each contiguous number of bytes can be represented as a character, we can call this file a text file. A visible text file is just a special case of a binary file. To distinguish it from text files, people refer to files other than text files as binary files. Conceptually we can simply assume that if a file is dedicated to storing text characters without any data other than characters, it is called a text file, except that the file is a binary file.
A text file refers to a file that consists entirely of visible characters. The so-called visible characters are ASCII code 32 to 126 characters, carriage return (ASCII code 13), newline (ASCII code 10), tab (ASCII 9), and all kanji characters (including, of course, other character sets such as Korean, Japanese, Arabic, etc.). If it is Unicode text, the ASCII code 0 is also included.
Binary files are defined in many ways, and the generalized binary refers to all the files (including text files) in the computer, because all the files in the computer are actually stored in binary mode, that is, each character (including visible characters, Control characters are eventually stored in 0 and 1 in the form of hard drives and other media (which is why computers only know the two numbers of 0 and 1);
The narrower binary file is relative to the text file, that is, as long as the file contains characters other than the visible characters (mainly control characters), is the binary file;
And more specific than the narrow definition of binary files are defined as executable (EXE), library function files (DLLs), picture video, database files and so on all by the program code, machine code, a particular binary code and data, such as a meaningful file.
Reader and writer classes and their subclasses (FileReader and FileWriter classes, etc.) are primarily used to read text-formatted content, while InputStream and OutputStream classes and their subclasses primarily read the contents of binary formats. Programming Example: Use the FileWriter class to write a string character to a file, and then read the written content with FileReader. Filestream2.java import java.io.*; public class filestream2{public static void Main (string[] args) throws Excetpion {FileWriter out = new FileWriter      ("Hello2.txt"); Out.write ("www.sina.com.cn");       You can write the string directly here without converting it to a byte array out.close ();  char[] buf = new char[1024];      Character array FileReader in = new FileReader ("Hello2.txt"); int len = In.read (BUF);      At this point the Read method can read one character or several characters, and Len represents the number of characters actually read. System.out.println (New String (buf,0,1024));      The string constructor converts a character array to a string.   In.close (); }}

FileInputStream is used to read byte data from a local file, inheriting from the InputStream class

Construction Method Summary
FileInputStream(File file)
Create a file by opening a connection to the actual file FileInputStream , which is specified by the object in the file system File file .
FileInputStream(FileDescriptor fdObj)
Creates a file descriptor that fdObj FileInputStream represents an existing connection to an actual file in the file system by using a file descriptor.
FileInputStream(String name)
Create a file by opening a connection to the actual file FileInputStream , which is specified by a pathname in the file system name .

Method Summary
 int available()
Returns the estimated number of remaining bytes that the next method invoked on this input stream can be read (or skipped) from this input stream in a blocked manner.
 void close()
Closes this file input stream and frees all system resources related to this stream.
protected  void finalize()
Make sure that the method is called when the file input stream is no longer referenced close .
 FileChannel getChannel()
Returns the unique object associated with this file input stream FileChannel .
 FileDescriptor getFD()
Returns an object that represents a connection to the actual file in the file system, which is being FileDescriptor used by this file system FileInputStream .
 int read()
Reads a byte of data from this input stream.
 int read(byte[] b)
b.lengthreads the most bytes of data from this input stream into a byte array.
 int read(byte[] b, int off, int len)
lenreads the most bytes of data from this input stream into a byte array.
 long skip(long n)
Skips and discards n bytes of data from an input stream.

read()it returns the int value (0-255) for the read-in byte, read(byte[] b) and the number of read(byte[] b, int off, int len) bytes read in

The FileOutputStream is used to write out byte data to a file. Inherit from OutputStream class

Construction Method Summary
FileOutputStream(File file)
Creates a File file output stream that writes data to the file represented by the specified object.
FileOutputStream(File file, boolean append)
Creates a File file output stream that writes data to the file represented by the specified object.
FileOutputStream(FileDescriptor fdObj)
Creates an output file stream that writes data to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)
Creates an output file stream that writes data to a file with the specified name.
FileOutputStream(String name, boolean append)
Creates a name stream of output files to write data to the file that has the specified.

Method Summary
 void close()
Closes this file output stream and frees all system resources related to this stream.
protected  void finalize()
Cleans up the connection to the file and ensures that this stream is called when the file output stream is no longer referenced close .
 FileChannel getChannel()
Returns the unique object associated with this file output stream FileChannel .
 FileDescriptor getFD()
Returns the file descriptor associated with this stream.
 void write(byte[] b)
b.lengthwrites a byte from the specified byte array to this file output stream.
 void write(byte[] b, int off, int len)
Writes a byte in the specified byte array from off the offset to len this file output stream.
 void write(int b)
Writes the specified bytes to this file output stream.


Instance code: Use these two classes for file copying:

[Java]View Plain
    1. Import Java.io.FileInputStream;
    2. Import Java.io.FileOutputStream;
    3. Import java.io.IOException;
    4. Class Iodemo
    5. {
    6. public static void Main (string[] args)
    7. {
    8. Try
    9. {
    10. //using FileInputStream and FileOutputStream for file copying
    11. FileInputStream fis=New FileInputStream ("a.txt");
    12. FileOutputStream fos=New FileOutputStream ("B.txt");
    13. int read;
    14. //read=fis.read ();
    15. byte b[]=new byte[1024];
    16. //Read file, byte array B, return the number of characters read, save to read, default every time the B array fills
    17. Read=fis.read (b);
    18. While (read!=-1)
    19. {
    20. Fos.write (b,0,read);
    21. Read=fis.read (b);
    22. //read=fis.read ();
    23. }
    24. Fis.close ();
    25. Fos.close ();
    26. }
    27. catch (IOException e)
    28. {
    29. E.printstacktrace ();
    30. }
    31. }
    32. }

FileInputStream and FileOutputStream class

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.