JAVA Programming Language Chapter 9 InputOutput with Java

  • Slides: 28
Download presentation
JAVA Programming Language Chapter 9. Input/Output with Java. io Juho Kim Department of Computer

JAVA Programming Language Chapter 9. Input/Output with Java. io Juho Kim Department of Computer Science and Engineering Sogang University 9 -1

java. io package Provides for system input and output through data streams, serialization and

java. io package Provides for system input and output through data streams, serialization and the file system. Java Virtual Machine connects data stream to input/output hardware. Data stream makes JAVA I/O independent of machines File and Directory File. Writer File. Reader class Buffered. Writer Buffered. Reader class File. Output. Stream File. Input. Stream class Buffered. Output. Stream Buffered. Input. Stream class Data. Output. Stream Data. Input. Stream class 9 -2

File class File and Directory – File class: An abstract representation of file and

File class File and Directory – File class: An abstract representation of file and directory pathnames. User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames • Constructor File(File parent, String child) Creates a new File instance from a parent abstract pathname and a child pathname string. File(String pathname) Creates a new File instance by converting the given pathname string into an abstract pathname File(String pathname, String filename) Creates a new File instance by pathname string and filename string 9 -3

 • Methods boolean can. Read() if readable return true boolean can. Write() if

• Methods boolean can. Read() if readable return true boolean can. Write() if write allowed return true boolean delete() delete file and return true boolean exists() String get. Absolute. Path() return pathname String get. Parent() return parent directory name String get. Name() return file name boolean is. File() boolean is. Directory() long last. Modified() return milliseconds from 1970/1/1 until last modified time String[] list() return filenames from directory 9 -4

File. Dir. Demo 1. java import java. io. File; class File. Dir. Demo 1

