AP m24 Week10 Fileio
AP m24 Week10 Fileio
CSE 201
Instructor: Sambuddho
● List<String> lines = Files.readAllLines(path, charset); // read multiple lines as strings for file path.
● InputStream inputStream = . . .;
● try (BufferedReader in = new BufferedReader(new InputStreamReader(inputStream,
● StandardCharsets.UTF_8)))
● {
● String line;
● while ((line = in.readLine()) != null)
● {
● do something with line
● }
● }
●
Text I/O – File Writer
Reading and Writing Binary Data
●
Reading and Writing Binary Data
Random Access Files
● {
● ZipEntry ze = new ZipEntry(filename);
● zout.putNextEntry(ze);
● zout.write(...);
● zout.closeEntry();
● }
● zout.close();
Path Interface and Files Class
● Used to work with the file system for operations like create file, delete file,
create directory etc.
Path resolution:
OR
Reading Files
Move/rename
Delete file
Getting File Attributes
(Caveat: In reality all files are mapped to a location of memory for faster operation, aka
BufferedReader).
Steps:
1. Get a FileChannel object of a file (like a file handle).
2. Specify the file area you want to map, using the FileChannel (1) and the mapping mode
(FileChannel.MapMode.READ_ONLY,FileChannel.MapMode.READ_WRITE,FileChannel.MapMode.P
rivate).
3. (2) provides a reference to ByteBuffer class that can be used for reading and writing like a
flat file.
Buffers can be both sequential (using the get() and put() method that advance the position) or
random-access using the actual location.
Memory Mapped I/O
Sequential access
Random access
Memory Mapped I/O