File Input and Output (2024)

File Input and Output

There are two types of files in Java - text files and binary files. Filesprovide both sequential and random access. A text file is processed as asequence of characters. A binary file is processed as a sequence of bytes.In a text file you have the illusion that the file is divided into lines.There is a special end-of-line symbol that creates this illusion.In addition you can think that there is a special end-of-file symbol that follows the last component in a file. A big advantage of textfiles is their portability. In binary files, the representation usedvaries from computer to computer. Java binary files are platform independent. They can be interpreted by any computer that supports Java.

A stream is a device for transmitting or retrieving 8-bit or byte values.The emphasis is on the action of reading or writing as opposed to thedata itself. A file is a collection of items stored on an external device.The Java object FileStream provides the means to access the data valuesbut does not actually hold the file contents.

There are two independent and largely parallel systems involved in I/O. InputStream and OutputStream are used to read and write8-bit quantities and process binary files. The alternative hierarchy hastwo different classes Reader and Writer that are used to read and write16-bit Unicode character values and process text files.

There are classes tied to a physical input source. These read values froma byte array, a file, or a pipe (e.g. a FileInputStream requires eithera file or a file name). There are also virtual input classes that relyon one or more underlying input streams for their data source. For examplethe class PushbackInputStream adds the ability to unread alreadyprocessed characters, returning them once more in response to a subsequentread operation.

One type of filtering is buffering which is provided by several bufferedstreams including BufferedInputStream and BufferedReader for performing binary and text input. BufferedOutputStream and BufferedWriter for binary and text outputs. A buffer is a relatively large region of memory used to temporarily store data whileit is being input or output.

StreamTokenizer breaks a textual file into a sequence of tokens.It recognizes words and numbers.

The File class provides methods for dealing with files or directories.File systems are organized into a hierarchy. A path is a description of a file's location in the hierarchy. When a program is running, the program'directory is considered the current directory. Any files located in thecurrent directory can be referred to by name alone. The relative path isthe location of a file with respect to the current directory. The absolutepath starts at the root directory.

