Net-informations.com
SiteMap  | About    

Java File Class Tutorial

This Chapter discusses the details of reading, writing, creating, and opening files in java programming. The java.io package contains the types required to perform input/output operations in Java. There are a wide array of file I/O methods to choose from.

Java's Input/Output (I/O) libraries are designed in an abstract way that enables you to read from external source of data sources and write to external targets, regardless of the kind of thing you’re writing to or reading from. When you work with files for I/O, you basically work with streams .

What Is a Stream?

A stream may be defined as a sequence of bytes . Input streams transfer bytes of data into a Java program from external resource, while Output streams transfer bytes of data from Java to some external target. The stream classes in Java, such as FileInputStream , are byte-oriented. They read and write data one byte at a time or in groups of bytes.

The Stream Classes

Most of the classes that work directly with streams are part of the java.io package. The two main classes are java.io.InputStream and java.io.OutputStream . These are abstract base classes for many different subclasses with more specialized abilities.

Java file class tutorial Reading, Writing, and Creating Files

JDK has two sets of I/O packages:

  1. Standard I/O (in package java.io), introduced since JDK 1.0 for stream-based I/O
  2. New I/O (in packages java.nio), introduced in JDK 1.4, for more efficient buffer-based I/O.

JDK 1.5 introduces the formatted text-I/O via new classes java.util.Scanner and Formatter , and C-like printf() and format() methods for formatted output using format specifiers.

JDK 1.7 enhances supports for file I/O via the so-called NIO.2 (non-blocking I/O) in new package java.nio.file and its auxiliary packages. It also introduces a new try-with-resources syntax to simplify the coding of close() method.

To use Java's input/output classes, make sure that in addition to importing any other necessary packages, also import the java.io package and the java.util package : import java.io.*; import java.util.*; .

Input operations are separated in terms of reading from a stream in a three-step process:

  1. Open the stream
  2. Read data items from the stream front to back in sequence
  3. Close the stream.

Output operations are separated in terms of writing to a stream in a three-step process:

  1. Open the stream
  2. Write data onto the end of the stream in sequence
  3. Close the stream.

From the following chapters, you can learn these classes in detail.







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.