File. Dir. Demo 1. java import java. io. File; class File. Dir. Demo 1 { public static void main(String args[]) { String directory = "c: /jdk 1. 4"; File f 1 = new File(directory); if (f 1. is. Directory()) { System. out. println("Directory : " + directory); System. out. println(" "); String s[] = f 1. list(); for(int i=0; i<s. length; i++) { File f = new File(directory + "/" + s[i]); if (f. is. Directory()) { System. out. println(s[i] + " : directory"); } else { System. out. println(s[i] + " : file"); }}} else { System. out. println(“this" +directory + " is not a directory"); } }} 9 -5

File. Dir. Demo 2. java import java. io. File; class File. Dir. Demo 2

File. Dir. Demo 2. java import java. io. File; class File. Dir. Demo 2 { static void p(String s) { System. out. println(s); } public static void main(String args[]) { File f 1 = new File("c: /jdk 1. 4/README. txt"); p("f 1. get. Name(): " + f 1. get. Name()); p("f 1. get. Path(): " + f 1. get. Path()); p("f 1. get. Absolute. Path(): " + f 1. get. Absolute. Path()); p("f 1. exists(): " + f 1. exists()); p("f 1. can. Write(): " + f 1. can. Write()); p("f 1. can. Read(): " + f 1. can. Read()); p("f 1. is. Directory(): " + f 1. is. Directory()); p("f 1. is. File(): " + f 1. is. File()); p("f 1. last. Modified(): " + f 1. last. Modified()); p("f 1. length(): " + f 1. length() + " bytes"); }} 9 -6

Fire. Writer and File. Reader class – Constructor File. Writer(String filepath) throws IOException File.

Fire. Writer and File. Reader class – Constructor File. Writer(String filepath) throws IOException File. Writer(String filepath, boolean append) throws IOException File. Writer(File file. Object) throws IOException File. Reader(String filepath) File. Reader(File file. Object) 9 -7

– Methods void close() closing input/output stream void flush() sending data in output buffer

– Methods void close() closing input/output stream void flush() sending data in output buffer to output device void write(int c)output lower 16 -bit of c void write(char buffer[]) output from buffer array void write(char buffer[], int index, int size) void write(String s, int index, int size) int read() return character, if end of file return -1 int read(char buffer[]) int read(char buffer[], int offset, int num. Chars) 9 -8

File. Writer. Demo. java import java. io. *; class File. Writer. Demo { public

File. Writer. Demo. java import java. io. *; class File. Writer. Demo { public static void main(String args[]) throws Exception { String source = "File Writer n. Demonstration"; char intxt[] = new char[source. length()]; source. get. Chars(0, source. length(), intxt, 0); File. Writer fw = new File. Writer(args[0]); fw. write(intxt); fw. close(); } } 9 -9

File. Reader. Demo. java import java. io. *; class File. Reader. Demo { public

File. Reader. Demo. java import java. io. *; class File. Reader. Demo { public static void main(String args[]) throws Exception { File. Reader fr = new File. Reader(args[0]); int i; while((i = fr. read()) != -1) { System. out. print((char) i); } fr. close(); } } 9 -10

Buffered. Writer and Buffered. Reader class – Buffered. Writer class Write text to a

Buffered. Writer and Buffered. Reader class – Buffered. Writer class Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings. The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes. – Buffered. Reader class Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. 9 -11

– Constructor Buffered. Writer(Writer output. Stream) Buffered. Writer(Writer output. Stream, int buf. Size) Buffered.

– Constructor Buffered. Writer(Writer output. Stream) Buffered. Writer(Writer output. Stream, int buf. Size) Buffered. Reader(Reader input. Stream, int buf. Size) Writer class : Abstract class for writing to character streams Reader class : Abstract class for reading to character streams 9 -12

Buffered. Writer. Demo. java import java. io. *; class Buffered. Writer. Demo { public

Buffered. Writer. Demo. java import java. io. *; class Buffered. Writer. Demo { public static void main(String args[]) throws Exception { String source = "Buffered Writer n. Demonstration"; char intxt[] = new char[source. length()]; source. get. Chars(0, source. length(), intxt, 0); File. Writer fw = new File. Writer(args[0]); Buffered. Writer bw = new Buffered. Writer(fw); bw. write(intxt); bw. close(); } } 9 -13

Buffered. Reader. Demo. java import java. io. *; class Buffered. Reader. Demo { public

Buffered. Reader. Demo. java import java. io. *; class Buffered. Reader. Demo { public static void main(String args[]) throws Exception { File. Reader fr = new File. Reader(args[0]); Buffered. Reader br = new Buffered. Reader(fr); String s; while((s = br. read. Line()) != null) { System. out. print(s); } br. close(); } } 9 -14

File. Output. Stream and File. Input. Stream – Output. Stream class • This abstract

File. Output. Stream and File. Input. Stream – Output. Stream class • This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. • Applications that need to define a subclass of Output. Stream must always provide at least a method that writes one byte of output. – Input. Stream class • This abstract class is the superclass of all classes representing an input stream of bytes. • Applications that need to define a subclass of Input. Stream must always provide a method that returns the next byte of input. 9 -15

– File. Output. Stream java. lang. Object | + --java. io. Output. Stream |

– File. Output. Stream java. lang. Object | + --java. io. Output. Stream | + --java. io. File. Output. Stream public class File. Output. Stream extends Output. Stream A file output stream is an output stream for writing data to a File or to a File. Descriptor. What files are available or may be created depends on the host environment. 9 -16

– Constructor File. Output. Stream(String filepath) throws IOException filepath constains pathname and filename File.

– Constructor File. Output. Stream(String filepath) throws IOException filepath constains pathname and filename File. Output. Stream(String filepath, boolean append) throws IOException if append is true, append at the end of the file otherwise, overwirte File. Output. Stream(File file. Object) throws IOException file. Object is a specific file 9 -17

– File. Input. Stream class java. lang. Object | + --java. io. Input. Stream

– File. Input. Stream class java. lang. Object | + --java. io. Input. Stream | + --java. io. File. Input. Stream public class File. Input. Stream extends Input. Stream A File. Input. Stream obtains input bytes from a file in a file system. What files are available depends on the host environment. – Constructor File. Input. Stream(String filepath) throws File. Not. Found. Exception File. Input. Stream(File file. Object) throws File. Not. Found. Exception 9 -18

File. Output. Stream. Demo. java import java. io. *; class File. Output. Stream. Demo

File. Output. Stream. Demo. java import java. io. *; class File. Output. Stream. Demo { public static void main(String args[]) throws IOException { File. Output. Stream fos = new File. Output. Stream(args[0]); for(int i = 0; i < 5; i++) { fos. write(i); } fos. close(); System. out. println("Byte. Stream. File is created"); } } 9 -19

File. Input. Stream. Demo. java import java. io. *; class File. Input. Stream. Demo

File. Input. Stream. Demo. java import java. io. *; class File. Input. Stream. Demo { public static void main(String args[]) throws IOException { File. Input. Stream fis = new File. Input. Stream(args[0]); int i; while((i = fis. read()) != -1) { System. out. println(i); } fis. close(); System. out. println("Byte. Stream. File is read and printed"); } } 9 -20

Buffered. Output. Stream and Buffered. Input. Stream class – The class Buffered. Output. Stream

Buffered. Output. Stream and Buffered. Input. Stream class – The class Buffered. Output. Stream implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written. The data is written into an internal buffer, and then written to the underlying stream if the buffer reaches its capacity, the buffer output stream is closed, or the buffer output stream is explicitly flushed. – When the Buffered. Input. Stream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. 9 -21

Buffered. Output. Stream. Demo. java import java. io. *; class Buffered. Output. Stream. Demo

Buffered. Output. Stream. Demo. java import java. io. *; class Buffered. Output. Stream. Demo { public static void main(String args[]) throws IOException { File. Output. Stream fos = new File. Output. Stream(args[0]); Buffered. Output. Stream bos = new Buffered. Output. Stream(fos); for(int i = 0; i < 5; i++) { bos. write(i); } bos. close(); System. out. println("Byte. Stream. File is created"); } } 9 -22

Buffered. Input. Stream. Demo. java import java. io. *; class Buffered. Input. Stream. Demo

Buffered. Input. Stream. Demo. java import java. io. *; class Buffered. Input. Stream. Demo { public static void main(String args[]) throws IOException { File. Input. Stream fis = new File. Input. Stream(args[0]); Buffered. Input. Stream bis = new Buffered. Input. Stream(fis); int i; while((i = bis. read()) != -1) { System. out. println(i); } bis. close(); System. out. println("Byte. Stream. File is read and printed"); } } 9 -23

Data. Output. Stream and Data. Input. Stream class – A data output stream lets

Data. Output. Stream and Data. Input. Stream class – A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in. – A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. – Constructor Data. Output. Stream(Output. Stream output. Stream) Data. Input. Stream(Input. Stream input. Stream) 9 -24

9 -25

9 -25

9 -26

9 -26

Data. Output. Stream. Demo. java import java. io. *; class Data. Output. Stream. Demo

Data. Output. Stream. Demo. java import java. io. *; class Data. Output. Stream. Demo { public static void main(String args[])throws IOException { File. Output. Stream fos = new File. Output. Stream(args[0]); Data. Output. Stream dos = new Data. Output. Stream(fos); dos. write. Boolean(false); dos. write. Byte(Byte. MAX_VALUE); dos. write. Char('A'); dos. write. Double(Double. MAX_VALUE); dos. write. Float(Float. MAX_VALUE); dos. write. Int(Integer. MAX_VALUE); dos. write. Long(Long. MAX_VALUE); dos. write. Short(Short. MAX_VALUE); fos. close(); } } 9 -27

Data. Input. Stream. Demo. java import java. io. *; class Data. Input. Stream. Demo

Data. Input. Stream. Demo. java import java. io. *; class Data. Input. Stream. Demo { public static void main(String args[])throws IOException { File. Input. Stream fis = new File. Input. Stream(args[0]); Data. Input. Stream dis = new Data. Input. Stream(fis); System. out. println("Boolean : " + dis. read. Boolean()); System. out. println("Byte. MAX_VALUE : " + dis. read. Byte()); System. out. println("Character : " + dis. read. Char()); System. out. println("Double. MAX_VALUE : " + dis. read. Double()); System. out. println("Float. MAX_VALUE : " + dis. read. Float()); System. out. println("Integer. MAX_VALUE : " + dis. read. Int()); System. out. println("Long. MAX_VALUE : " + dis. read. Long()); System. out. println("Short. MAX_VALUE : " + dis. read. Short()); fis. close(); } } 9 -28