Presentation is loading. Please wait.

Presentation is loading. Please wait.

Streams input, output, FILE I/O, tokenization, serialization and compression.

Similar presentations

Presentation on theme: "Streams input, output, FILE I/O, tokenization, serialization and compression."— Presentation transcript:

1 Streams input, output, FILE I/O, tokenization, serialization and compression

2 What is a Stream? an unformatted sequence of bytes There are several stream oriented classed in the package. –InputStream – interface –OutputStream – interface –FileInputStream implements InputStream

3 Why would I want a stream? To input and output data. To perform elementary data processing Streaming data is data that flows without stopping. Higher performance Binary data processing

4 What kind of Streams are there? Two kinds: –InputStream (FileInputStream) –OutputStream (FileOutputStream) –you need a File instance to make a new instance of a FileInputStream or a FileOutputStream

5 Byte Streams

6 Introducing

7 Where are streams used? File IO Socket IO Web-based IO Concurrent computing Distributed computing keyboard and terminal printer and the scanner

8 Streams for input and output Abstract base classes for input and output functionality. Defines basic set of methods. Used to get data from files, other objects, etc. Two base structures: 1.InputStream/OutputStream Byte streams 8-bit 2.Reader/Writer Character streams 16-bit UNICODE Internationalization Efficiency Buffer (and not byte) operations Better locking scheme

9 9 Streams and I/O basic classes for file IO –FileInputStream, for reading from a file –FileOutputStream, for writing to a file Example: Open a file "myfile.txt" for reading FileInputStream fis = new FileInputStream("myfile.txt"); Open a file "outfile.txt" for writing FileOutputStream fos = new FileOutputStream ("myfile.txt");

10 InputStream methods int read() int read(byte[] ) int read(byte[], int, int) void close int available() skip(long)

11 OutputStream methods void write(int) void write(byte[] ) void write(byte[], int, int) void close() void flush()

12 Basic Stream Classes FileInputStream FileOutputStream DataInputStream –byte readByte(), long readLong(), double readDouble() DataOutputStream –writeByte(byte), writeLong(long), writeDouble(double) PipedInputStream - PipedOutputStream

13 How do I use a FileInputStream? public static FileInputStream getFileInputStream(String prompt) { try { return new FileInputStream(getReadFile(prompt)); } catch (IOException e) { System.out.println("Er: FileOutputStream in"); } return null; }

