SlideShare a Scribd company logo
www.webstackacademy.com
Java Programming Language SE – 6
Module 11: Console I/O and File I/O
www.webstackacademy.com
Objectives
● Read data from the console
● Write data to the console
● Describe files and file I/O
www.webstackacademy.com
Console I/O
● The variable System.out enables you to write to standard output.
System.out is an object of type PrintStream.
● The variable System.in enables you to read from standard input.
System.in is an object of type InputStream.
● The variable System.err enables you to write to standard error.
System.err is an object of type PrintStream.
www.webstackacademy.com
Writing to Standard Output
● The println methods print the argument and a newline character (n).
● The print methods print the argument without a newline character.
● The print and println methods are overloaded for most primitive types
(boolean, char, int, long, float, and double) and for char[], Object, and
String.
● The print(Object) and println(Object) methods call the toString method
on the argument.
www.webstackacademy.com
Reading From Standard
Input
public class KeyboardInput {
public static void main (String args[]) {
String s;
// Create a buffered reader to read
// each line from the keyboard.
InputStreamReader ir
= new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(ir);
System.out.println("Unix: Type ctrl-d to exit." +
"nWindows: Type ctrl-z to exit");
www.webstackacademy.com
Reading From Standard
Input
try {
// Read each input line and echo it to the screen.
s = in.readLine();
while ( s != null ) {
System.out.println("Read: " + s);
s = in.readLine();
}
// Close the buffered reader.
in.close();
} catch (IOException e) { // Catch any IO exceptions.
e.printStackTrace();
}}}
www.webstackacademy.com
Simple Formatted Output
● You can use the formatting functionality as follows:
out.printf(“name countn”);
String s = String.format(“%s %5d%n”, user, total);
● Common formatting codes are listed in this table.
www.webstackacademy.com
Simple Formatted Output
www.webstackacademy.com
Simple Formatted Input
The Scanner class provides a formatted input function.
A Scanner class can be used with console input streams as well as file or
network streams.
www.webstackacademy.com
Simple Formatted Input
You can read console input as follows:
import java.io.*;
import java.util.Scanner;
public class ScanTest {
public static void main(String [] args) {
Scanner s = new Scanner(System.in);
String param = s.next();
System.out.println("the param 1" + param);
int value = s.nextInt();
System.out.println("second param" + value);
s.close();
}}
www.webstackacademy.com
Files and File I/O
The java.io package enables you to do the following:
– Create File objects
– Manipulate File objects
– Read and write to file streams
www.webstackacademy.com
Creating a New File Object
The File class provides several utilities:
– File myFile;
– myFile = new File("myfile.txt");
– myFile = new File("MyDocs", "myfile.txt");
www.webstackacademy.com
Creating a New File Object
● Directories are treated like files in the Java programming
language. You can create a File object that represents a
directory and then use it to identify other files, for example:
File myDir = new File("MyDocs");
myFile = new File(myDir, "myfile.txt");
www.webstackacademy.com
The File Tests and Utilities
● File information:
– String getName()
– String getPath()
– String getAbsolutePath()
– String getParent()
– long lastModified()
– long length()
www.webstackacademy.com
The File Tests and Utilities
● File modification:
– boolean renameTo(File newName)
– boolean delete()
● Directory utilities:
– boolean mkdir()
– String[] list()
www.webstackacademy.com
The File Tests and Utilities
● File tests:
– Boolean exists()
– Boolean cabRead()
– Boolean canRead()
– Boolean isFile()
– Boolean isDirectory()
– Boolean isAbsolute();
– Boolean is Hidden();
www.webstackacademy.com
File Stream I/O
● For file input:
– Use the FileReader class to read characters.
– Use the BufferedReader class to use the readLine method.
● For file output:
– Use the FileWriter class to write characters.
– Use the PrintWriter class to use the print and println methods.
www.webstackacademy.com
File Input Example
public class ReadFile {
public static void main (String[] args) {
// Create file
File file = new File(args[0]);
try {
// Create a buffered reader
// to read each line from a file.
BufferedReader in
= new BufferedReader(new FileReader(file));
String s;
www.webstackacademy.com
Printing a File
s = in.readLine();
while ( s != null ) {
System.out.println("Read: " + s);
s = in.readLine();
}
// Close the buffered reader
in.close();
} catch (FileNotFoundException e1) {
// If this file does not exist
System.err.println("File not found: " + file);
} catch (IOException e2) {
// Catch any other IO exceptions.
e2.printStackTrace();
}}}
www.webstackacademy.com
File Output Example
public class WriteFile {
public static void main (String[] args) {
// Create file
File file = new File(args[0]);
try {
// Create a buffered reader to read each line from standard in.
InputStreamReader isr
= new InputStreamReader(System.in);
BufferedReader in
= new BufferedReader(isr);
// Create a print writer on this file.
PrintWriter out
= new PrintWriter(new FileWriter(file));
String s;
www.webstackacademy.com
File Output Example
System.out.print("Enter file text. ");
System.out.println("[Type ctrl-d to stop.]");
// Read each input line and echo it to the screen.
while ((s = in.readLine()) != null) {
out.println(s);
}
// Close the buffered reader and the file print writer.
in.close();
out.close();
} catch (IOException e) {
// Catch any IO exceptions.
e.printStackTrace();
}}}
www.webstackacademy.com
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com

