FileChannel Objects in Java, ByteBuffer Type Los Angeles CA

This lesson will walk you through a sample program that uses FileChannel objects in conjunction with ByteBuffer objects to perform read/write file IO for data of the primitive type byte.

Local Companies

Interneer Inc.
8005586832 x85
6101 W. Centinela Ave.
Culver City, CA
Greene Computer Corporation
(818) 956-4961
200 S. Louise Street
Glendale, CA
Alphatier Systems
818-409-8920
517 Griswold St
Glendale, CA
Hutchinson & Bloodgood, LLP
(818) 637-5000
101 N. Brand Blvd. #1600
Glendale, CA
Abraxas Technologies Inc
818-502-9100
450 N Brand Blvd
Glendale, CA
Moyea Software
92295612365
Hot building, ring street
LA, CA
Cornerstone Concepts Inc
818-247-3909
600 W Broadway
Glendale, CA
Corticalx Inc Software Solutions & Technology
818-500-0881
425 E Colorado St
Glendale, CA
TimeTECH - Customizable Time and Attendance / Workforce Management Solutions
905-677-7009
7420 Airport Rd 203
Mississauga, CA
Telsoft Solutions
818-545-8680
100 N Brand Blvd
Glendale, CA

provided by: 
Originally published at Internet.com


Java Programming Notes # 1788 * Preface * Discussion and Sample Code * Run the Program * Summary * What's Next? * Complete Program Listing -----------------------------------

Preface

New features in SDK Version 1.4.0

The recently released JavaTM 2 SDK, Standard Edition Version 1.4 contains a large number of new features.

Among the features which are completely new to version 1.4 is the concept of an IO channel. The previous lesson, entitled FileChannel Objects in Java, Background Information, introduced you to the concept of channels from a read/write IO viewpoint.

Data of type byte

This lesson will walk you through a sample program that uses FileChannel objects in conjunction with ByteBuffer objects to perform read/write file IO for data of the primitive type byte.

Mixed primitive types

In the next lesson, I will show you how to use the new I/O classes to transfer data of type double and data of type short between the computer's memory and a physical file. You will learn how to extend the concept to any primitive data type other than boolean.

In a future lesson, I will show you how to use the new I/O classes to create records consisting of sequences of data values of different primitive types, and how to transfer those records between the computer's memory and a physical file.

Memory-mapped IO

Future lessons will teach you how to do memory-mapped IO using channels.

A channel is not a stream

Note that a channel is not the same as a stream, but a channel may be based on a stream. Streams have been supported by Java since the earliest days of Java. Channels were not introduced until version 1.4.

The java.nio.channels package

Much of the support for channels is provided in the package named java.nio.channels. This package defines more than a dozen new classes, and about seven new interfaces, not counting the new exception classes. (In addition, there are four more new packages that begin with the name java.nio.)

Viewing tip

You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different listings and figures while you are reading about them.

Supplementary material

I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com.

Discussion and Sample Code

The Buffer and ByteBuffer classes

Before getting into the details of FileChannel objects, I need to tell you that much of what youu will see in this lesson will involve objects of the ByteBuffer class. The ByteBuffer class extends the Buffer class, and as such inherits a number of methods from the Buffer class.

It will be much easier for you to understand this discussion of FileChannel objects if you are already familiar with the features of Buffer and ByteBuffer. I have previously published lessons on these topics entitled Understanding the Buffer class in Java and The ByteBuffer Class in Java. You may find it useful to review those two lessons in preparation for your study of this lesson.

What is a FileChannel?

Sun describes an object of the FileChannel class simply as "A channel for reading, writing, mapping, and manipulating a file." In this lesson, I will show you how to use FileChannel objects for reading files and writing files. In a future lesson, I will show you how to use FileChannel objects for mapping files into memory.

Enough talk, let's see some code!

I will illustrate the FileChannel class using the sample program named Channel01. You will find a complete listing of the program in Listing 16 near the end of the lesson. As is my normal approach, I will discuss this program in fragments.

This program, which was tested using Java SDK version 1.4.0 under Win2000, first writes, and then reads a file using FileChannel objects. The data written to and read from the file is of the primitive type byte.

Import directives

The first fragment, shown in Listing 1, shows the beginning of the class named Channel01. The primary purpose of showing this listing at this point is to emphasize the use of the new java.nio packages, as illustrated by the import directives. import java.io.*; import java.nio.channels.*; import java.nio.*; class Channel01 { Listing 1

The showBufferData method

Listing 2 shows the signature of a static convenience method of my own design named showBufferData. As you might guess from the name and the incoming parameter type, the purpose of this method is to display the contents of a ByteBuffer object. This method will be used later in the program, so you need to understand how it behaves.   static void showBufferData (           ByteBuffer buf, String name){ Listing 2

A ByteBuffer parameter

As you can see, this method receives a reference to a ByteBuffer object as an incoming parameter. The method displays the byte data encapsulated by that object.

A String parameter

The method also receives a reference to a String object as an incoming parameter. This string is included in the display to make it easier to associate the output with the program code.

Saving the position property

When the ByteBuffer object is passed to the method, its position property may or may not be zero. This method needs to be careful to avoid modifying the value of the

Author: Richard G. Baldwin

Read article at Internet.com site

Featured Local Company

Moyea Software

92295612365
Hot building, ring street
LA, CA

Related Local Events
SOLAR POWER - Exhibition and Conference
Dates: 10/12/2010 - 10/14/2010
Location: Los Angeles Convention & Exhibition Center
Los Angeles, CA
View Details

Automation Technology Expo West (ATX West)
Dates: 2/9/2010 - 2/11/2010
Location: Anaheim Convention Center
Anaheim, CA
View Details

REAL-TIME & EMBEDDED COMPUTING CONFERENCE - LONG BEACH 2009
Dates: 10/1/2009 - 10/1/2009
Location: Marriott Long Beach
Long Beach, CA
View Details

2009 IEEE Petroleum and Chemical Industry Technical Conference (PCIC 2009)
Dates: 9/14/2009 - 9/16/2009
Location:
Anaheim, CA
View Details