Java Read file: binary file

Source: Internet
Author: User

FileWriter can only accept arguments in the form of strings, which means that the content can only be stored in a text file. Relative to the text of this article
Save more space in binary format file storage

The InputStream is used to read data from the input stream by byte. where the int read () method reads a byte, this byte
Returns a value from 0 to 255 as an integer . Why read a byte without directly returning a value of type byte?
because the highest bit of the byte type is the sign bit, the largest positive integer it can represent is 127.

InputStream is just an abstract class and cannot be instantiated. Filelnputstream is a subclass of InputStream that is used to
Read by byte in the file.

 Public Static void throws  = "D:/test.txt"new// Create a file object according to the file path  // If the file cannot be found, Throws a FileNotFoundException exception new// closes the file input stream and throws a IOException exception if it fails to shut down properly 

The Write (int b) method in OutputStream is used to write out data by byte. FileOutputStream is used to set the number of bytes by byte
The file was written. For example, read the content from a file in bytes and write another new file, the file
System functions.

File Fileln =NewFile ("source. TXT ");//Open source FileFile fileout =NewFile ("Target.txt");//Open write files, that is, target file Filelnputstream streamln =NewFilelnputstream (Fileln);//build an input stream from a source fileFileOutputStream streamout =NewFileOutputStream (fileout);//build an output stream from a target fileintC;//reads the data in bytes from the source file, and if the content is not finished reading, continue while((c = Streamln.read ())! = 1{streamout. write (c);//write to destination file}streamln.close. ;//close the input streamStreamout.close ();//turn off the output stream

Determines whether the file already exists and generates the file if it does not exist.

New File (Dicdir + datadic); if (! datafile.exists ()) {// write to file if the file does not exist }

Use the File.mkdirs () method to create a multilevel directory. For example, when a directory does not exist, it is created.

New File (imgpath); if (! tempdir.exists ()) {Tempdir.mkdirs ();}

Java Read file: binary file

Related Article

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.