More Related Content

What's hot

Java file
Java fileJava file
Java file
sonnetdp
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
Victer Paul
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
Filehandling
FilehandlingFilehandling
Filehandling
Amandeep Kaur
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
PRN USM
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
 
Java stream
Java streamJava stream
Java stream
Arati Gadgil
 
I/O in java Part 1
I/O in java Part 1I/O in java Part 1
I/O in java Part 1
ashishspace
 
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
 
Files io
Files ioFiles io
Files io
Narayana Swamy
 
Java File I/O
Java File I/OJava File I/O
Java File I/O
Canterbury HS
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
File handling
File handlingFile handling
File handling
prateekgemini
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.net
Everywhere
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
Shyam Gupta
 
Java
JavaJava

What's hot (20)

Java file
Java fileJava file
Java file
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Filehandling
FilehandlingFilehandling
Filehandling
 
Java IO
Java IOJava IO
Java IO
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
File Input & Output
File Input & OutputFile Input & Output
File Input & Output
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Java stream
Java streamJava stream
Java stream
 
I/O in java Part 1
I/O in java Part 1I/O in java Part 1
I/O in java Part 1
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Files io
Files ioFiles io
Files io
 
Java File I/O
Java File I/OJava File I/O
Java File I/O
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
File handling
File handlingFile handling
File handling
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.net
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Java
JavaJava
Java
 

Similar to Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O

File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
PragatiSutar4
 
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
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
NguynThiThanhTho
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course
 
5java Io
5java Io5java Io
5java Io
Adil Jafri
 
Io streams
Io streamsIo streams
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
Sreedhar Chowdam
 
Java Notes
Java Notes Java Notes
Java Notes
Sreedhar Chowdam
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
siragezeynu
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
Sunil OS
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
Berk Soysal
 
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PAGE  1Input output for a file tutorialStreams and File IOI.docxPAGE  1Input output for a file tutorialStreams and File IOI.docx
PAGE 1Input output for a file tutorialStreams and File IOI.docx
alfred4lewis58146
 

Similar to Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O (20)

File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
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
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
5java Io
5java Io5java Io
5java Io
 
Io streams
Io streamsIo streams
Io streams
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Java Tutorial Lab 6
Java Tutorial Lab 6Java Tutorial Lab 6
Java Tutorial Lab 6
 
PAGE 1Input output for a file tutorialStreams and File IOI.docx
PAGE  1Input output for a file tutorialStreams and File IOI.docxPAGE  1Input output for a file tutorialStreams and File IOI.docx
PAGE 1Input output for a file tutorialStreams and File IOI.docx
 

