When Reading Words Using a Scanner Object's Next Method, ____.
Java: File Input and Output
Introduction
When data items are stored in a estimator system, they can be stored for varying periods of fourth dimension—temporarily or permanently.
- Temporary storage is commonly chosen reckoner memory or random access retentivity (RAM). When you write a Java program that stores a value in a variable, you are using temporary storage; the value you store is lost when the program ends or the computer loses power. This blazon of storage is volatile.
- Permanent storage, on the other hand, is not lost when a figurer loses power; it is nonvolatile. When y'all write a Java program and save it to a disk, y'all are using permanent storage.
Files exist on permanent storage devices, such as hard disks, Nix disks, USB drives, reels or cassettes of magnetic record, and compact discs .Estimator files are the electronic equivalent of newspaper files often stored in file cabinets in offices.
When you piece of work with stored files in an awarding, you typically perform all or some of the following tasks:
- Determining whether and where a file exists
- Opening a file
- Reading information from a file
- Writing information to a file
- Endmost a file
USING THE File CLASS
You lot tin utilize Java'southward File class to gather file information, such as its size, its most recent modification engagement, and whether the file even exists. Yous must include the following argument to utilize the File form:
import java.io.File;
The java.io package contains all the classes yous use in file processing, so it is usually easiest to import the unabridged parcel using the wildcard * grapheme, as follows:
import coffee.io.*;
The File course is a subclass of the Object grade. Y'all can create a File object using a constructor that includes a filename as its statement, for example, you brand the following argument when Data.txt is a file on the projection root binder:
File fileName = new File("Information.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 proper noun |
String getPath() | Returns the file'southward path |
String getParent() | Returns the name of the binder in which the file tin 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 simply for comparison with other files' times, not as an accented fourth dimension |
boolean isDirectory( ) | When you create a File object and it is a directory, the isDirectory( ) method will return true. |
Let's empathize these method'southward implementation with help of java program. In the primary()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 awarding. We need to create Data.txt file in project root directory else we will get a message saying "File does not exist".
Java Code: Become to the editor
import java.io.File; public class FileClassMethods { public static void primary(String[] args) { File myFile = new File("Information.txt"); if (myFile.exists()) { System.out.println(myFile.getName() + " exists"); Organization.out.println("The file is " + myFile.length() + " bytes long"); if (myFile.canRead()) Organization.out.println(" ok to read"); else System.out.println(" not ok to read"); if (myFile.canWrite()) Organization.out.println(" ok to write"); else Arrangement.out.println(" not ok to write"); Arrangement.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.
- A FileNotFound exception occurs when you try to create a stream or file object using a file that couldn't be located.
- An EOFException indicates that the end of a file has been reached unexpectedly as data was being read from the file through an input stream.
These exceptions are subclasses of IOException. Ane way to deal with all of them is to enclose all input and output statements in a try-catch block that catches IOException objects. Phone call the exception's toString() or getMessage() methods in the catch block to detect out more about the problem
Summary
- All real life application generates lots of data which need to be referred at a later stage. This requirement is achieved using File storage on a permanent storage device like a deejay bulldoze, CD ROM, pen bulldoze etc.
- Java provides a library of classes to access this permanently stored information on files called FileIO or java.io. * Package.
- coffee.io.File grade provides some of the very useful utility methods like permission checking, file size checking, absolute path checking etc.
Java Code Editor:
Previous: Cord buffer grade and cord builder class
Next:Reading file
spradlinlonty1949.blogspot.com
Source: https://www.w3resource.com/java-tutorial/file-input-and-output.php
0 Response to "When Reading Words Using a Scanner Object's Next Method, ____."
Post a Comment