SlideShare a Scribd company logo
1 of 26
Java Presentation Materials
αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី
Korea Software HRD Center
αž£αŸ’αž“αž€αž”αŸ’αžšαžΉαž€αžΆαžšαŸ„αžšαž›αŸ‹: αžšαžŽαŸ’αžŒ ិត αž‚αžΈαž˜β€‹αžšαŸαžαŸ’αž™αž» αž„
www.kshrd.com.kh
αž£αŸ’αž“αž€αžœαžŽαŸ’αž“αŸ†αžΆ: αžšαŸ„αž€ αŸ„αž„ αžšαŸŠαž»αž“ αŸ‰αŸαŸŠαž»αž„
αžšαŸ„αž€ αž›αž“αŸ‹ αžŸαŸŠαž»αŸ‚αžαŸ’αžαžΆ αž“
αžšαŸ„αž€ αžšαŸαž„ αžαŸŠαž»αŸ„
αžšαŸ„αž€ αžŠαžΆαžšαŸ‰αŸ αžšαŸαž‰αž…αž·αžαž
αžαŸ’αž“αž“ αž€αŸ‹ αžšαžŸαŸ€αž˜αžšαžš αž”αŸ’αž€ αž»αž˜αž‘αžΈ ៣
αž”αŸ’αžšαž’αžΆαž“αžšαž‘αŸ– FileI/O
αžŸαž˜αžΆαž‡αŸ’αž·αž€
1. αž›αŸ„αž€ αž›αž“αŸ‹ αžŸαž»αžœαžαŸ’αžαžΆ αž“αžΆ
2. αž›αŸ„αž€ αž•αŸ‰αžΆ αž“αŸ‹ αž—αžΈαžšαŸ‰αž„αŸ‹
3. αž›αŸ„αž€ αž’αžΆαŸ†αž„ αžαžΉαž€αž‡αž»αž“
4. αž€αž‰αŸ’αž‰αžΆ αž› αŸ€αž αž˜αŸ‰αžΆ αž“αž·αž
αž˜αžΆαžαž·αž€αžΆ
1. Path Class/File Class
2. Read/Write File
3. FileInputStream/FileOutputStream
4. FileReader/FileWriter
3
5. InputStream/OutputStream
6. InputStreamReader/OutputStreamWriter
7. Serialization
8. Random Access
1. Path Class and File Class
4
β€’ Path Class αž‚αžΉαž‡αžΆ class αž˜αž½αž™αžŠαŸ‚αž›αž•αŸ’αž‘αž»αž€αž‘αŸ…αž‘αŸ„αž™ method αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž‘αŸ’αžœαžΎαž€αžΆαžš αž‡αžΆαž˜αž½αž™ information αžšαž”αžŸαŸ‹ Path αž‘αŸ…αž€αž“αž»αž„ File
System αŸ” Path object αžΆαž“ file name αž“αž·αž„ directory list αžŸαž˜αŸ’αžšαžΆαž”αŸ‹ αž€αžΆαžšαž”αž‘αž„αž€αžΎαž Path, αž€αžΆαžšαŸ„αž€αŸ‹αž‘αžΈαžαžΆαŸ†αž„ file αž“αž·αž„ αž˜αŸ’αžšαžαž½αžαž–αž·αž“αž·
αžαž™ file/directoryαŸ”
Method and Description
get(String first, String... more) Converts a path string, or a sequence of strings that
when joined form a path string, to a Path.
get(URI uri) Converts the given URI to a Path object.
5
β€’ Create Path
import java.io.file.Path;
import java.io.file.Paths;
public class CreatePath {
private Path path; // instance variable
public static void main (String [] args) {
CreatePath example = new CreatePath();
example.createPath();
}
private void createPath() {
path = Paths.get("D:JCGarticlesPathfile1.txt");
System.out.println("Path created: " + path.toString());
}
}
1. Path Class and File Class
1. Path Class and File Class
6
β€’ Java File Class αž‘αž˜αŸ’αžšαž”αžΎαž‘αŸ‚αžΎαž˜αž”αžΈαž‘αŸ’αžœαžΎαž€αžΆαžšαž‡αžΆαž˜αž½αž™ file αž“αž·αž„ directory pathnamesαŸ” αž‘ αžΎαž™αž€αžΆαžšαžšαž”αžŸαŸ‹αžœαžΆαž‚αžΉαžŸαž˜αŸ’αžšαžΆαž”αŸ‹ αž”αž‘αž„αž€αžΎαž files αž“αž·αž„
directories, file searching, file deletion .
S.N. Constructor & Description
1 File(File parent, String child)
This method creates a new File instance from a parent abstract pathname and a child pathname string.
2 File(String pathname)
This method creates a new File instance by converting the given pathname string into an abstract
pathname.
3 File(String parent, String child)
This method creates a new File instance from a parent pathname string and a child pathname string.
4 File(URI uri)
This method Creates a new File instance by converting the given file : URI into an abstract pathname.
1. Path Class and File Class
7
β€’ Create New file β€œtest.txt”
import java.io.File;
public class CreateFile {
public static void main(String[] args) {
try {
File new_file = new File("test.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. Read/Write File
8
 Read File : αžŠαžΆαŸ†αž› αžΎαžšβ€‹αž€αžΆαžšβ€‹αž’αž“β€‹αž‘αž·αž“αž“αž“αŸαž™β€‹αž–αžΈβ€‹File
 Write File : αžŠαžΆαŸ†αž› αžΎαžšβ€‹αž€αžΆαžšαžŸαžšαž›αžŸαžšβ€‹/αž› αŸ„αŸ‡β€‹β€‹αž‘αž·αž“αž“αž“αŸαž™αž–αžΈβ€‹File
9
Read/ write File αž˜αž“β€‹:
β€’ Read/Write all Bytes or Line from/to File : for small file
2. Read/Write File
10
β€’ Read/Write File Using Buffer Stream I/O : Read/Write TEXT from/to File
β€’ Read/Write File Using Stream I/O : Read/Write BYTE stream from/to File
β€’ Read/Write File Using Channel I/O : Read/Write TEXT from/to File
(Channel I/O read/write a buffer at a time)
2. Read/Write File
3. FileInputStream / FileOutputStream
11
FileInputStream αž‚αžΊαž‡αžΆ Class αž˜αž½αž™αžŠαžŠαž›αž›αŸ’αžšαžΎαžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Read file αž‡αžΆ Byte Stream. αž›αŸ…αž€αž“ αž»αž„ FileInputStream αž˜αž“
SubClass αž˜αž½αž™αž›αž‘αŸ€αžαž›αŸ’αž˜ αŸ„αŸ‡ ថអ InputStream. αž…αžΆαŸ† αžŠαž“αž€ FileOutputStream αž‚αžΊαž‡αžΆ Class αž˜αž½αž™αžŠαžŠαž›αž›αŸ’αžšαžΎαžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Write
File αž‡αžΆ Byte Stream. αž›αŸ…αž€αž“ αž»αž„ FileInputStream αž˜αž“ SubClass αž˜αž½αž™αž›αž‘αŸ€αžαž›αŸ’αž˜ αŸ„αŸ‡αžαžΆ OutputStream.
3. FileInputStream / FileOutputStream
12
Method αžŸαŸ†αžΆαžαžΆαž“αŸ‹αŸ— αžšαžŸαŸ‹ FileInputStream/FileOutputStream
β€’ int available() αž‡αžΆMethod αž˜αž½αž™αžŠαžŠαž›αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžšαž–αž·αž“αž·αžαž™ αž›αž˜αžΎαž›αžαžΆαž’αž…Read αž“αž¬ αžαŸ‹αž›αžšαžΎαžœαžΆ Read αž“αž›αž“αžΆαŸ„αŸ‡ return
true αžŠαžαž›αžšαžΎαžœαžΆαž˜αž·αž“αž’αž… Reader αž“αž›αž“αžΆαŸ„αŸ‡αžœαžΆ Return false.
β€’ void close() αž‡αžΆ Method αž˜αž½αž™αžŠαžŠαž› closes file input stream αž› αžΎαž™αž“αž·αž„ resources αž›αžŸαŸαž„αž›αž‘αŸ€αžαžŠαžŠαž›αž˜αž“αž‘αžΆαŸ†αž“αžΆαž€αŸ‹
αž‘αžΆαŸ†αž“αž„ αž“αž·αž„stream.
β€’ int read() αž‡αžΆ method reads a byte of data from this input stream.
4. FileReader / FileWriter
13
β€’ FileReader (java.io.Reader)αž‚αžΊαž‡αžΆ base class αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Read subclasses in the Java IO API. FileReader
β€’ αžœαžΆαž€αŸαžŠαžΌαž…αž›αŸ…αž“αž·αž„ InputStreamReader αžŠαžαžœαžΆαžαž»αžŸαž‚αŸ’αž“αž“ αŸ’αžαž„αŸ‹αžαžΆ FileReader αž›αŸ’αžœαžΎαž€αžΆαžšβ€‹Read αž‡αžΆText ដត
InputStreamReader វអRead αž‡αžΆ Byte Stream
β€’ αž…αžΆαŸ† αžŠαž“αž€ FileWriter (java.io.Writer) αž‚αžΊαž‡αžΆ base class αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Write subclasses in the Java IO API
FileWriter αžœαžΆαž€αŸαžŠαžΌαž…αž›αŸ…αž“αž·αž„ InputStreamWriter αžŠαžαžœαžΆαžαž»αžŸαž‚αŸ’αž“αž“ αŸ’αžαž„αŸ‹αžαžΆ FileWriter αž›αŸ’αžœαžΎαž€αžΆWrite αž‡αžΆText
ដតInputStreamReader វអWriteαž‡αžΆ Byte Stream.
4. FileReader / FileWriter
14
FileReader
FileWriter
5. InputSream / OutputStream
15
Java Application αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ input stream αž›αžŠαžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš read data αž–αžΈ source αž“αž·αž„ αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ output stream
αž›αžŠαžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš write data αž›αŸ…αž€αžΆαž“αŸ‹ destinationαŸ”
InputStream Class and OutputStream Class:αž‚αžΊαž‡αžΆ abstract class αžŠαžŠαž›αž‡αžΆ subclass αžšαžšαžŸαŸ‹ classes αž‘αžΆαŸ†αž„ αžŸαŸ‹
αžŠαžŠαž›αžαžΆαŸ†αžŽαžΆαž„αž›αž’αž™ input stream αžšαžšαžŸαŸ‹ bytes
αžŠαŸ†αžΆαžšαžŽαŸ’αžΎ αž€αžΆ αžšαžŸαŸ‹ inputstream αž“αž·αž„ outputstream
Method of InputStream Method of OutputStream
1) public abstract int read()throws
IOException:
1) public void write(int)throws IOException:
2) public int available()throws IOException: 2) public void write(byte[])throws IOException:
3) public void close()throws IOException: 3) public void close()throws IOException:
5. InputSream / OutputStream
16
Hairachy of InputStream/OutputStream
6. InputSreamReader/OutputStreamWriter
17
Java.io.InputStreamReader class αž‚αžΊαž‡αžΆαžŸαŸ’αž–αžΆ αž“αž…αž˜αž›αž„αž–αžΈ byte streams αž›αŸ…αž€αžΆαž“αŸ‹ character streams
αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžš read αž–αžΈ byte αž› αžΎαž™ decode αž‡αžΆ character αž›αŸ„αž™αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ charset
αž…αžΆαŸ† αžŠαž“αž€Java.io.OutputStreamWriter αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžš write αž› αžΎαž™ encode αž›αŸ…αž‡αžΆ byte β€‹αžœ αž‰ αž›αŸ„αž™αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ charsetαŸ”
S.N. InputStreamReader: Constructor & Description OutputStreamWriter: Constructor & Description
1 InputStreamReader(InputStream in)
This creates an InputStreamReader that uses the default charset.
OutputStreamWriter(OutputStream out)
This creates an OutputStreamWriter that uses the default character
encoding.
2 InputStreamReader(InputStream in, Charset cs)
This creates an InputStreamReader that uses the given charset.
OutputStreamWriter(OutputStream out, Charset cs)
This creates an OutputStreamWriter that uses the given charset.
3 InputStreamReader(InputStream in, CharsetDecoder dec)
This creates an InputStreamReader that uses the given charset
decoder.
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
This creates an OutputStreamWriter that uses the given charset
encoder.
4 InputStreamReader(InputStream in, String charsetName)
This creates an InputStreamReader that uses the named charset.
OutputStreamWriter(OutputStream out, String charsetName)
This creates an OutputStreamWriter that uses the named charset.
7. Serialization
18
β€’ Serialization αž‚αžΊαž‡αžΆαž˜αŸ‰αžΆ αžŸαž»αžΈαž“ αž“αž“αž€αžΆαžš αžŸαžšαž›αžŸαžšαž“αžΌαžœ State αž“αž“ Object αž›αŸ…αž‡αžΆ Byte stream.
β€’ αŸ’αž–αž˜αž‘αžΆαŸ†αž„αž€αžΆαžšαž›αŸ…αž“αžΌαžœ Serialization αž›αž“αŸ„αŸ‡αž˜αž€αžœ αž‰ αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹αž›αŸ…αžαžΆ Deserialization.
β€’ String class αž“αž·αž„ wrapper class αž‘αžΆαŸ†αž„ αžŸαŸ‹αŸ’αžαžΌαžœαžΆ αž“ implements αž–αžΈ java.io.serializable interface
αž›αŸ„αž™ default.
β€’ Classes ObjectInputStream and ObjectOutputStream αž‚αžΊαž‡αžΆ high-level stream αžŠαžŠαž›αžœαžΆαž˜αž“αžŸαž‘ αž»αž€
αž“αžΌαžœ method αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Serialization αž“αž·αž„ Deserialization αžšαžšαžŸαŸ‹ Object.
β€’ Method write Object αžŠαžŠαž›αž›αž‚αž“αž·αž™αž˜αž›αŸ’αžšαžΎαŸ–
β€’ Method serialization αž“αž·αž„ deserialization Object αžŠαžŠαž›αž›αž‚αž“αž·αž™αž˜αž›αŸ’αžšαžΎαŸ–
7. Serialization
19
Example:
Example Serilization Object :
7. Serialization
20
Example Deserializing an Object:
8. RandomacessFile
21
Java.io.RandomAccessfile class αžœαžΆαŸ’αžΆαžšαžšαžΈαžŠαžΌαž…αž‡αžΆ Array αž™αŸ‰αžΆ αž„αŸ’αžΆαŸ†αž˜αž½αž™αžŠαžŠαž› αžŸαž‘ αž»αž€αž‡αžΆ bytes αž›αŸ…αž€αž“ αž»αž„ file
αžŠαžŠαž› αžœαžΆαž’αž…αž‘αž‘αžΌαž› αž“αž‘αžΆαŸ†αž„ reaing αž“αž·αž„ writing αž›αŸ…αž‡αžΆ random access file.
Class constructor:Constructor Descriptio
RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to
to, the file
specified by the File argument.
RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to
to, a file
with the specified name.
8. RandomacessFile
22
Methods Descriptio
void close This method Closes this random access file stream and releases any system
associated with the stream.
FileChannel getChannel This method returns the unique FileChannel object associated with this file.
FileDescriptor getFD This method returns the opaque file descriptor object associated with this stream.
long getFilePointer This method returns the current offset in this file.
long length This method returns the length of this file.
int read This method reads a byte of data from this file.
int readbyte[]b This method reads up to b.length bytes of data from this file into an array of bytes.
int readbyte[]b, intoff, intlen This method reads up to len bytes of data from this file into an array of bytes.
boolean readBoolean This method reads a boolean from this file.
byte readByte This method reads a signed eight-bit value from this file.
8. RandomacessFile
23
β€’ Example:
Result:
Hello world
Closing Stream....
Stream Closed.
8. RandomAcessFile
24
S.N. InputStreamReader: Method & Description OutputStreamWriter: Method & Description
1 void close()
This method closes the stream and releases any system resources
associated with it.
void close()
This method closes the stream, flushing it first.
2 String getEncoding()
This method returns the name of the character encoding being used by
this stream
String getEncoding()
This method returns the name of the character encoding being used by
this stream.
3 int read()
This method reads a single character.
void write(char[] cbuf, int off, int len)
This method writes a portion of an array of characters.
4 int read(char[] cbuf, int offset, int length)
This method reads characters into a portion of an array.
void write(int c)
This method writes a single character.
5 boolean ready()
This method tells whether this stream is ready to be read.
void write(String str, int off, int len)
This method writes a portion of a string.
9. αž”αŸ’αžšαž—αŸαž―αž€αžŸαžΆ
β€’ http://www.tutorialspoint.com/java/io
β€’ http://www.tutorialspoint.com/java/java_files_io.htm
β€’ https://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.htmll
β€’ https://examples.javacodegeeks.com/core-java/nio/file-nio/path/java-nio-file-path-example/
β€’ https://docs.oracle.com/javase/tutorial/essential/io/file.html
β€’ http://www.tutorialspoint.com/java/java_files_io.htm
25
αžŸαžΌαž˜αž’αžšαž‚αž»αžŽ!!!
26

