File Handling in Java
Activity Outcomes:
Student will be able to:
- Describe the concept of an I/O stream
- Explain the difference between text files and binary files
- Save data
- Read data
Introduction
The character input and output shown so far has used the pre-defined “standard” streams System.in and System.out. Obviously in many real applications it is necessary to access named files. In many programming languages there is a logical (and practical) distinction made between files that will contain text and those while will be used to hold binary information. Files processed as binary are thought of as sequences of 8- bit bytes, while ones containing text model sequences of characters.
Java uses names typically based on the word Stream for binary access, and Reader and Writer for text. So when you read the documentation expect to find two broadly parallel sets of mechanisms, one for each style of access.
Lab Activities:
Activity 1:
creates InputStreamReader to read standard input stream until the user types a “q”.
Solution:
Activity 2:
Write a java program to demonstrate InputStream and OutputStream
Solution:
Activity 3:
A java program to show that a method called to read characters, and that it returns the integer -1 at end of file.
Solution:
Activity 4:
Write a java program by using list( ) method provided by File object to list down all the files and directories available in a directory.
Solution:
Activity 5:
Write a java program to compare paths of two files?
Solution:
Activity 6:
Write a java program to create a new file?
Solution:
Activity 7:
Write a java program to create a new file (another way)?
Solution:
Activity 8:
Write a java program to get last modification date of a file?
Solution:
Home Activities:
Activity 1:
Write a java program to create a file in a specified directory?
Activity 2:
Write a java program to check a file exist or not?
Activity 3:
Write a java program to make a file read-only?
Activity 4:
Write a java program to rename a file?