SlideShare una empresa de Scribd logo
1 de 30
Basic Classes Input/Output
Contents ,[object Object],[object Object],[object Object],[object Object]
What Is Stream? Streams Basics
What is Stream? ,[object Object],[object Object],Input stream Output stream
Stream Basics ,[object Object],[object Object],[object Object],[object Object],[object Object]
Reading Text Files Using the   Scanner  Class
The  Scanner   Class ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Using  Scanner  for reading a Text File ,[object Object],[object Object],[object Object],[object Object],File file = new File("test.txt"); Scanner fileInput = new Scanner(file, "windows-1251"); //Read file here... fileInput.close(); Specifies the text encoding.
Reading a Text File Line by Line ,[object Object],File file = new File("somefile.txt"); //Next line may throw exception! Scanner fileInput = new Scanner(file);  int lineNumber = 0; while (fileInput.hasNextLine()) { lineNumber++; System.out.printf("Line %d: %s%n", lineNumber, fileInput.nextLine()); } fileInput.close();
Reading Text Files Live Demo
Writing Text Files Using the  PrintStream  Class
The  PrintStream   Class ,[object Object],[object Object],[object Object],[object Object],PrintStream fileOutput = new PrintStream ( "test.txt", "windows-1251");
Writing to a Text File ,[object Object],//Next line may throw exception PrintStream fileOutput = new PrintStream(&quot;numbers.txt&quot;); for (int number = 1; number <= 20; number++) { fileOutput.println(number); } fileOutput.close();
Writing Text Files Live Demo
Handling I/O Exceptions Introduction
What is Exception? ,[object Object],[object Object],[object Object],[object Object],[object Object]
How to Handle and Catch Exceptions? ,[object Object],[object Object],try { // Some exception is thrown } catch (<exception type> ex) {   // Exception is handled } finally { // Type functionality that must be performed  // no matter if exception has occurred or not  } try {...} catch (FileNotFoundException fnfe) { System.out.println(&quot;Can not find file.&quot;); }
Handling Exceptions When Opening a File String fileName = &quot;somefile.txt&quot;; Scanner fileInput = null; try { fileInput = new Scanner(new File(fileName)); System.out.printf( &quot;File %f successfully opened.%n&quot;, fileName); } catch (NullPointerException npe) { System.err.printf( &quot;Can not find file %s.&quot;, fileName); } catch (FileNotFoundException fnf) { System.err.printf( &quot;Can not find file %s.&quot;, fileName); }
Handling I/O Exceptions Live Demo
Reading and Writing Text Files More Examples
Reading and Writing Text Files – Example ,[object Object],Scanner fileInput =   new Scanner( new File( &quot; somefile .txt) ) ; int count = 0; while (fileInput.hasNext()){ String line = fileInput. next Line(); int index = line.indexOf(&quot;foundme&quot;, 0) + 1; while (index != 0) { count++; index = line.indexOf(&quot;foundme&quot;, index) + 1; } } System.out.println(count); What is missing in this code?
Fixing Subtitles – Example ,[object Object],Scanner fileInput = null; PrintStream fileOutput = null; try { // Create scanner with the Cyrillic encoding fileInput = new Scanner(new File(&quot;source.sub&quot;), &quot;windows-1251&quot;); // Create PrintWriter with the Cyrillic encoding fileOutput = new PrintStream( &quot;fixed.sub&quot;, &quot;windows-1251&quot;); String line; while (fileInput.hasNextLine()) { line = fileInput.nextLine(); fileOutput.println(fixLine(line)); } // Example continues…
Fixing Subtitles – Example(2) } catch (FileNotFoundException fnfe) { System.out.println(fnfe.getMessage()); } catch (UnsupportedEncodingException uee) { System.out.println(uee.getMessage()); } finally { //remember to close the streams if (null != fileInput) { fileInput.close(); } if (null != fileOutput) { fileOutput.close(); } }
Fixing Movie Subtitles Live Demo
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object]
Exercises ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercises (2) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Exercises (3) ,[object Object],[object Object],[object Object],[object Object]
Exercises (4) ,[object Object],[object Object],[object Object],[object Object]
Exercises (5) ,[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

Java IO
Java IOJava IO
Java IO
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Nhap xuat trong java
Nhap xuat trong javaNhap xuat trong java
Nhap xuat trong java
 
Python Session - 5
Python Session - 5Python Session - 5
Python Session - 5
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python Session - 2
Python Session - 2Python Session - 2
Python Session - 2
 
Python - Introduction
Python - IntroductionPython - Introduction
Python - Introduction
 
Python Session - 6
Python Session - 6Python Session - 6
Python Session - 6
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python ppt
Python pptPython ppt
Python ppt
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python made easy
Python made easy Python made easy
Python made easy
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Python basics
Python basicsPython basics
Python basics
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
Python Book/Notes For Python Book/Notes For S.Y.B.Sc. I.T.
 
Intro to Python Programming Language
Intro to Python Programming LanguageIntro to Python Programming Language
Intro to Python Programming Language
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
Python Workshop
Python WorkshopPython Workshop
Python Workshop
 

Destacado

My pls learning_activities_lams0811
My pls learning_activities_lams0811My pls learning_activities_lams0811
My pls learning_activities_lams0811hccit
 
Logbook
LogbookLogbook
Logbookhccit
 
Gary Wolley Interview
Gary Wolley InterviewGary Wolley Interview
Gary Wolley InterviewMedia Studies
 
UCAN 2010: Registrar's Office Update in September 2010
UCAN 2010: Registrar's Office Update in September 2010UCAN 2010: Registrar's Office Update in September 2010
UCAN 2010: Registrar's Office Update in September 2010UCAN at UNT
 
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...UCAN at UNT
 
Week08
Week08Week08
Week08hccit
 

Destacado (7)

My pls learning_activities_lams0811
My pls learning_activities_lams0811My pls learning_activities_lams0811
My pls learning_activities_lams0811
 
Logbook
LogbookLogbook
Logbook
 
Food energy
Food energyFood energy
Food energy
 
Gary Wolley Interview
Gary Wolley InterviewGary Wolley Interview
Gary Wolley Interview
 
UCAN 2010: Registrar's Office Update in September 2010
UCAN 2010: Registrar's Office Update in September 2010UCAN 2010: Registrar's Office Update in September 2010
UCAN 2010: Registrar's Office Update in September 2010
 
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...
#UNTAdv14 Reality Check: How Students Academic Choices will Impact Their Futu...
 
Week08
Week08Week08
Week08
 

Similar a Basic I/O Classes Streams Files Exceptions Java</title

Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OWebStackAcademy
 
Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfrishabjain5053
 
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.docxalfred4lewis58146
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdfSudhanshiBakre1
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docxtheodorelove43763
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File ConceptsANUSUYA S
 
For the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdfFor the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdfarihantplastictanksh
 
15 Text files
15 Text files15 Text files
15 Text filesmaznabili
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxfredharris32
 

Similar a Basic I/O Classes Streams Files Exceptions Java</title (20)

15. text files
15. text files15. text files
15. text files
 
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
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 
Need help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdfNeed help with this java code. fill in lines where needed. Also, not.pdf
Need help with this java code. fill in lines where needed. Also, not.pdf
 
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
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
C++ - UNIT_-_V.pptx which contains details about File Concepts
C++  - UNIT_-_V.pptx which contains details about File ConceptsC++  - UNIT_-_V.pptx which contains details about File Concepts
C++ - UNIT_-_V.pptx which contains details about File Concepts
 
For the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdfFor the following I have finished most of this question but am havin.pdf
For the following I have finished most of this question but am havin.pdf
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
15 Text files
15 Text files15 Text files
15 Text files
 
ExtraFileIO.pptx
ExtraFileIO.pptxExtraFileIO.pptx
ExtraFileIO.pptx
 
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
 
file.ppt
file.pptfile.ppt
file.ppt
 
Bioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-filesBioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-files
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
 
Satz1
Satz1Satz1
Satz1
 

Más de BG Java EE Course (20)

Rich faces
Rich facesRich faces
Rich faces
 
JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
 
JSTL
JSTLJSTL
JSTL
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Web Applications and Deployment
Web Applications and DeploymentWeb Applications and Deployment
Web Applications and Deployment
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
CSS
CSSCSS
CSS
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 
HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
WWW and HTTP
WWW and HTTPWWW and HTTP
WWW and HTTP
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Creating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Introduction to-RDBMS-systems
Introduction to-RDBMS-systemsIntroduction to-RDBMS-systems
Introduction to-RDBMS-systems
 

Basic I/O Classes Streams Files Exceptions Java</title

Notas del editor

  1. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  2. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  3. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  4. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  5. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  6. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  7. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  8. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  9. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  10. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  11. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## The missing code is the &amp;quot;using&amp;quot; block. The file should be properly closed.
  12. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  13. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  14. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  15. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  16. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  17. * 12/04/10 07/16/96 (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##