More from WebStackAcademy

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 

More from WebStackAcademy (20)

Webstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement JourneyWebstack Academy - Course Demo Webinar and Placement Journey
Webstack Academy - Course Demo Webinar and Placement Journey
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per SecondWSA: Scaling Web Service to Handle Millions of Requests per Second
WSA: Scaling Web Service to Handle Millions of Requests per Second
 
WSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer CourseWSA: Course Demo Webinar - Full Stack Developer Course
WSA: Course Demo Webinar - Full Stack Developer Course
 
Career Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
 
Webstack Academy - Internship Kick Off
Webstack Academy - Internship Kick OffWebstack Academy - Internship Kick Off
Webstack Academy - Internship Kick Off
 
Building Your Online Portfolio
Building Your Online PortfolioBuilding Your Online Portfolio
Building Your Online Portfolio
 
Front-End Developer's Career Roadmap
Front-End Developer's Career RoadmapFront-End Developer's Career Roadmap
Front-End Developer's Career Roadmap
 
Angular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and AuthorizationAngular - Chapter 9 - Authentication and Authorization
Angular - Chapter 9 - Authentication and Authorization
 
Angular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP ServicesAngular - Chapter 7 - HTTP Services
Angular - Chapter 7 - HTTP Services
 
Angular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase IntegrationAngular - Chapter 6 - Firebase Integration
Angular - Chapter 6 - Firebase Integration
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular - Chapter 4 - Data and Event Handling
 Angular - Chapter 4 - Data and Event Handling Angular - Chapter 4 - Data and Event Handling
Angular - Chapter 4 - Data and Event Handling
 
Angular - Chapter 3 - Components
Angular - Chapter 3 - ComponentsAngular - Chapter 3 - Components
Angular - Chapter 3 - Components
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
JavaScript - Chapter 15 - Debugging Techniques
 JavaScript - Chapter 15 - Debugging Techniques JavaScript - Chapter 15 - Debugging Techniques
JavaScript - Chapter 15 - Debugging Techniques
 
JavaScript - Chapter 14 - Form Handling
 JavaScript - Chapter 14 - Form Handling   JavaScript - Chapter 14 - Form Handling
JavaScript - Chapter 14 - Form Handling
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)JavaScript - Chapter 13 - Browser Object Model(BOM)
JavaScript - Chapter 13 - Browser Object Model(BOM)
 
JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 

Recently uploaded

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 

