SlideServe
Menu
Préférences
© SlideServe · v2026

Working with files

nani avatar
12 diapositives · 268 Views · Sep 15, 2014

This guide explores the fundamental concepts of file handling in Java, focusing on reading and writing text files. It explains how files are permanent storage for data, providing a structured way to interact with data beyond primary storage (RAM). Key classes like FileReader and PrintWriter are introduced for reading from and writing to text files, respectively. Additionally, the importance of handling exceptions such as IOException is emphasized to ensure robust file operations. Perfect for Java beginners wanting to implement file I/O in their programs.

Working with files - slide 1
01 / 12Working with files
1 / 17

Télécharger la présentation

Working with files
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Working with files

  2. Motivation • All our programs so far have only worked with data stored in primary storage (RAM) • Data is lost when program is terminated • Almost all ”real” programs allow us to save the state – by some definition – of the program to permanent storage (e.g disk) • Such permanent storage is often a file RHS – SWC

  3. What is a file? • A collection of bytes, stored on a perma-nent storage media • Bytes can be interpreted as text, numerics, picture, etc. • Files have a name, and a position (path) • Myschedule.doc (name) • C:\documents\per\rhs (path) RHS – SWC

  4. Text files • A text file just contains characters (bytes inter-preted as characters) • Usually named .txt • Created with simple text editor like Notepad, or NetBeans… RHS – SWC

  5. Text files and Java • It is rather easy to read a text file in Java – we use the FileReader class and the Scanner class FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); String line = in.nextLine(); RHS – SWC

  6. Text files and Java Constructor takes file name as a String (remember ””) FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); String line = in.nextLine(); Several next… methods available next() nextInt() nextDouble() … FileReader itself cannot be used for reading – need a Scanner object RHS – SWC

  7. Text files and Java FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); int lineCount = 0; while (in.hasNextLine()) { String line = in.nextLine(); lineCount++; } in.close(); System.out.println(”Lines in file: ” + lineCount); RHS – SWC

  8. Text files and Java • When providing a file path as a constant string, the string mus look like this: ”c:\\documents\\psl\\work.txt”; • Notice the use of double backslash! • Recall that \ is used for escape characters! • We do not need double backslash when e.g the user types the path in a dialog RHS – SWC

  9. Text files and Java • It is also easy to write to a file in Java – we use the PrintWriter class PrintWriter out = new PrintWriter(”output.txt”); • That’s it – we can now use the well-known print(…) and println(…) methods RHS – SWC

  10. Text files and Java PrintWriter out = new PrintWriter(”output.txt”); for (int i = 0; i < 10; i++) { out.println(”Writing to a file is easy!”); } out.close(); System.out.println(”Done writing to file”); RHS – SWC

  11. Text files and Exceptions • Working with files is easy, but… • …errors beyond the control of the pro-grammer can happen! • File was not found • File was not a text file • File was different format than expected • …and so on RHS – SWC

  12. Text files and Exceptions • Code for handling files should include proper handling of exceptions! • IOException class, and subclass thereof • We can define more specific classes as well RHS – SWC