14 14 Display File Contents import*; public class FileToOut1 { public static void main(String args[]) { try { FileInputStream infile = new FileInputStream("testfile.txt"); byte buffer[] = new byte[50]; int nBytesRead; do { nBytesRead =; System.out.write(buffer, 0, nBytesRead); } while (nBytesRead == buffer.length); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read failed"); } }

15 copying a file... public static void binaryCopyFile(FileInputStream fis, FileOutputStream fos) throws IOException { byte buffer[] = new byte[512]; int count; while ((count = > 0) fos.write(buffer, 0, count); }

16 Byte Example (EOF) import*; public class CopyBytes { public static void main(String[] args) throws IOException { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); FileInputStream in = new FileInputStream(inputFile); FileOutputStream out = new FileOutputStream(outputFile); int c; while ((c = != -1) //EOF condition test out.write(c); in.close(); out.close(); }

17 Working with Filter Streams Filters data being read from or written to a stream Some streams buffer data, some count data, others convert data to another form. –DataInputStream & DataOutputStream –BufferedInputStream & BufferedOutputStream –LineNumberInputStream –PushbackInputStream (Character based) –PrintStream (This is an output stream.)

18 DataInputStream & DataOutputStream Conceptually, data looks like this, although in binary form (non-text): Java T-shirt Java Mug The data might have been created with the following: DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.bin")); for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); //out.writeChar('\t'); out.writeInt(units[i]); //out.writeChar('\t'); out.writeChars(descs[i]); out.writeChar('\n'); } out.close();

19 DataInputStream in = new DataInputStream(new FileInputStream("invoice1.bin")); // break on EOF while (true) { price = in.readDouble(); unit = in.readInt(); desc = new StringBuffer(20); //end of line (carriage return linefeed) char lineSep = System.getProperty("line.separator").charAt(0); char chr; while (((chr = in.readChar()) != lineSep) && chr != -1) { desc.append(chr); } if (chr == -1) break; System.out.println("You've ordered " + unit + " units of “ + desc + " at $" + price); total = total + unit * price; } System.out.println("For a TOTAL of: $" + total); in.close();

20 write out the bytes public static boolean writeBytes(File f, byte b[]) { FileOutputStream fos = null; try { fos = new FileOutputStream(f); fos.write(b); fos.close(); return true; } catch (IOException e) { System.out.println("Futil.writeBytes,Could not open" + f); return false; }

21 How big is that file? public static int available(File file) { FileInputStream fis = null; int sizeInBytes = -1; try { fis = new FileInputStream(file); sizeInBytes = fis.available(); fis.close(); } catch (IOException e) { System.out.println("Futil:Could not open file"); } return sizeInBytes; }

22 22 Filters Once a stream (e.g., file) has been opened, we can attach filters Filters make reading/writing more efficient Most popular filters: For basic types: DataInputStream, DataOutputStream For objects: ObjectInputStream, ObjectOutputStream

23 23 Writing data to a file using Filters import*; public class GenerateData { public static void main(String args[]) { try { FileOutputStream fos = new FileOutputStream("stuff.dat"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(2); dos.writeDouble( ); dos.writeDouble( ); dos.close(); fos.close(); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read or write failed"); }

24 24 Reading data from a file using filters import*; public class ReadData { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream("stuff.dat"); DataInputStream dis = new DataInputStream(fis); int n = dis.readInt(); System.out.println(n); for( int i = 0; i < n; i++ ) { System.out.println(dis.readDouble()); } dis.close(); fis.close(); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read or write failed"); }

25 Random-Access Files A program can start reading or writing a random-access file at any place and read or write any number of bytes at a time. “Random-access file” is an abstraction: any file can be treated as a random-access file. You can open a random-access file both for reading and writing at the same time.

26 Random Access Files new RandomAccessFile(“data.txt", "rw"); int skipBytes(int) --Moves the file pointer forward the specified number of bytes void seek(long) --Positions the file pointer just before the specified byte long getFilePointer() --Returns the current byte location of the file pointer The RandomAccessFile class implements both the DataInput and DataOutput interfaces Does not extend stream class No support for Serialized Objects

27 Random-Access Files (cont’d) A binary file containing fixed-length data records is suitable for random-access treatment. A random-access file may be accompanied by an “index” (either in the same or a different file), which tells the address of each record. Tape : CD == Stream : Random-access

28 Serialization a way to write instance to a stream Anything that implements the interface serializable can be serialized Most java core classes implement serializable. All primitive data types can be serialized

29 29 Object serialization Write objects to a file, instead of writing primitive types. Use the ObjectInputStream, ObjectOutputStream classes, the same way that filters are used.

30 Why do I need serialization? Persistence (data storage) Transmission via a stream comparison Distributed computing (CORBA, RMI, etc.) serialization is an application of streams.

31 ObjectOutputStream (Serialization) FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject("Today"); s.writeObject(new Date()); s.flush(); The writeObject method throws a NotSerializableException if it's given an object that is not serializable. An object is serializable only if its class implements the Serializable interface.

32 ObjectInputStream (Deserialization) FileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject(); ObjectInputStream stream implements the DataInput interface that defines methods for reading primitive data types

33 How can I use serialization? Make an instance of an ObjectOutputStream ObjectOutputStream resides in To read back use ObjectInputStream. Object o =; oos.write(o); Customer c = (Customer)o; //CNFE?

34 34 Write an object to a file import*; import java.util.*; public class WriteDate { public WriteDate () { Date d = new Date(); try { FileOutputStream f = new FileOutputStream("date.ser"); ObjectOutputStream s = new ObjectOutputStream (f); s.writeObject (d); s.close (); } catch (IOException e) { e.printStackTrace(); } public static void main (String args[]) { new WriteDate (); }

35 35 Read an object from a file import java.util.*; public class ReadDate { public ReadDate () { Date d = null; ObjectInputStream s = null; try { FileInputStream f = new FileInputStream ("date.ser"); s = new ObjectInputStream (f); } catch (IOException e) { e.printStackTrace(); } try { d = (Date)s.readObject (); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvalidClassException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (OptionalDataException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println ("Date serialized at: "+ d); } public static void main (String args[]) { new ReadDate (); } }

36 How do I control what is saved? I need a special java keyword for this! transient is a keyword...for example class Customer implements Serializable { transient String name = null; }

37 Package CheckedInputStream & CheckedOutputStream An input and output stream pair that maintains a checksum as the data is being read or written. DeflaterOutputStream & InflaterInputStream Compresses or uncompresses the data as it is being read or written. GZIPInputStream & GZIPOutputStream Reads and writes compressed data in the GZIP format. ZipInputStream & ZipOutputStream Reads and writes compressed data in the ZIP format

38 Save a gzip file public void saveGzDb(final File writeFile) throws IOException { if (writeFile == null) return; FileOutputStream fos = new FileOutputStream(writeFile); GZIPOutputStream gos = new GZIPOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(gos); oos.writeObject(addressVector); oos.flush(); oos.close(); gos.finish(); fos.close(); System.out.println("saved to:"+writeFile); }

39 Read in the gzip object FileInputStream fis = new FileInputStream(readFileName); GZIPInputStream gis = new GZIPInputStream(fis); ObjectInputStream ois = new ObjectInputStream(gis); final Object o = ois.readObject(); addressVector = (Vector) o;

40 Playing Audio from a File audio is stored in a file format. One way to open and play the audio is via the ULawCodec. The ULawCodec resides in the sound package. Run the main and it will open and play an au file.

41 Project ideas write a java program that will prompt the user for a directory containing AU files. Play all au files contained in the directory or the subdirectories from the root selected by the users. Use the sound.UlawCodec class to help you. You can use the web to get multiple au files.

42 Whats a codec? Coder + decoder = codec. Why do I need a codec? How many codec do I need? Can I writing my own codec? When should I write my own codec?

43 How does the Ulaw Codec work? public UlawCodec() { String fileName = Futil.getReadFileName( "select an au file"); readAUFile(fileName); }

44 How do you get the bytes from a file? private void readUlawDataFromAFile(String fileName) throws IOException { FileInputStream fis = new FileInputStream(fileName); ulawData = readData(fis); fis.close(); }

45 Reading from an input stream public static byte[] readData(InputStream is) throws IOException { AudioStream as = new AudioStream(is); int length = as.getLength(); byte b[] = new byte[length];, 0, length); return b; }

46 How do you play the sound? public static void playFromFile() { UlawCodec ulc = new UlawCodec();; //ulc.writeAUFile(); }

47 How does play work? public void play() { stop(); AudioData audioData = new AudioData(ulawData); audioDataStream = new AudioDataStream(audioData); AudioPlayer.player.start(audioDataStream); }

48 What is audioData? a class in package; public class AudioData { AudioFormat format; byte[] buffer; public AudioData(byte[] bytes) { /* compiled code */ } AudioData(AudioFormat audioFormat, byte[] bytes) { /* compiled code */ } }

49 What is an AudioDataStream? public class AudioDataStream extends ByteArrayInputStream { AudioData ad; public AudioDataStream(AudioData audioData) { /* compiled code */ } AudioData getAudioData() { /* compiled code */ } }

50 What is an AudioPlayer? public class AudioPlayer extends Thread { private AudioDevice devAudio; private static boolean DEBUG; public static final AudioPlayer player; private static ThreadGroup getAudioThreadGroup() { /* compiled code */ } private static AudioPlayer getAudioPlayer() { /* compiled code */ } private AudioPlayer() { /* compiled code */ } public synchronized void start(InputStream inputStream) { /* compiled code */ } public synchronized void stop(InputStream inputStream) { /* compiled code */ } public void run() { /* compiled code */ } AudioPlayer($1 audioPlayer$1) { /* compiled code */ }

Download ppt "Streams input, output, FILE I/O, tokenization, serialization and compression."

Similar presentations

Ads by Google