More Related Content

What's hot

Input output streams
Input output streamsInput output streams
Input output streamsParthipan Parthi
Β 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentationAzeemaj101
Β 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streamsHamid Ghorbani
Β 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49myrajendra
Β 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsAnton Keks
Β 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
Β 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
Β 
File Input & Output
File Input & OutputFile Input & Output
File Input & OutputPRN USM
Β 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in JavaCIB Egypt
Β 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
Β 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51myrajendra
Β 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
Β 
Javaiostream
JavaiostreamJavaiostream
JavaiostreamTien Nguyen
Β 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
Β 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
Β 

What's hot (20)

Java stream
Java streamJava stream
Java stream
Β 
Input output streams
Input output streamsInput output streams
Input output streams
Β 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Β 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Β 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
Β 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Β 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Β 
Java I/O
Java I/OJava I/O
Java I/O
Β 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Β 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
Β 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
Β 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
Β 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
Β 
Files in java
Files in javaFiles in java
Files in java
Β 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
Β 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
Β 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Β 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Β 
Java
JavaJava
Java
Β 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Β 

Similar to Java program file I/O

Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
Β 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptxssuser9d7049
Β 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
Β 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdfSudhanshiBakre1
Β 
Input & output
Input & outputInput & output
Input & outputSAIFUR RAHMAN
Β 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2Gera Paulos
Β 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfhemanth248901
Β 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization Hitesh-Java
Β 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, SerializationPawanMM
Β 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptxRathanMB
Β 
Io Streams
Io StreamsIo Streams
Io Streamsphanleson
Β 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
Β 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and outputRiccardo Cardin
Β 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxSadhilAggarwal
Β 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfVithalReddy3
Β 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In JavaRajan Shah
Β 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
Β 

