FileInputStream and FileOutputStream class

Source: Internet
Author: User

FileInputStream and FileOutputStream class
Reproduced in the network
FileInputStream and FileOutputStream class
    • 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 the a file, and then create an output class to output the content to the B file. (e.g.)


Programming Example: Use the FileOutputStream class to write a string character to a file, and then read the written content with FileInputStream. Filestream.javaimport 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());//Convert string to byte array and write to streamOut.close ();      byte[] buf = new byte[1024];      File F = new file ("Hello.txt");      FileInputStream in = new FileInputStream (f); int len = In.read (BUF);//Read contents into byte arraySystem.out.println (NewString (Buf,0,len)); The//string constructor converts a byte array into 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.
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.javaimport 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 ArrayFileReader in = new FileReader ("Hello2.txt"); int len = In.read (BUF);//The Read method can read one character or several characters at this time, and Len represents the number of characters actually read. System.out.println (New String (buf,0,1024));The//string constructor converts a character array into a string. In.close (); }}

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.