You are on page 1of 16

Homework 8:

SWING COMPONENTS
IN JAVA
Object Oriented Programming – Lec

Submitted to: Ms. Regina Santos

Submitted by: Ericka Michelle Labajo

1
BSIT – SMBA B21

10/26/19

Table of Contents
Introduction of Swing API

page 3-4

o File InputStream Constructors page 4

o File InputStream Methods page 5-6

o Java Read File using File InputStream page 7

 Output Stream page 8

o File OutputStream Constructors page 9

o Java File OutputStream Methods page 10


o Java read to File using File OutputStream page 11

2
 Stream page 12

 Reader page 13

 References page 14

Introduction of SWING in Java

Java Swing is a lightweight Java graphical user interface (GUI) widget toolkit that
includes a rich set of widgets. It is part of the Java Foundation Classes (JFC) and includes
several packages for developing rich desktop applications in Java. Swing includes built-in
controls such as trees, image buttons, tabbed panes, sliders, toolbars, color choosers, tables,
and text areas to display HTTP or rich text format (RTF). Swing components are written entirely
in Java and thus are platform-independent.

Swing offers customization of the look and feel of every component in an application without
making significant changes to the application code. It also includes a pluggable look and feel
feature, which allows it to emulate the appearance of native components while still having the
advantage of platform independence. This particular feature makes writing applications in Swing
easy and distinguishes it from other native programs.

3
Hierarchy of Java Swing classes

4
Input Stream

The InputStream class is an abstract superclass that provides a minimal programming


interface and a partial implementation of input streams. The InputStream class defines methods
for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input,
finding out the number of bytes available for reading, and resetting the current position within
the stream. An input stream is automatically opened when you create it. You can explicitly close
a stream with the close method, or let it be closed implicitly when the InputStream is garbage
collected. Remember that garbage collection occurs when the object is no longer referenced.

5
The figures show the class hierarchy for the input stream classes in the java.io package.

As you can see from the figure, InputStream inherits from the Object class; six classes inherit
directly from InputStream. One of InputStream's descendants, Filter InputStream, is itself an
abstract class with four children.

6
File InputStream Constructors

File InputStream provides many methods for reading bytes from a file and we can create an
instance of FileInputStream using below constructors.

1. File InputStream (File file): Creates a FileInputStream object by opening a connection to


an actual file by using specified file object.
2. File InputStream (File Descript orfdObj): Creates a FileInputStream object by using the
specified file descriptor fdObj, which represents an existing connection to an actual file
in the file system.
3. File InputStream (String name): Creates a FileInputStream object by opening a
connection to an actual file named by the specified name which represents path of the
file.

File InputStream Methods


Let’s have a look at the below methods of FileInputStream class.

1. available(): This method returns an estimate of the number of remaining bytes that can be read
from input stream without blocking by the next invocation of method for this input stream.

2. close(): This method closes this file input stream and release any system resources associated
with the stream. If this stream has an associated channel then channel is closed as well.

3. finalize(): This method ensures that the close() method of this file input stream is called when
there are no more reference to it.

4. getChannel(): This method returns the unique FileChannel object associated with this file input
stream. The initial position of the returned channel will be equal to the number of bytes read
from the file and reading bytes from this stream will increment the channel’s position.

5. getFD(): This method returns the FileDescriptor object that represents the connection to the
actual file in the file system being used by this FileInputStream.

6. read(): This method reads a byte of data from this input stream and returns the next byte of data
or -1 if the end of file is reached.

7
7. read(byte[] b): This method reads byte of data up to the length of specified array(b.length) from
this input stream into specified array of bytes and returns the total number of bytes read into
the buffer or -1 if the end of file is reached.

8. read(byte[] b, int off, int len): This method reads bytes of data up to specified len from this input
stream into specified array of bytes. If specified len is not zero, the method blocks until some
input are available; otherwise no bytes are read and 0 is returned. It returns number of bytes
read into the buffer or -1 if the end of file is reached.

9. skip(long n): This method skips over and discards specified n bytes of data from the input stream
and returns the actual number of bytes skipped. It throws an IOException if the specified n is
negative.

Java Read File using File InputStream

SOURCE CODE

Output

8
Output Stream

