chapter_14_pages_javabook - 14.3 Handling files as binary...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
14.3 Handling files as binary files 469 14.3 Handling files as binary files Although all files on a computer’s hard disk contain only bits, binary digits, we say that some files are text files while others are considered binary files. Categorizing files into text files and binary files is thus somewhat arbitrary. A binary file can be considered a series of bytes, and so can a text file. It is possible to open a text file as a binary file. The essential distinction between these two file types is that a binary file can contain any bytes, but a text file contains only character codes of visible characters, line termination charac- ters, and such text formatting characters as tabulator characters. It is possible to view a text file with an editor program (e.g. JCreator, EditPad or Notepad). Binary files, on the other hand, usually contain data that is not readable by the human eye, and may even cause strange behavior in an editor program. When we want to process binary files in a Java program, we use different standard classes depending on whether we want to read data from a file or to write data to a file. A file can be opened for binary reading operations with a statement like FileInputStream file_to_read = new FileInputStream( file_name_as_string ) ; In this statement the constructor of class FileInputStream opens a file with the given file name. After the statement is successfully executed, the file is represented by the FileInputStream object, and it can be read by using the file reading methods of class FileInputStream . The basic FileInputStream method for reading a file in binary form has the name read() . Actually, there are several versions of the read() method. The version of the read() method which we use in this book can be called to read a certain number of bytes from a file. The method puts the bytes it reads to an array of type byte[] , and returns a value that indicates how many bytes it was able to read. Normally, the read() method reads as many bytes as it was asked to read, but when the end of the file has been reached, it is possible that it may not be able to read all the bytes that were requested. One possible way to call the read() method is the following: byte[] bytes_from_file = new byte[ 50 ] ; int number_of_bytes_read = file_to_read.read( bytes_from_file ) ; This method call could be "translated" as follows: "Here is this array of bytes. Please, read bytes from the file and put them to the array, starting from the array position that has index 0. Read 50 bytes at maximum because that is the length of the array; it cannot hold any more bytes." After receiving these orders, the read() method would do its job and return the number of bytes it was able to read. The read() method checks the length of the given array of type byte[] , and automatically delivers as many bytes as is the length of the array.
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern