IO: fileinputstream and fileoutputstream

Source: Internet
Author: User

The fileinputstream and fileoutputstream classes are used to create input stream and output stream objects of disk files, and specify the file path and file name through their constructors. When creating a fileinputstream object instance, the specified file should exist and be readable. When creating a fileoutputstream instance object, if the specified file already exists, the original content of the file will be overwritten.

You can use either of the following methods to create a stream object of the fileinputstream class for a disk file:
(1) fileinputstream FCM = new fileinputstream ("test.txt ");
(2) file F = new file ("test.txt ");
Fileinputstream FCM = new fileinputstream (f );
The second method can perform many file operations on the test file, such as determining whether the file exists or is writable. This is the content in the file article.

When creating a fileoutputstream instance object, you can specify a file name that does not exist, but cannot specify a file opened by other programs.

The following is an example: Use the fileoutputstream class to write a string character to a file, and then use fileinputstream to read the written content.

import java.io.*;
public class FileStream {

Public static void main (string [] ARGs) throws exception {
Fileoutputstream out = new fileoutputstream ("test.txt ");
Out. Write ("www.cublog.com". getbytes (); // read the string. Write () cannot directly read the string, so it can only be converted to the number of segments.

Out. Close (); // close the input file object


Byte [] Buf = new byte [1, 1024];
File F = new file ("test.txt ");
Fileinputstream in = new fileinputstream (f );
Int Len = in. Read (BUF); // obtain the object length.

System. Out. println (new string (BUF, 0, Len ));
In. Close ();
}
}

Reader and Writer:

The characters in Java are unicode encoded and double-byte. The preceding fileinputstream and fileoutputstream are both used to process bytes. when processing the preceding string, you need to convert the string into bytes before writing it to the file, when reading a string, it also reads the byte array first, and then converts it into a string. In this case, you need to write additional code for conversion between bytes and characters. In Java, a separate class is used to input and output characters to Io devices. For example, filereader and filewriter can be used in the preceding example.

Reader and writer are used to read text files, while inputstream and outputstream are used to read the content of binary files.

Because the above example is a text file, you can use the filereader and filewriter classes to rewrite it:

import java.io.*;
public class FileStream {

    public static void main(String[] args) throws Exception{
             
        FileWriter out = new FileWriter("test.txt");
        out.write("www.cublog.com");
        out.close();
        
        char[] buf = new char[1024];
        FileReader in = new FileReader("test.txt");
        int len = in.read(buf);
        System.out.println(new String(buf,0,len));
        in.close();
        
    }
}

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.