Streams and files java io

I/O Streams

An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time:

Reading information into a program.

A program uses an output stream to write data to a destination, one item at time:

Writing information from a program.

In this lesson, we’ll see streams that can handle all kinds of data, from primitive values to advanced objects.

The data source and data destination pictured above can be anything that holds, generates, or consumes data. Obviously this includes disk files, but a source or destination can also be another program, a peripheral device, a network socket, or an array.

In the next section, we’ll use the most basic kind of streams, byte streams, to demonstrate the common operations of Stream I/O. For sample input, we’ll use the example file xanadu.txt , which contains the following verse:

In Xanadu did Kubla Khan A stately pleasure-dome decree: Where Alph, the sacred river, ran Through caverns measureless to man Down to a sunless sea.

Источник

Lesson: Basic I/O

This lesson covers the Java platform classes used for basic I/O. It first focuses on I/O Streams, a powerful concept that greatly simplifies I/O operations. The lesson also looks at serialization, which lets a program write whole objects out to streams and read them back again. Then the lesson looks at file I/O and file system operations, including random access files.

Читайте также:  Parsing http request php

Most of the classes covered in the I/O Streams section are in the java.io package. Most of the classes covered in the File I/O section are in the java.nio.file package.

I/O Streams

  • Byte Streams handle I/O of raw binary data.
  • Character Streams handle I/O of character data, automatically handling translation to and from the local character set.
  • Buffered Streams optimize input and output by reducing the number of calls to the native API.
  • Scanning and Formatting allows a program to read and write formatted text.
  • I/O from the Command Line describes the Standard Streams and the Console object.
  • Data Streams handle binary I/O of primitive data type and String values.
  • Object Streams handle binary I/O of objects.

File I/O (Featuring NIO.2)

  • What is a Path? examines the concept of a path on a file system.
  • The Path Class introduces the cornerstone class of the java.nio.file package.
  • Path Operations looks at methods in the Path class that deal with syntactic operations.
  • File Operations introduces concepts common to many of the file I/O methods.
  • Checking a File or Directory shows how to check a file’s existence and its level of accessibility.
  • Deleting a File or Directory.
  • Copying a File or Directory.
  • Moving a File or Directory.
  • Managing Metadata explains how to read and set file attributes.
  • Reading, Writing and Creating Files shows the stream and channel methods for reading and writing files.
  • Random Access Files shows how to read or write files in a non-sequentially manner.
  • Creating and Reading Directories covers API specific to directories, such as how to list a directory’s contents.
  • Links, Symbolic or Otherwise covers issues specific to symbolic and hard links.
  • Walking the File Tree demonstrates how to recursively visit each file and directory in a file tree.
  • Finding Files shows how to search for files using pattern matching.
  • Watching a Directory for Changes shows how to use the watch service to detect files that are added, removed or updated in one or more directories.
  • Other Useful Methods covers important API that didn’t fit elsewhere in the lesson.
  • Legacy File I/O Code shows how to leverage Path functionality if you have older code using the java.io.File class. A table mapping java.io.File API to java.nio.file API is provided.
Читайте также:  Post twitter with php

Summary

A summary of the key points covered in this trail.

Questions and Exercises

Test what you’ve learned in this trail by trying these questions and exercises.

The I/O Classes in Action

Many of the examples in the next trail, Custom Networking use the I/O streams described in this lesson to read from and write to network connections.

Security consideration: Some I/O operations are subject to approval by the current security manager. The example programs contained in these lessons are standalone applications, which by default have no security manager. To work in an applet, most of these examples would have to be modified. See What Applets Can and Cannot Do for information about the security restrictions placed on applets.

Источник

Lesson: Basic I/O

This lesson covers the Java platform classes used for basic I/O. It first focuses on I/O Streams, a powerful concept that greatly simplifies I/O operations. The lesson also looks at serialization, which lets a program write whole objects out to streams and read them back again. Then the lesson looks at file I/O and file system operations, including random access files.

Most of the classes covered in the I/O Streams section are in the java.io package. Most of the classes covered in the File I/O section are in the java.nio.file package.

I/O Streams

  • Byte Streams handle I/O of raw binary data.
  • Character Streams handle I/O of character data, automatically handling translation to and from the local character set.
  • Buffered Streams optimize input and output by reducing the number of calls to the native API.
  • Scanning and Formatting allows a program to read and write formatted text.
  • I/O from the Command Line describes the Standard Streams and the Console object.
  • Data Streams handle binary I/O of primitive data type and String values.
  • Object Streams handle binary I/O of objects.

File I/O (Featuring NIO.2)

  • What is a Path? examines the concept of a path on a file system.
  • The Path Class introduces the cornerstone class of the java.nio.file package.
  • Path Operations looks at methods in the Path class that deal with syntactic operations.
  • File Operations introduces concepts common to many of the file I/O methods.
  • Checking a File or Directory shows how to check a file’s existence and its level of accessibility.
  • Deleting a File or Directory.
  • Copying a File or Directory.
  • Moving a File or Directory.
  • Managing Metadata explains how to read and set file attributes.
  • Reading, Writing and Creating Files shows the stream and channel methods for reading and writing files.
  • Random Access Files shows how to read or write files in a non-sequentially manner.
  • Creating and Reading Directories covers API specific to directories, such as how to list a directory’s contents.
  • Links, Symbolic or Otherwise covers issues specific to symbolic and hard links.
  • Walking the File Tree demonstrates how to recursively visit each file and directory in a file tree.
  • Finding Files shows how to search for files using pattern matching.
  • Watching a Directory for Changes shows how to use the watch service to detect files that are added, removed or updated in one or more directories.
  • Other Useful Methods covers important API that didn’t fit elsewhere in the lesson.
  • Legacy File I/O Code shows how to leverage Path functionality if you have older code using the java.io.File class. A table mapping java.io.File API to java.nio.file API is provided.
Читайте также:  Load img with css

Summary

A summary of the key points covered in this trail.

Questions and Exercises

Test what you’ve learned in this trail by trying these questions and exercises.

The I/O Classes in Action

Many of the examples in the next trail, Custom Networking use the I/O streams described in this lesson to read from and write to network connections.

Security consideration: Some I/O operations are subject to approval by the current security manager. The example programs contained in these lessons are standalone applications, which by default have no security manager. To work in an applet, most of these examples would have to be modified. See What Applets Can and Cannot Do for information about the security restrictions placed on applets.

Источник

Оцените статью