public class File extends Object implements Serializable{ // Constructors public File ( String path ); public File ( String path, String name ); // Public methods public boolean canRead();// is the file readable? public boolean canWrite();// is the file writeable? public boolean delete();// delete the file public boolean exists();// does the file exist public long lastModified();// when was the file last modified public long length();// How many bytes does it contain public boolean renameTo(File f); // rename this file to f's name}

Creating a Handle to a File

A handle to a file is created by passing the name of the file tothe constructor for the File object:
File inFile = new File ( "FileIO.txt" );

Reading from a Text File

Reading from a text file is facilitated by the class FileReader. Unfortunately, the methods available from the class FileReader arenot convenient for text processing. We usually embed this stream withina BufferedReader object. FileReader will throw the exception FileNotFoundException so the input statements should beenclosed in a try block and the exception handled in the catch block.
import java.io.*;public class ReadTextFile{ public static void main (String [] args) throws IOException { File inFile = new File ("input.txt"); FileReader fReader = new FileReader (inFile); BufferedReader bReader = new BufferedReader (fReader); try { String line = bReader.readLine(); while (line != null) { System.out.println (line); line = bReader.readLine(); } } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { fReader.close(); } }}

A text file can also be read using a Scanner object. Using the Scanneroffers the advantage of using the methods that come with the Scannerclass.

import java.util.Scanner;import java.io.File;public class ReadTextFile{ public static void main (String [] args) throws IOException { File inFile = new File ("input.txt"); Scanner sc = new Scanner (inFile); while (sc.hasNextLine()) { String line = sc.nextLine(); System.out.println (line); } sc.close(); }}

Writing to a Text File

To write text to a file you open an output stream by using the class FileWriter. If the file does not exist a new empty file with this name is created. If the file already exists opening it erases the data inthe file. If you want to append to the file use the following option whencreating the FileWriter object:
FileWriter fWriter = new FileWriter (outFile, true);
The class PrintWriter has methods print(), printf() andprintln() that will allow us to write to a file.
import java.io.*;public class WriteTextFile{ public static void main (String [] args) throws IOException { File outFile = new File ("output.txt"); FileWriter fWriter = new FileWriter (outFile); PrintWriter pWriter = new PrintWriter (fWriter); pWriter.println ("This is a line."); pWriter.println ("This is another line."): pWriter.close(); }}

Binary Files

When designing binary file I/O applications it is a good idea to designthe input and output methods together. It is convenient to save data ofdifferent types such as int, double, and String to filesin such a way that these can be read back as integers, doubles, andstrings.

Writing to a Binary File

The class DataOutputStream is used to output several types ofdata. It has methods:
  • void writeChar ( char ch )
  • void writeDouble ( double d )
  • void writeInt ( int i )
  • void writeUTF ( String s )
UTF stands for Unicode Text Format. It is the coding scheme forJava's Unicode character set.
import java.io.*;public class WriteBinaryFile{ public static void main (String [] args) throws IOException { File outFile = new File ("output.bin"); FileOutputStream outStream = new FileOutputStream (outFile); DataOutputStream output = new DataOutputStream (outStream); String name = "John Doe"; long ssNum = 123456789; double gpa = 3.85; try { output.writeUTF (name); output.writeLong (ssNum); output.writeDouble (gpa); } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { outStream.close(); } }}

Reading from a Binary File

The class DataInputStream is used to input several types ofdata. This class has the following methods that allows us to input data:
  • char readChar()
  • double readDouble()
  • int readInt()
  • String readUTF()
import java.io.*;public class ReadBinaryFile{ public static void main (String [] args) throws IOException { File inFile = new File ("input.bin"); FileInputStream inStream = new FileInputStream (inFile); DataInputStream input = new DataInputStream (inStream); String name; long ssNum; double gpa; try { while (true) { name = input.readUTF(); ssNum = input.readLong(); gpa = input.readDouble(); System.out.println (name + " " + ssNum + " " + gpa); } } catch (EOFException e) { // Do nothing if it is the end of file. } catch (Exception e) { System.out.println (e.getMessage()); e.printStackTrace(); System.exit(0); } finally { inStream.close(); } }}
File Input and Output (2024)
Top Articles
VA Disability Rating for GERD
What is a myogenic heart? Describe the mechanism regulating the heartbeat in mammals.
Mybranch Becu
Www.paystubportal.com/7-11 Login
Ups Dropoff Location Near Me
Restaurer Triple Vitrage
Fat Hog Prices Today
Chicago Neighborhoods: Lincoln Square & Ravenswood - Chicago Moms
East Cocalico Police Department
Robinhood Turbotax Discount 2023
Sissy Hypno Gif
Lesson 1 Homework 5.5 Answer Key
Does Pappadeaux Pay Weekly
Cape Cod | P Town beach
litter - tłumaczenie słowa – słownik angielsko-polski Ling.pl
Audrey Boustani Age
Slag bij Plataeae tussen de Grieken en de Perzen
Craigslist Motorcycles Orange County Ca
Uc Santa Cruz Events
Katherine Croan Ewald
Lancasterfire Live Incidents
Alfie Liebel
Vintage Stock Edmond Ok
Zack Fairhurst Snapchat
Bing Chilling Words Romanized
Walmart Near South Lake Tahoe Ca
Valic Eremit
Kroger Feed Login
Play It Again Sports Forsyth Photos
Japanese Emoticons Stars
Salemhex ticket show3
Primerica Shareholder Account
Home Auctions - Real Estate Auctions
6465319333
Hotel Denizen Mckinney
Chapaeva Age
Phone number detective
Sun-Tattler from Hollywood, Florida
Craigslist Greencastle
Jefferson Parish Dump Wall Blvd
Honda Ruckus Fuse Box Diagram
Ktbs Payroll Login
Jason Brewer Leaving Fox 25
Carroll White Remc Outage Map
Doublelist Paducah Ky
Online-Reservierungen - Booqable Vermietungssoftware
Breaking down the Stafford trade
Whitney Wisconsin 2022
Terrell Buckley Net Worth
Gameplay Clarkston
Www.card-Data.com/Comerica Prepaid Balance
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6445

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.