Home arrow Java arrow Page 5 - Working with Input and Output in Java
JAVA

Working with Input and Output in Java


This article, the first of three parts, will teach you how the programs you create in Java can interact with different storage devices using a communications system called streams. It is excerpted from chapter 15 of the book Sams Teach Yourself Java 2 in 21 days, written by Roger Cadenhead and Laura Lemay (Sams, ISBN: 0672326280).

Author Info:
By: Sams Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
April 13, 2006
TABLE OF CONTENTS:
  1. · Working with Input and Output in Java
  2. · Using a Stream
  3. · Handling Exceptions
  4. · File Input Streams
  5. · File Output Streams

print this article
SEARCH DEVARTICLES

Working with Input and Output in Java - File Output Streams
(Page 5 of 5 )

A file output stream can be created with the FileOutputStream(String) constructor. The usage is the same as the FileInputStream(String) constructor, so you can specify a path along with a filename.

You have to be careful when specifying the file associated with an output stream. If it's the same as an existing file, the original will be wiped out when you start writing data to the stream.

You can create a file output stream that appends data after the end of an existing file with the FileOutputStream(String, boolean) constructor. The string specifies the file, and the Boolean argument should equal true to append data instead of overwriting existing data.

The file output stream's write(int) method is used to write bytes to the stream. After the last byte has been written to the file, the stream's close() method closes the stream.

To write more than one byte, the write(byte[], int, int) method can be used. This works in a manner similar to the read(byte[], int, int) method described previously. The arguments to this method are the byte array containing the bytes to output, the starting point in the array, and the number of bytes to write.

The WriteBytes application in Listing 15.2 writes an integer array to a file output stream.

Listing 15.2 The Full Text of WriteBytes.java
 1: import java.io.*;
2: 
3: public class WriteBytes {
4:   public static void main(String[] arguments) {
5:     int[] data = { 71, 73, 70, 56, 57, 97, 13, 0,
12, 0, 145, 0,
6:       0, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0,
0, 0, 44, 0, 7: 0, 0, 0, 13, 0, 12, 0, 0, 2, 38, 132, 45,
121, 11, 25, 8: 175, 150, 120, 20, 162, 132, 51, 110, 106,
239, 22, 8, 9: 160, 56, 137, 96, 72, 77, 33, 130, 86, 37,
219, 182, 230, 10: 137, 89, 82, 181, 50, 220, 103, 20, 0, 59
}; 11: try { 12: FileOutputStream file = new 13: FileOutputStream("pic.gif"); 14: for (int i = 0; i < data.length; i++) 15: file.write(data[i]); 16: file.close(); 17: } catch (IOException e) { 18: System.out.println("Error -- " +
e.toString()); 19: } 20: } 21: }

The following things are taking place in this program:

  • Lines 5–10—Create an integer array called data with 66 elements

  • Lines 12 and 13—Create a file output stream with the filename pic.gif in the same folder as the WriteBytes.class file

  • Lines 14 and 15—Use a for loop to cycle through the data array and write each element to the file stream

  • Line 16—Closes the file output stream

After you run this program, you can display the pic.gif file in any Web browser or graphics-editing tool. It's a small image file in the GIF format, as shown in Figure 15.1.

                                          
Figure 15.1  The pic.gif file (enlarged).


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials