27
Java Group- 4

Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Embed Size (px)

DESCRIPTION

The presentation gives you a brief introducation to JAva and is about the use of FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream in Java and the role of Java in Android. It also includes working programs and their output.

Citation preview

Page 1: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

JavaGroup- 4

Page 2: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Facts about JAVA

• B led to C, C evolved into C++, and C++ set the stage for Java.

• Although inseparably linked with the online environment of the Internet, Java is first and foremost a programming language.

• Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time.

• Named Java, from Java coffee, said to be consumed in large quantities by the language's creators.

• The most interested thing about java is "write once, run anywhere" (WORA), meaning the code that runs on one platform does not need to be recompiled to run on another.

Page 3: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream
Page 4: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Advantages of Java

• Free. • The syntax is familiar to the programmers

that know any other C based language. • Java (the platform) has a very large and

standard class library, some parts of which are very well written.

• Automatic Memory Management implemented by Garbage Collection.

• Good portability (certainly better than that of nearly any compiled alternative)

Page 5: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Why Java?• Java runs on more than 850 million personal computers

worldwide, and on billions of devices worldwide, including mobile and TV devices.

• There are lots of applications and websites that won't work unless you have Java installed, and more are created every day. Java is fast, secure, and reliable. From laptops to datacentres, game consoles to scientific supercomputers, cell phones to the Internet, Java is everywhere!

• Widely used for developing network related programs and softwares.

• Web applications and it can also generate static HTML content like php and JavaScript so it is used in website designing too.

• Creating Graphical User Interface.

Page 6: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

FileOutputStream

• A file output stream is an output stream for writing data to a File.

• OutputStream is an abstract of java.io class that defines streaming byte output. All of the methods in this class return a void value and throw an IOException in the case of errors.

• “public class FileOutputStream” extends OutputStream.

• Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream at a time.

Page 7: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Constructor Summary

• public FileOutputStream(String name)- Creates an output file stream to write to the file with the specified name.

• public FileOutputStream(String name, boolean append)--If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

• public FileOutputStream(File file)Creates a file output stream to write to the file represented by the specified File object.

• public FileOutputStream(File file, boolean append)-If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

Page 8: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Methods

• public void write(int b)-Writes the specified byte to this file output stream.

• public void write(byte[] b)-Writes b.length bytes from the specified byte array to this file output stream.

• public void write(byte[] b, int off, int len)-Writes len bytes from the specified byte array starting at offset.

• public void close()-Closes this file output stream and releases any system resources associated with this stream.

• protected void finalize()-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 references to this stream.

Page 9: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

eXAmple

// Demonstrate FileOutputStream.import java.io.*;class FileOutputStreamDemo {public static void main(String args[]) throws Exception {String source = "Now is the time for all good men\n"+ " to come to the aid of their country\n"+ " and pay their due taxes.";Byte buf[] = source.getBytes();OutputStream f0 = newFileOutputStream("file1.txt");

for (int i=0; i < buf.length; i = i+ 2) {f0.write(buf[i]);}f0.close();OutputStream f1 = new FileOutputStream("file2.txt");f1.write(buf);f1.close();} }

Page 10: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

OutputHere are the contents of each file after running this program. First, file1.txt:Nwi h iefralgo et oet h i ftercuty n a hi u ae.

Next, file2.txt:Now is the time for all good mento come to the aid of their countryand pay their due taxes.

Page 11: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

ByteArrayInputStream

• A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream.

• An internal counter keeps track of the next byte to be supplied by the read method.

• Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

Page 12: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Constructor Detail & Method

• public ByteArrayInputStream(byte[] buf)-Creates a ByteArrayInputStream so that it uses buf as its buffer array.

• public ByteArrayInputStream(byte[] buf, int offset, int length)-The initial value of pos is offset and the initial value of count is length.

Method• public int read()-Reads the next byte of data from this

input stream. The value byte is returned as an int in the range 0 to 255.

• public int read(byte[] b, int off, int len)-Reads up to len bytes of data into an array of bytes from this input stream.

Page 13: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Method• public long skip(long n)-Skips n bytes of input from

this input stream.

• public int available()-Returns the number of bytes that can be read from this input stream without blocking.

• public void reset()-Resets the buffer to the marked position. The marked position is 0 unless another position was marked.

• public void close()-Closing a ByteArrayInputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException

Page 14: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Example

// Demonstrate ByteArrayInputStream.import java.io.*;class ByteArrayInputStreamDemo {public static void main(String args[]) throws IOException {String tmp = "abcdefghijklmnopqrstuvwxyz";byte b[] = tmp.getBytes();ByteArrayInputStream input1 = new ByteArrayInputStream(b);ByteArrayInputStream input2 = new ByteArrayInputStream(b, 0,3);} }

import java.io.*;class ByteArrayInputStreamReset {public static void main(String args[]) throws IOException {String tmp = "abc";byte b[] = tmp.getBytes();ByteArrayInputStream in = new ByteArrayInputStream(b);for (int i=0; i<=1; i++) {int c;while ((c = in.read()) != -1) {if (i == 0) {System.out.print((char) c);} else {System.out.print(Character.toUpperCase((char) c));}} System.out.println();in.reset();} } }

Page 15: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Output• This example first reads each character

from the stream and prints it as is, in lowercase.

• It then resets the stream and begins reading again, this time converting each character to uppercase before printing. Here’s the output:

abcABC

Page 16: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

ByteArrayOutputStream

• “public class ByteArrayOutputStream” extends OutputStream.

• This class implements an output stream in which the data is written into a byte array.

• The buffer automatically grows as data is written to it.

• The data can be retrieved using toByteArray() and toString().

• Closing a ByteArrayOutputStream has no effect.

• The methods in this class can be called after the stream has been closed without generating an IOException.

Page 17: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Constructor Detail & Method

• public ByteArrayOutputStream()-Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.

• public ByteArrayOutputStream(int size)-Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.

Methods• public void write(int b)-Writes the specified byte to this byte array output

stream. • public void write(byte[] b, int off, int len)-Writes len bytes from the

specified byte array starting at offset off to this byte array output stream. • public void writeTo(OutputStream out)-Writes the complete contents of

this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write

Page 18: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Constructor Detail & Method

• public void reset()-Resets the count field of this byte array output stream to zero.

• public byte[] toByteArray()-Creates a newly allocated byte array.

• public int size()-Returns the current size of the buffer.

• public String toString() Converts the buffer's contents into a string, translating bytes into characters according to the platform's default character encoding.

• public String toString(String enc) -Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.

Page 19: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Example

// Demonstrate ByteArrayOutputStream.import java.io.*;class ByteArrayOutputStreamDemo {public static void main(String args[]) throws IOException {ByteArrayOutputStream f = new ByteArrayOutputStream();String s = "This should end up in the array";byte buf[] = s.getBytes();f.write(buf);System.out.println("Buffer as a string");System.out.println(f.toString());System.out.println("Into array");byte b[] = f.toByteArray();

for (int i=0; i<b.length; i++) {System.out.print((char) b[i]);}System.out.println("\nTo an OutputStream()");OutputStream f2 = new FileOutputStream("test.txt");f.writeTo(f2);f2.close();System.out.println("Doing a reset");f.reset();for (int i=0; i<3; i++)f.write('X');System.out.println(f.toString());} }

Page 20: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Output• When you run the program, you will create the

following output. Notice how after the call to reset( ), the three X’s end up at the beginning.

Buffer as a stringThis should end up in the arrayInto arrayThis should end up in the arrayTo an OutputStream()Doing a resetXXX

Page 21: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Java in Android

Page 22: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Forms of Java• JSP- Creating dynamic web pages.

• Java Applets -Used within a web page to add many new features to a web browser.

• Mobile Java –Primitively, Mobile Information Devices Profile (MIDP) used Java run time environment in cell phones, mobile tracking systems and other traditional PDA devices. Eg- Nokia, Sony Ericsson, Motorola, etc.

• JavaBeans –Used in commercial softwares, to create high-end applications.

• J2EE– Lighter version of Java. Now a days used in coding games and apps for latest backgrounds(mostly Mobile).

Page 23: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Java: Your Android programming language

• Java is the Basic programing language behind Android.

• The J2EE form of Java is used in Android.• All the syntax of Java are directly applicable in

Android without much changes.• The Effect from the response to the touch, to

the playing of complex games is controlled by Java..

Page 24: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

FileOutputStream in Android

• When we use text editor in Android, the file output stream constructors are called.• The default name is “Untitled.txt”, if the file exists, the text is saved as “Untitled-1.txt” and so on.• If some name is mentioned, if the file exists, it is opened, if it does not exist, a new file is created.• That is how documents are created in Android.• The Android takes this concept to the further level and allows images too to be handled in the same way as text.

Page 25: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Byte Array Streams

• A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. • ByteArrayOutputStream implements an output stream in which the data is

written into a byte array.• When typing messages the above two classes are used.• When we type the text, it is temporarily stored in the buffer by the ByteArrayInputStream and when we confirm send, the text is copied from the ByteArrayInputStream, converted into a string using the ByteArrayOutputStream and send to the receiver.

Page 26: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

Conc

lusi

on• Android is the Windows of the Mobile World.• Without its knowledge, the Dreams to make it

BIG in the field of IT could become tough.• Dependent on Java.• As basic Activities like formatting documents and

sending texts, mails are undertaken only by the tags undertaken in the presentation, hence it becomes important to understand them and implement them.

Page 27: Java- Android, FileOutputStream, ByteArrayInputStream, ByteArrayOutputStream

THANK YOU FOR LISTENING.

PPT CAN BE DOWNLOADED FROM:-

www.karantrehan.comwww.virajmali.com