file handling and file operations in java
file handling and file operations in java
UNIVERSITY
FILE HANDLING
FILE OPERATIONS
CODING
APPLICATIONS
CONCLUSION
4
FILE HANDLING
5
FILE HANDLING
File handling in programming refers to the process of creating,
opening, reading, writing, and closing files. It allows us to store
and manage data outside of the program, so information can
be saved and accessed later. This is useful for tasks like saving
user data, logs, and settings.
6
FILE OPERATIONS
FILE OPERATIONS 7
The key function for working with files in Python is the open() function.
The open() function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
"r" - Read - Default value. Opens a file for reading, error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
In addition, we can specify if the file should be handled as binary or text mode
8
Reading Entire File file.read() Reads the entire content of the file.
Reading Line by Line file.readline() Reads one line from the file at a time.
Reading All Lines file.readlines() Reads all lines and returns them as a list.
Getting Cursor Position file.tell() Returns the current position of the file cursor.
CODE
10
11
12
13
14
15
16
17
18
19
APPLICATIONS
20
• Saving Data: Apps store user information, like saved games or documents.
• Settings Storage: Programs use files to remember user settings.
• Logs: Systems keep logs to track errors or activities.
• Data Sharing: Files like CSV or JSON make it easy to share data between apps.
• Backups: Important files are saved as backups in case of data loss.
• Multimedia: Media files are handled to view, edit, or play content.
• Databases: Databases use files to store information and for exporting data.
• User Authentication: Files can securely store login details.
• Reports: Systems create reports as text or PDF files for users.
• Websites: Websites rely on files for HTML, CSS, and images.
21
CONCLUSION
file handling and operations are essential in managing and
storing data effectively in real-world applications. They allow
programs to save, retrieve, and share information, making
them crucial for everything from data storage and user
settings to log management and multimedia handling.
Mastering file handling is a valuable skill that enhances
application functionality and ensures data is used efficiently.
22