Categories
Digital Marketing

Java Programming Tutorial – 81 – Reading from Files

Cheap and easy Video Content Marketing

import java.io.File;  // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

The File class has many useful methods for creating and getting information about files. For example:

MethodTypeDescriptioncanRead()BooleanTests whether the file is readable or notcanWrite()BooleanTests whether the file is writable or notcreateNewFile()BooleanCreates an empty filedelete()BooleanDeletes a fileexists()BooleanTests whether the file existsgetName()StringReturns the name of the filegetAbsolutePath()StringReturns the absolute pathname of the filelength()LongReturns the size of the file in byteslist()String[]Returns an array of the files in the directorymkdir()BooleanCreates a directory

If you don't know what a package is, read our Java Packages Tutorial.

Create a File

Use the createNewFile() method to create a file. This method returns a boolean value: true if the file was successfully created, and false if the file already exists. Note that the method is enclosed in a try…catch block. This is necessary because it throws an IOException if an error occurs (if the file cannot be created for some reason):

Example

import java.io.File;  // Import the File class
import java.io.IOException; …

Watch/Read More

Categories
Digital Marketing

In this weeks Week in Review: Quibi shuts down just six months after launching, Google claims its seeing success from its sandbox experiments, and Snapchat sees growing revenues and user engagement. To receive an update on the industrys top stories every Friday, sign up to the weekly Video Round-Up.