KEMBAR78
Java program file I/O | PPTX
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

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/FileClass 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 Classand 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 importjava.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 Classand 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 Classand 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 FileUsing 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 αžŠαžŠαž›αž›αž‚αž“αž·αž™αž˜αž›αŸ’αžšαžΎαŸ–
  • 19.
  • 20.
  • 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 voidclose 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: Helloworld 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
  • 26.