Similar to Java program file I/O (20)

Input output files in java
Input output files in javaInput output files in java
Input output files in java
Β 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
Β 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
Β 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
Β 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
Β 
Input & output
Input & outputInput & output
Input & output
Β 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
Β 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdf
Β 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
Β 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
Β 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
Β 
Io Streams
Io StreamsIo Streams
Io Streams
Β 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
Β 
Java Day-6
Java Day-6Java Day-6
Java Day-6
Β 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
Β 
CHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptxCHAPTER 5 mechanical engineeringasaaa.pptx
CHAPTER 5 mechanical engineeringasaaa.pptx
Β 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
Β 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
Β 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
Β 
JAVA
JAVAJAVA
JAVA
Β 

Recently uploaded

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
Β 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Β 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
Β 
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Β 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
Β 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
Β 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
Β 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
Β 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
Β 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
Β 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
Β 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
Β 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
Β 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
Β 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
Β 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
Β 

Recently uploaded (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
Β 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Β 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Β 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Β 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Β 
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 βœ“Call Girls In Kalyan ( Mumbai ) secure service
Β 
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY πŸ” 8264348440 πŸ” Call Girls in Diplomatic Enclave | Delhi
Β 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Β 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
Β 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Β 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
Β 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
Β 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Β 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Β 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Β 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
Β 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Β 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
Β 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
Β 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Β 

Java program file I/O

  • 1. Java Presentation Materials αž˜αž‡αŸ’αžˆαž˜αžŽαŸ’αžŒ αž›αž€αžΌαžš αŸ‰αŸ αžŸαž αŸ’αžœ αžœαŸ‚ αžšαŸαž… េ ឌី Korea Software HRD Center αž£αŸ’αž“αž€αž”αŸ’αžšαžΉαž€αžΆαžšαŸ„αžšαž›αŸ‹: αžšαžŽαŸ’αžŒ ិត αž‚αžΈαž˜β€‹αžšαŸαžαŸ’αž™αž» αž„ www.kshrd.com.kh αž£αŸ’αž“αž€αžœαžŽαŸ’αž“αŸ†αžΆ: αžšαŸ„αž€ αŸ„αž„ αžšαŸŠαž»αž“ αŸ‰αŸαŸŠαž»αž„ αžšαŸ„αž€ αž›αž“αŸ‹ αžŸαŸŠαž»αŸ‚αžαŸ’αžαžΆ αž“ αžšαŸ„αž€ αžšαŸαž„ αžαŸŠαž»αŸ„ αžšαŸ„αž€ αžŠαžΆαžšαŸ‰αŸ αžšαŸαž‰αž…αž·αžαž
  • 2. αžαŸ’αž“αž“ αž€αŸ‹ αžšαžŸαŸ€αž˜αžšαžš αž”αŸ’αž€ αž»αž˜αž‘αžΈ ៣ αž”αŸ’αžšαž’αžΆαž“αžšαž‘αŸ– FileI/O αžŸαž˜αžΆαž‡αŸ’αž·αž€ 1. αž›αŸ„αž€ αž›αž“αŸ‹ αžŸαž»αžœαžαŸ’αžαžΆ αž“αžΆ 2. αž›αŸ„αž€ αž•αŸ‰αžΆ αž“αŸ‹ αž—αžΈαžšαŸ‰αž„αŸ‹ 3. αž›αŸ„αž€ αž’αžΆαŸ†αž„ αžαžΉαž€αž‡αž»αž“ 4. αž€αž‰αŸ’αž‰αžΆ αž› αŸ€αž αž˜αŸ‰αžΆ αž“αž·αž
  • 3. αž˜αžΆαžαž·αž€αžΆ 1. Path Class/File Class 2. Read/Write File 3. FileInputStream/FileOutputStream 4. FileReader/FileWriter 3 5. InputStream/OutputStream 6. InputStreamReader/OutputStreamWriter 7. Serialization 8. Random Access
  • 4. 1. Path Class and File Class 4 β€’ Path Class αž‚αžΉαž‡αžΆ class αž˜αž½αž™αžŠαŸ‚αž›αž•αŸ’αž‘αž»αž€αž‘αŸ…αž‘αŸ„αž™ method αžŸαž˜αŸ’αžšαžΆαž”αŸ‹αž‘αŸ’αžœαžΎαž€αžΆαžš αž‡αžΆαž˜αž½αž™ information αžšαž”αžŸαŸ‹ Path αž‘αŸ…αž€αž“αž»αž„ File System αŸ” Path object αžΆαž“ file name αž“αž·αž„ directory list αžŸαž˜αŸ’αžšαžΆαž”αŸ‹ αž€αžΆαžšαž”αž‘αž„αž€αžΎαž Path, αž€αžΆαžšαŸ„αž€αŸ‹αž‘αžΈαžαžΆαŸ†αž„ file αž“αž·αž„ αž˜αŸ’αžšαžαž½αžαž–αž·αž“αž· αžαž™ file/directoryαŸ” Method and Description get(String first, String... more) Converts a path string, or a sequence of strings that when joined form a path string, to a Path. get(URI uri) Converts the given URI to a Path object.
  • 5. 5 β€’ Create Path import java.io.file.Path; import java.io.file.Paths; public class CreatePath { private Path path; // instance variable public static void main (String [] args) { CreatePath example = new CreatePath(); example.createPath(); } private void createPath() { path = Paths.get("D:JCGarticlesPathfile1.txt"); System.out.println("Path created: " + path.toString()); } } 1. Path Class and File Class
  • 6. 1. Path Class and File Class 6 β€’ Java File Class αž‘αž˜αŸ’αžšαž”αžΎαž‘αŸ‚αžΎαž˜αž”αžΈαž‘αŸ’αžœαžΎαž€αžΆαžšαž‡αžΆαž˜αž½αž™ file αž“αž·αž„ directory pathnamesαŸ” αž‘ αžΎαž™αž€αžΆαžšαžšαž”αžŸαŸ‹αžœαžΆαž‚αžΉαžŸαž˜αŸ’αžšαžΆαž”αŸ‹ αž”αž‘αž„αž€αžΎαž files αž“αž·αž„ directories, file searching, file deletion . S.N. Constructor & Description 1 File(File parent, String child) This method creates a new File instance from a parent abstract pathname and a child pathname string. 2 File(String pathname) This method creates a new File instance by converting the given pathname string into an abstract pathname. 3 File(String parent, String child) This method creates a new File instance from a parent pathname string and a child pathname string. 4 File(URI uri) This method Creates a new File instance by converting the given file : URI into an abstract pathname.
  • 7. 1. Path Class and File Class 7 β€’ Create New file β€œtest.txt” import java.io.File; public class CreateFile { public static void main(String[] args) { try { File new_file = new File("test.txt"); } catch (Exception e) { e.printStackTrace(); } } }
  • 8. 2. Read/Write File 8  Read File : αžŠαžΆαŸ†αž› αžΎαžšβ€‹αž€αžΆαžšβ€‹αž’αž“β€‹αž‘αž·αž“αž“αž“αŸαž™β€‹αž–αžΈβ€‹File  Write File : αžŠαžΆαŸ†αž› αžΎαžšβ€‹αž€αžΆαžšαžŸαžšαž›αžŸαžšβ€‹/αž› αŸ„αŸ‡β€‹β€‹αž‘αž·αž“αž“αž“αŸαž™αž–αžΈβ€‹File
  • 9. 9 Read/ write File αž˜αž“β€‹: β€’ Read/Write all Bytes or Line from/to File : for small file 2. Read/Write File
  • 10. 10 β€’ Read/Write File Using Buffer Stream I/O : Read/Write TEXT from/to File β€’ Read/Write File Using Stream I/O : Read/Write BYTE stream from/to File β€’ Read/Write File Using Channel I/O : Read/Write TEXT from/to File (Channel I/O read/write a buffer at a time) 2. Read/Write File
  • 11. 3. FileInputStream / FileOutputStream 11 FileInputStream αž‚αžΊαž‡αžΆ Class αž˜αž½αž™αžŠαžŠαž›αž›αŸ’αžšαžΎαžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Read file αž‡αžΆ Byte Stream. αž›αŸ…αž€αž“ αž»αž„ FileInputStream αž˜αž“ SubClass αž˜αž½αž™αž›αž‘αŸ€αžαž›αŸ’αž˜ αŸ„αŸ‡ ថអ InputStream. αž…αžΆαŸ† αžŠαž“αž€ FileOutputStream αž‚αžΊαž‡αžΆ Class αž˜αž½αž™αžŠαžŠαž›αž›αŸ’αžšαžΎαžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Write File αž‡αžΆ Byte Stream. αž›αŸ…αž€αž“ αž»αž„ FileInputStream αž˜αž“ SubClass αž˜αž½αž™αž›αž‘αŸ€αžαž›αŸ’αž˜ αŸ„αŸ‡αžαžΆ OutputStream.
  • 12. 3. FileInputStream / FileOutputStream 12 Method αžŸαŸ†αžΆαžαžΆαž“αŸ‹αŸ— αžšαžŸαŸ‹ FileInputStream/FileOutputStream β€’ int available() αž‡αžΆMethod αž˜αž½αž™αžŠαžŠαž›αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžšαž–αž·αž“αž·αžαž™ αž›αž˜αžΎαž›αžαžΆαž’αž…Read αž“αž¬ αžαŸ‹αž›αžšαžΎαžœαžΆ Read αž“αž›αž“αžΆαŸ„αŸ‡ return true αžŠαžαž›αžšαžΎαžœαžΆαž˜αž·αž“αž’αž… Reader αž“αž›αž“αžΆαŸ„αŸ‡αžœαžΆ Return false. β€’ void close() αž‡αžΆ Method αž˜αž½αž™αžŠαžŠαž› closes file input stream αž› αžΎαž™αž“αž·αž„ resources αž›αžŸαŸαž„αž›αž‘αŸ€αžαžŠαžŠαž›αž˜αž“αž‘αžΆαŸ†αž“αžΆαž€αŸ‹ αž‘αžΆαŸ†αž“αž„ αž“αž·αž„stream. β€’ int read() αž‡αžΆ method reads a byte of data from this input stream.
  • 13. 4. FileReader / FileWriter 13 β€’ FileReader (java.io.Reader)αž‚αžΊαž‡αžΆ base class αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Read subclasses in the Java IO API. FileReader β€’ αžœαžΆαž€αŸαžŠαžΌαž…αž›αŸ…αž“αž·αž„ InputStreamReader αžŠαžαžœαžΆαžαž»αžŸαž‚αŸ’αž“αž“ αŸ’αžαž„αŸ‹αžαžΆ FileReader αž›αŸ’αžœαžΎαž€αžΆαžšβ€‹Read αž‡αžΆText ដត InputStreamReader វអRead αž‡αžΆ Byte Stream β€’ αž…αžΆαŸ† αžŠαž“αž€ FileWriter (java.io.Writer) αž‚αžΊαž‡αžΆ base class αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Write subclasses in the Java IO API FileWriter αžœαžΆαž€αŸαžŠαžΌαž…αž›αŸ…αž“αž·αž„ InputStreamWriter αžŠαžαžœαžΆαžαž»αžŸαž‚αŸ’αž“αž“ αŸ’αžαž„αŸ‹αžαžΆ FileWriter αž›αŸ’αžœαžΎαž€αžΆWrite αž‡αžΆText ដតInputStreamReader វអWriteαž‡αžΆ Byte Stream.
  • 14. 4. FileReader / FileWriter 14 FileReader FileWriter
  • 15. 5. InputSream / OutputStream 15 Java Application αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ input stream αž›αžŠαžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš read data αž–αžΈ source αž“αž·αž„ αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ output stream αž›αžŠαžΎαž˜αž”αžΈαž›αŸ’αžœαžΎαž€αžΆαžš write data αž›αŸ…αž€αžΆαž“αŸ‹ destinationαŸ” InputStream Class and OutputStream Class:αž‚αžΊαž‡αžΆ abstract class αžŠαžŠαž›αž‡αžΆ subclass αžšαžšαžŸαŸ‹ classes αž‘αžΆαŸ†αž„ αžŸαŸ‹ αžŠαžŠαž›αžαžΆαŸ†αžŽαžΆαž„αž›αž’αž™ input stream αžšαžšαžŸαŸ‹ bytes αžŠαŸ†αžΆαžšαžŽαŸ’αžΎ αž€αžΆ αžšαžŸαŸ‹ inputstream αž“αž·αž„ outputstream Method of InputStream Method of OutputStream 1) public abstract int read()throws IOException: 1) public void write(int)throws IOException: 2) public int available()throws IOException: 2) public void write(byte[])throws IOException: 3) public void close()throws IOException: 3) public void close()throws IOException:
  • 16. 5. InputSream / OutputStream 16 Hairachy of InputStream/OutputStream
  • 17. 6. InputSreamReader/OutputStreamWriter 17 Java.io.InputStreamReader class αž‚αžΊαž‡αžΆαžŸαŸ’αž–αžΆ αž“αž…αž˜αž›αž„αž–αžΈ byte streams αž›αŸ…αž€αžΆαž“αŸ‹ character streams αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžš read αž–αžΈ byte αž› αžΎαž™ decode αž‡αžΆ character αž›αŸ„αž™αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ charset αž…αžΆαŸ† αžŠαž“αž€Java.io.OutputStreamWriter αžœαžΆαž›αŸ’αžœαžΎαž€αžΆαžš write αž› αžΎαž™ encode αž›αŸ…αž‡αžΆ byte β€‹αžœ αž‰ αž›αŸ„αž™αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹ charsetαŸ” S.N. InputStreamReader: Constructor & Description OutputStreamWriter: Constructor & Description 1 InputStreamReader(InputStream in) This creates an InputStreamReader that uses the default charset. OutputStreamWriter(OutputStream out) This creates an OutputStreamWriter that uses the default character encoding. 2 InputStreamReader(InputStream in, Charset cs) This creates an InputStreamReader that uses the given charset. OutputStreamWriter(OutputStream out, Charset cs) This creates an OutputStreamWriter that uses the given charset. 3 InputStreamReader(InputStream in, CharsetDecoder dec) This creates an InputStreamReader that uses the given charset decoder. OutputStreamWriter(OutputStream out, CharsetEncoder enc) This creates an OutputStreamWriter that uses the given charset encoder. 4 InputStreamReader(InputStream in, String charsetName) This creates an InputStreamReader that uses the named charset. OutputStreamWriter(OutputStream out, String charsetName) This creates an OutputStreamWriter that uses the named charset.
  • 18. 7. Serialization 18 β€’ Serialization αž‚αžΊαž‡αžΆαž˜αŸ‰αžΆ αžŸαž»αžΈαž“ αž“αž“αž€αžΆαžš αžŸαžšαž›αžŸαžšαž“αžΌαžœ State αž“αž“ Object αž›αŸ…αž‡αžΆ Byte stream. β€’ αŸ’αž–αž˜αž‘αžΆαŸ†αž„αž€αžΆαžšαž›αŸ…αž“αžΌαžœ Serialization αž›αž“αŸ„αŸ‡αž˜αž€αžœ αž‰ αž›αŸ’αžšαžΎαŸ’ αžŸαŸ‹αž›αŸ…αžαžΆ Deserialization. β€’ String class αž“αž·αž„ wrapper class αž‘αžΆαŸ†αž„ αžŸαŸ‹αŸ’αžαžΌαžœαžΆ αž“ implements αž–αžΈ java.io.serializable interface αž›αŸ„αž™ default. β€’ Classes ObjectInputStream and ObjectOutputStream αž‚αžΊαž‡αžΆ high-level stream αžŠαžŠαž›αžœαžΆαž˜αž“αžŸαž‘ αž»αž€ αž“αžΌαžœ method αžŸαžΆαŸ†αžšαžΆαžšαŸ‹ Serialization αž“αž·αž„ Deserialization αžšαžšαžŸαŸ‹ Object. β€’ Method write Object αžŠαžŠαž›αž›αž‚αž“αž·αž™αž˜αž›αŸ’αžšαžΎαŸ– β€’ Method serialization αž“αž·αž„ deserialization Object αžŠαžŠαž›αž›αž‚αž“αž·αž™αž˜αž›αŸ’αžšαžΎαŸ–
  • 21. 8. RandomacessFile 21 Java.io.RandomAccessfile class αžœαžΆαŸ’αžΆαžšαžšαžΈαžŠαžΌαž…αž‡αžΆ Array αž™αŸ‰αžΆ αž„αŸ’αžΆαŸ†αž˜αž½αž™αžŠαžŠαž› αžŸαž‘ αž»αž€αž‡αžΆ bytes αž›αŸ…αž€αž“ αž»αž„ file αžŠαžŠαž› αžœαžΆαž’αž…αž‘αž‘αžΌαž› αž“αž‘αžΆαŸ†αž„ reaing αž“αž·αž„ writing αž›αŸ…αž‡αžΆ random access file. Class constructor:Constructor Descriptio RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to to, the file specified by the File argument. RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to to, a file with the specified name.
  • 22. 8. RandomacessFile 22 Methods Descriptio void close This method Closes this random access file stream and releases any system associated with the stream. FileChannel getChannel This method returns the unique FileChannel object associated with this file. FileDescriptor getFD This method returns the opaque file descriptor object associated with this stream. long getFilePointer This method returns the current offset in this file. long length This method returns the length of this file. int read This method reads a byte of data from this file. int readbyte[]b This method reads up to b.length bytes of data from this file into an array of bytes. int readbyte[]b, intoff, intlen This method reads up to len bytes of data from this file into an array of bytes. boolean readBoolean This method reads a boolean from this file. byte readByte This method reads a signed eight-bit value from this file.
  • 23. 8. RandomacessFile 23 β€’ Example: Result: Hello world Closing Stream.... Stream Closed.
  • 24. 8. RandomAcessFile 24 S.N. InputStreamReader: Method & Description OutputStreamWriter: Method & Description 1 void close() This method closes the stream and releases any system resources associated with it. void close() This method closes the stream, flushing it first. 2 String getEncoding() This method returns the name of the character encoding being used by this stream String getEncoding() This method returns the name of the character encoding being used by this stream. 3 int read() This method reads a single character. void write(char[] cbuf, int off, int len) This method writes a portion of an array of characters. 4 int read(char[] cbuf, int offset, int length) This method reads characters into a portion of an array. void write(int c) This method writes a single character. 5 boolean ready() This method tells whether this stream is ready to be read. void write(String str, int off, int len) This method writes a portion of a string.
  • 25. 9. αž”αŸ’αžšαž—αŸαž―αž€αžŸαžΆ β€’ http://www.tutorialspoint.com/java/io β€’ http://www.tutorialspoint.com/java/java_files_io.htm β€’ https://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.htmll β€’ https://examples.javacodegeeks.com/core-java/nio/file-nio/path/java-nio-file-path-example/ β€’ https://docs.oracle.com/javase/tutorial/essential/io/file.html β€’ http://www.tutorialspoint.com/java/java_files_io.htm 25