Recently uploaded (20)

Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 

Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O

  • 1. www.webstackacademy.com Java Programming Language SE – 6 Module 11: Console I/O and File I/O
  • 2. www.webstackacademy.com Objectives ● Read data from the console ● Write data to the console ● Describe files and file I/O
  • 3. www.webstackacademy.com Console I/O ● The variable System.out enables you to write to standard output. System.out is an object of type PrintStream. ● The variable System.in enables you to read from standard input. System.in is an object of type InputStream. ● The variable System.err enables you to write to standard error. System.err is an object of type PrintStream.
  • 4. www.webstackacademy.com Writing to Standard Output ● The println methods print the argument and a newline character (n). ● The print methods print the argument without a newline character. ● The print and println methods are overloaded for most primitive types (boolean, char, int, long, float, and double) and for char[], Object, and String. ● The print(Object) and println(Object) methods call the toString method on the argument.
  • 5. www.webstackacademy.com Reading From Standard Input public class KeyboardInput { public static void main (String args[]) { String s; // Create a buffered reader to read // each line from the keyboard. InputStreamReader ir = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(ir); System.out.println("Unix: Type ctrl-d to exit." + "nWindows: Type ctrl-z to exit");
  • 6. www.webstackacademy.com Reading From Standard Input try { // Read each input line and echo it to the screen. s = in.readLine(); while ( s != null ) { System.out.println("Read: " + s); s = in.readLine(); } // Close the buffered reader. in.close(); } catch (IOException e) { // Catch any IO exceptions. e.printStackTrace(); }}}
  • 7. www.webstackacademy.com Simple Formatted Output ● You can use the formatting functionality as follows: out.printf(“name countn”); String s = String.format(“%s %5d%n”, user, total); ● Common formatting codes are listed in this table.
  • 9. www.webstackacademy.com Simple Formatted Input The Scanner class provides a formatted input function. A Scanner class can be used with console input streams as well as file or network streams.
  • 10. www.webstackacademy.com Simple Formatted Input You can read console input as follows: import java.io.*; import java.util.Scanner; public class ScanTest { public static void main(String [] args) { Scanner s = new Scanner(System.in); String param = s.next(); System.out.println("the param 1" + param); int value = s.nextInt(); System.out.println("second param" + value); s.close(); }}
  • 11. www.webstackacademy.com Files and File I/O The java.io package enables you to do the following: – Create File objects – Manipulate File objects – Read and write to file streams
  • 12. www.webstackacademy.com Creating a New File Object The File class provides several utilities: – File myFile; – myFile = new File("myfile.txt"); – myFile = new File("MyDocs", "myfile.txt");
  • 13. www.webstackacademy.com Creating a New File Object ● Directories are treated like files in the Java programming language. You can create a File object that represents a directory and then use it to identify other files, for example: File myDir = new File("MyDocs"); myFile = new File(myDir, "myfile.txt");
  • 14. www.webstackacademy.com The File Tests and Utilities ● File information: – String getName() – String getPath() – String getAbsolutePath() – String getParent() – long lastModified() – long length()
  • 15. www.webstackacademy.com The File Tests and Utilities ● File modification: – boolean renameTo(File newName) – boolean delete() ● Directory utilities: – boolean mkdir() – String[] list()
  • 16. www.webstackacademy.com The File Tests and Utilities ● File tests: – Boolean exists() – Boolean cabRead() – Boolean canRead() – Boolean isFile() – Boolean isDirectory() – Boolean isAbsolute(); – Boolean is Hidden();
  • 17. www.webstackacademy.com File Stream I/O ● For file input: – Use the FileReader class to read characters. – Use the BufferedReader class to use the readLine method. ● For file output: – Use the FileWriter class to write characters. – Use the PrintWriter class to use the print and println methods.
  • 18. www.webstackacademy.com File Input Example public class ReadFile { public static void main (String[] args) { // Create file File file = new File(args[0]); try { // Create a buffered reader // to read each line from a file. BufferedReader in = new BufferedReader(new FileReader(file)); String s;
  • 19. www.webstackacademy.com Printing a File s = in.readLine(); while ( s != null ) { System.out.println("Read: " + s); s = in.readLine(); } // Close the buffered reader in.close(); } catch (FileNotFoundException e1) { // If this file does not exist System.err.println("File not found: " + file); } catch (IOException e2) { // Catch any other IO exceptions. e2.printStackTrace(); }}}
  • 20. www.webstackacademy.com File Output Example public class WriteFile { public static void main (String[] args) { // Create file File file = new File(args[0]); try { // Create a buffered reader to read each line from standard in. InputStreamReader isr = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(isr); // Create a print writer on this file. PrintWriter out = new PrintWriter(new FileWriter(file)); String s;
  • 21. www.webstackacademy.com File Output Example System.out.print("Enter file text. "); System.out.println("[Type ctrl-d to stop.]"); // Read each input line and echo it to the screen. while ((s = in.readLine()) != null) { out.println(s); } // Close the buffered reader and the file print writer. in.close(); out.close(); } catch (IOException e) { // Catch any IO exceptions. e.printStackTrace(); }}}
  • 22. www.webstackacademy.com Web Stack Academy (P) Ltd #83, Farah Towers, 1st floor,MG Road, Bangalore – 560001 M: +91-80-4128 9576 T: +91-98862 69112 E: info@www.webstackacademy.com