SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Java stream
Java streamJava stream
Java stream
Arati Gadgil
 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
 
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
Anton Keks
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
 
Java I/O
Java I/OJava I/O
Java I/O
Jussi Pohjolainen
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
myrajendra
 
Files in java
Files in javaFiles in java
Files in java
Muthukumaran Subramanian
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
myrajendra
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Tien 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 Streams
Don Bosco BSIT
 
Java
JavaJava
Java
Dhruv Sabalpara
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
 
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
Anton Keks
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
myrajendra
 
Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
myrajendra
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
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
Don Bosco BSIT
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
Eduardo Bergavera
 

Similar to Java program file I/O (20)

IO Programming.pptx all informatiyon ppt
IO Programming.pptx all informatiyon pptIO Programming.pptx all informatiyon ppt
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ssuser9d7049
 
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
phanleson
 
Java IO Stream, the introduction to Streams
Java IO Stream, the introduction to StreamsJava IO Stream, the introduction to Streams
Java IO Stream, the introduction to Streams
ranganadh6
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
chapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and streamchapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and stream
amarehope21
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Input & output
Input & outputInput & output
Input & output
SAIFUR RAHMAN
 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
Gera Paulos
 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdf
hemanth248901
 
Java development development Files lectur6.ppt
Java development development Files lectur6.pptJava development development Files lectur6.ppt
Java development development Files lectur6.ppt
rafeakrafeak
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
Hitesh-Java
 
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
RathanMB
 
Io Streams
Io StreamsIo Streams
Io Streams
phanleson
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
People Strategists
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
Riccardo Cardin
 
IO Programming.pptx all informatiyon ppt
IO Programming.pptx all informatiyon pptIO Programming.pptx all informatiyon ppt
IO Programming.pptx all informatiyon ppt
nandinimakwana22cse
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Stream In Java.pptx
Stream In Java.pptxStream In Java.pptx
Stream In Java.pptx
ssuser9d7049
 
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
phanleson
 
Java IO Stream, the introduction to Streams
Java IO Stream, the introduction to StreamsJava IO Stream, the introduction to Streams
Java IO Stream, the introduction to Streams
ranganadh6
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
chapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and streamchapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and stream
amarehope21
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Advanced programming ch2
Advanced programming ch2Advanced programming ch2
Advanced programming ch2
Gera Paulos
 
inputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdfinputoutputstreams-140612032817-phpapp02.pdf
inputoutputstreams-140612032817-phpapp02.pdf
hemanth248901
 
Java development development Files lectur6.ppt
Java development development Files lectur6.pptJava development development Files lectur6.ppt
Java development development Files lectur6.ppt
rafeakrafeak
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
Hitesh-Java
 
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
RathanMB
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
Riccardo Cardin
 
Ad

Recently uploaded (20)

UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Build 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHSBuild 3D Animated Safety Induction - Tech EHS
Build 3D Animated Safety Induction - Tech EHS
TECH EHS Solution
 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Social Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTechSocial Media App Development Company-EmizenTech
Social Media App Development Company-EmizenTech
Steve Jonas
 
MINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PRMINDCTI revenue release Quarter 1 2025 PR
MINDCTI revenue release Quarter 1 2025 PR
MIND CTI
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
TrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token ListingTrsLabs Consultants - DeFi, WEb3, Token Listing
TrsLabs Consultants - DeFi, WEb3, Token Listing
Trs Labs
 
Ad

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