Output

Output Stream

The OutputStream class is an abstract superclass that provides a minimal programming


interface and a partial implementation of output streams. OutputStream defines methods for
writing bytes or arrays of bytes to the stream. An output stream is automatically opened when
you create it. You can explicitly close an output stream with the close method, or let it be closed

9
implicitly when the OutputStream is garbage collected, which occurs when the object is no
longer referenced.

As you can see OutputStream inherits from the Object class; four classes inherit directly
from OutputStream. One of OutputStream's descendants, Filter OutputStream, is itself an
abstract class with three descendants.

File OutputStream Constructors

File OutputStream using below constructors.

1. FileOutputStream(File file): Creates a file output stream to write to the file represented by
specified file object. If the file exists but is a directory rather than a regular file, does not exist

10
but cannot be created, or cannot be opened for any other reason then
a FileNotFoundException is thrown.

2. File OutputStream(File file, boolean append): Creates a file output stream to write to the file
represented by the specified File object. If the second argument is true, then bytes will be
written to the end of the file rather than the beginning. If the file exists but is a directory rather
than a regular file, does not exist but cannot be created, or cannot be opened for any other
reason then a FileNotFoundException is thrown.

3. FileOutputStream (FileDescriptorfdObj): Creates a file output stream to write to the specified


file descriptor, which represents an existing connection to an actual file in the file system.

4. FileOutputStream (String name): Creates a file output stream to write to the file with the
specified name.

5. FileOutputStream (String name, boolean append): Creates a file output stream to write to the
file with the specified name. If the second argument is true, then bytes will be written to the end
of the file rather than the beginning. If the file exists but is a directory rather than a regular file,
does not exist but cannot be created, or cannot be opened for any other reason
then FileNotFoundException is thrown.

Java File OutputStream Methods


1. close(): This method closes this file output stream and release any system resources associated
with the stream.

2. finalize(): This method cleans up the connection to the file and ensures that the close() method
of this file output stream is called when there are no more reference to it.

11
3. getChannel(): This method returns the unique FileChannel object associated with this file output
stream. The initial position of the returned channel will be equal to the number of bytes written
to the file and writing bytes to this stream will increment the channel’s position.

4. getFD(): This method returns the FileDescriptor object that represents the connection to the
actual file in the file system being used by this FileOutputStream object.

5. write(byte[] b): This method writes b.length bytes from the specified byte array to this file
output stream.

6. write(byte[] b, int off, int len): This method writes len bytes from the specified byte array
starting at offset off to this file output stream.

7. write(int b): This method writes the specified byte to this file output stream. Implements the
write method of OutputStream.

Java read to File using File OutputStream


SOURCE CODE

12
OUTPUT

Stream
13
 A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a
stream because it is like a stream of water that continues to flow.
 In Java, 3 streams are created for us automatically. All these streams are attached with
the console.

1) System.out: standard output stream

2) System.in: standard input stream

3) System.err: standard error stream Input and Output Stream

Let's see the code to print output and an error message to the console.

Let's see the code to get input from console.

Reader

The Java Reader is the base class of all Reader's in the Java IO API. Subclasses include
a BufferedReader, PushbackReader, InputStreamReader, StringReader and several others.

14
Here is a simple Java IO Reader example:

Reader reader = new FileReader("c:\\data\\myfile.txt");

int data = reader.read();


while(data != -1){
char dataChar = (char) data;
data = reader.read();
}

Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and
255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value
between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily mean
that the Reader reads two bytes at a time from the source it is connected to. It may read one or
more bytes at a time, depending on the encoding of the text being read.

Format Use

Font theme: Calibri

15
Font Size: pt.12 and pt.22

Font Styles: Bold and Normal

Font Color: Blue (Accent 5, darker 50%)

Black (Text 1)

Spacing: 1.5

References

http://journals.ecs.soton.ac.uk/java/tutorial/java/io/overview.html

https://www.journaldev.com/19187/java-fileinputstream

http://math.hws.edu/eck/cs124/javanotes7/c11/s1.html

http://www.java2s.com/Tutorials/Java/Stream_Reader_Writer/Get_to_know_Java_input_outpu
t_stream_and_reader_writer.htm

https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

16

You might also like