In these lessons you will see how to read and write to and from the file system.
In these lessons you will see how to read and write to and from the file system. The legacy file API is used across many older projects, frameworks and libraries. Despite its age, the legacy API is not deprecated and you can still use it with any of the latest Java versions.
You’ll learn how java.nio.file.Path does everything java.io.File can, and generally in a better way, with the new classes able to support symlinks, proper file attributes, metadata, ACLs and more. When working with files or paths, you will likely be using the java.nio.file.Files class, and we’ll cover many common functions that operate on files and directories. Input and Output encompass a broad swath of sources and we naturally need to deal with character and byte file contents. The NIO library is chock-full of everything we need to accomplish whatever I/O requirements we may face.
Learning Objectives:
- Describe the new NIO library
- Understand how to read and write to the file system
- Explain how to create temporary files
Skills you’ll gain
File I/OI/O ProcessorJava 11Java Advanced ImagingJava APIsNon-Blocking I/O (Java)What You'll Learn
- Describe the Java NIO library and its file-handling capabilities
- Read from and write to the file system using Java I/O
- Use java.nio.file.Path in place of the legacy java.io.File API
- Work with the java.nio.file.Files class for common file and directory operations
- Handle file metadata, attributes, ACLs, and symlinks
- Create temporary files using the NIO library
Key Takeaways
- The legacy java.io.File API is not deprecated and still works with the latest Java versions.
- java.nio.file.Path does everything java.io.File can and generally does it better, supporting symlinks, file attributes, metadata, and ACLs.
- The java.nio.file.Files class provides many common functions that operate on files and directories.
- Input and output cover a broad range of sources, requiring handling of both character and byte file contents.
- The course covers reading, writing, and working with file metadata.
Frequently Asked Questions
What does this course cover?
It covers how to read and write to and from the file system in Java, including the NIO library, the java.nio.file.Path and java.nio.file.Files classes, file metadata, and creating temporary files. Lessons include Reading, Writing, and File Metadata.
Does this course teach the legacy file API or the newer NIO library?
Both. It explains the legacy file API, which is still usable and not deprecated, while showing how java.nio.file.Path and the NIO library generally do the same work better, with support for symlinks, file attributes, metadata, and ACLs.
What skills will I gain from this course?
You will gain skills in File I/O, I/O processing, Java 11, Java Advanced Imaging, Java APIs, and Non-Blocking I/O in Java.
What are the learning objectives?
The objectives are to describe the new NIO library, understand how to read and write to the file system, and explain how to create temporary files.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hey, and welcome back. Scott Stanlick here. In these lessons, we're gonna take a look at file input/output. And we'll see how to read files, write files, obtain metadata about files. So I wanna start off my just saying that in Java, you have the capability of reading and writing byte streams, which are eight-bit characters or character streams, which are Unicode 16-bit characters, and there's bridging built in to transition between byte streams and character streams or character streams to byte streams. And there are many, many different ways that you can read content in. But for data print streams, piped, channels, byte arrays, string buffers, object input streams, the list is lengthy. There's probably just about anything possible that you would need to do with respect to reading or writing files, I/O. So the first thing we're gonna look at is reading a small file using this new I/O library, and we have a file, this happens to be a Java file actually that we are going to read. We're gonna use the Files class, readAllLines method, pass it the path and give it the character set, which this is UTF-8. So if we run this first test, I get a stream and I'm going to say for each line in the stream or for each element in the stream, print it out. So we can see here, sure enough, that looks like the file, FileReadWriteFun, so there's the file that we're reading. The next one shows us a technique that we can use to not read all the lines in and because if you had a monster file, like huge, very large file, you may not have enough memory to read all the lines. So there's another behavior on the Files class called Lines that will actually do just that. You stream and as you're reading the lines here, only bits of the file are loaded into memory at a time. So it's much more memory efficient. You accomplish the same thing as we will see here. I'm still going to read the entire file. But you read it in chunks as opposed to reading the entire file into memory. So same output, same content, same result rather. It's just a much more memory-efficient way of reading pieces of a file in as opposed to the whole thing. And the last read we're gonna look at is a data input stream where we have control over each and every byte that we read in as opposed to reading lines or all the lines. Well, the approach that is being used here is we're going to find out how many bytes are available on the reader given that path. And we're going to create a byte array here and read all the bytes into that byte array. And then print the contents of the byte array out as a string. So let's see. This should result in the same output. It sure does. So I appreciate you watching. Thank you very much.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.