How to Create a File in Java to Read From Input Output
Java: File Input and Output
Introduction
When information items are stored in a computer organisation, they tin exist stored for varying periods of time—temporarily or permanently.
- Temporary storage is usually called computer retentivity or random access memory (RAM). When y'all write a Coffee program that stores a value in a variable, you are using temporary storage; the value you store is lost when the programme ends or the computer loses ability. This blazon of storage is volatile.
- Permanent storage, on the other hand, is non lost when a reckoner loses power; information technology is nonvolatile. When you write a Java programme and save it to a deejay, you are using permanent storage.
Files exist on permanent storage devices, such as difficult disks, Naught disks, USB drives, reels or cassettes of magnetic tape, and compact discs .Computer files are the electronic equivalent of paper files oft stored in file cabinets in offices.
When yous work with stored files in an application, you typically perform all or some of the following tasks:
- Determining whether and where a file exists
- Opening a file
- Reading data from a file
- Writing data to a file
- Closing a file
USING THE File Course
Y'all tin can use Java's File class to gather file information, such equally its size, its virtually recent modification engagement, and whether the file fifty-fifty exists. You lot must include the post-obit argument to utilize the File class:
import java.io.File;
The coffee.io parcel contains all the classes yous use in file processing, and so it is unremarkably easiest to import the entire packet using the wildcard * character, equally 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 every bit its argument, for example, y'all make the following statement when Data.txt is a file on the projection 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 truthful if file exists |
| String getName() | Returns file proper name |
| Cord getPath() | Returns the file's path |
| String getParent() | Returns the name of the folder in which the file can exist found |
| long length() | Returns the file's size |
| long lastModified() | Returns the time the file was last modified; this time is arrangement dependent and should be used simply for comparison with other files' times, non equally an absolute time |
| boolean isDirectory( ) | When you create a File object and it is a directory, the isDirectory( ) method will return true. |
Let's sympathize 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 information technology, the file is known every bit myFile within the awarding. Nosotros need to create Data.txt file in project root directory else we will go a message proverb "File does not exist".
Coffee Code: Become to the editor
import coffee.io.File; public course FileClassMethods { public static void main(String[] args) { File myFile = new File("Data.txt"); if (myFile.exists()) { Organization.out.println(myFile.getName() + " exists"); Organisation.out.println("The file is " + myFile.length() + " bytes long"); if (myFile.canRead()) System.out.println(" ok to read"); else Organization.out.println(" not ok to read"); if (myFile.canWrite()) System.out.println(" ok to write"); else System.out.println(" not ok to write"); Organization.out.println("path: " +myFile.getAbsolutePath()); System.out.println("File Name: "+ myFile.getName()); Organization.out.println("File Size: "+ myFile.length() + " bytes"); } else Organization.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 parcel might occur when y'all are working with files and streams.
- A FileNotFound exception occurs when yous try to create a stream or file object using a file that couldn't exist located.
- An EOFException indicates that the stop of a file has been reached unexpectedly as data was beingness read from the file through an input stream.
These exceptions are subclasses of IOException. One style to deal with all of them is to enclose all input and output statements in a endeavour-take hold of block that catches IOException objects. Call the exception'due south toString() or getMessage() methods in the catch block to find out more about the trouble
Summary
- All real life application generates lots of data which need to exist referred at a after stage. This requirement is achieved using File storage on a permanent storage device like a disk drive, CD ROM, pen drive etc.
- Java provides a library of classes to access this permanently stored data on files chosen FileIO or java.io. * Package.
- java.io.File class provides some of the very useful utility methods similar permission checking, file size checking, absolute path checking etc.
Java Code Editor:
Previous: String buffer class and string builder course
Next:Reading file
Source: https://www.w3resource.com/java-tutorial/file-input-and-output.php
0 Response to "How to Create a File in Java to Read From Input Output"
Post a Comment