A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/java-tutorial/file-input-and-output.php below:

Website Navigation


Java: File Input and Output

Java: File Input and OutputLast update on August 19 2022 21:50:42 (UTC/GMT +8 hours) Introduction

When data items are stored in a computer system, they can be stored for varying periods of time—temporarily or permanently.

Files exist on permanent storage devices, such as hard disks, Zip disks, USB drives, reels or cassettes of magnetic tape, and compact discs .Computer files are the electronic equivalent of paper files often stored in file cabinets in offices.

When you work with stored files in an application, you typically perform all or some of the following tasks:

USING THE File CLASS

You can use Java’s File class to gather file information, such as its size, its most recent modification date, and whether the file even exists. You must include the following statement to use the File class:

import java.io.File;

The java.io package contains all the classes you use in file processing, so it is usually easiest to import the entire package using the wildcard * character, as follows:

import java.io.*;

The File class is a subclass of the Object class. You can create a File object using a constructor that includes a filename as its argument, for example, you make the following statement when Data.txt is a file on the project root folder:

File fileName = new File("Data.txt");

Below is the list of some of important File class methods with purpose and method signature

Method name/Signature Purpose boolean canRead() Returns true if a file is readable boolean canWrite() Returns true if a file is writable boolean exists() Returns true if file exists String getName() Returns file name String getPath() Returns the file’s path String getParent() Returns the name of the folder in which the file can be found long length() Returns the file’s size long lastModified() Returns the time the file was last modified; this time is system dependent and should be used only for comparison with other files’ times, not as an absolute time boolean isDirectory( ) When you create a File object and it is a directory, the isDirectory( ) method will return true.

Let’s understand these method’s implementation with help of java program. In the main()method, a File object named myFile is declared. The String passed to the constructor is “SomeData.txt”, which is the stored file’s system name. In other words, although SomeData.txt might be the name of a stored file when the operating system refers to it, the file is known as myFile within the application. We need to create Data.txt file in project root directory else we will get a message saying “File does not exist”.

Java Code: Go to the editor

import java.io.File;
public class FileClassMethods {
	public static void main(String[] args) {
		File myFile = new File("Data.txt");
		if (myFile.exists()) {
			System.out.println(myFile.getName() + " exists");
			System.out.println("The file is " + myFile.length() + " bytes long");
			if (myFile.canRead())
				System.out.println(" ok to read");
			else
				System.out.println(" not ok to read");
			if (myFile.canWrite())
				System.out.println(" ok to write");
			else
				System.out.println(" not ok to write");
			System.out.println("path: " +myFile.getAbsolutePath());
			System.out.println("File Name: "+ myFile.getName());
			System.out.println("File Size: "+ myFile.length() + " bytes");
		} else
			System.out.println("File does not exist");
	}
}

Output:

If file is not available in project root folder.


when the file is present:


Handling Exceptions

Several exceptions in the java.io package might occur when you are working with files and streams.

These exceptions are subclasses of IOException. One way to deal with all of them is to enclose all input and output statements in a try-catch block that catches IOException objects. Call the exception’s toString() or getMessage() methods in the catch block to find out more about the problem

Summary

Java Code Editor:

Previous: String buffer class and string builder class
Next:Reading file


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4