AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Java new file8/6/2023 We check whether we can read the data of the file or not using the canRead().We check whether we can write data into a file or not using the canWrite().We get the absolute path of the file using the getAbsolutePath() method of the file.We get the name of the file using the getName().We check the existence of the file using a conditional statement, and if it is present, we get the following information about that file: In the main method, we create an object of the text file which we have created in our previous example. In the above code, we import the java.io.File package and create a class FileInfo. ("The size of the file in bytes is: " + f0.length()) Getting the length of the file in bytes Checking whether the file is readable or not ("Is file writeable?: " + f0.canWrite()) Checking whether the file is writable or not ("The absolute path of the file is: " + f0.getAbsolutePath()) ("The name of the file is: " + f0.getName()) Let's take an example to understand how to use file methods to get the information of the file. We use several methods to get the information about the file like name, absolute path, is readable, is writable and length. The operation is performed to get the file information. If there is any error, it gets handled in the catch block. If the method returns false, it will jump to the else section. In the try block, we call the createNewFile() method through the f0 object to create a new file in the specified location. We create the f0 object of the File class and specify the location of the directory where we want to create a file. In the above code, we import the File and IOException class for performing file operation and handling errors, respectively. ("File is already exist in the directory.") ("File " + f0.getName() + " is created successfully.") Importing the IOException class for handling errorsįile f0 = new File("D:FileOperationExample.txt") Let's take an example of creating a file to understand how we can use the createNewFile() method to perform this operation. The createNewFile() method returns true when it successfully creates a new file and returns false when the file already exists. We use the createNewFile() method of file. We can perform the following operation on a file:Ĭreate a File operation is performed to create a new file. The mkdir() method is used for creating a new directory. The list() method is used to get an array of the files available in the directory. The length() method is used to get the size of the file in bytes. The getAbsolutePath() method is used to get the absolute pathname of the file. The getName() method is used to find the file name. The delete() method is used to delete a file. The exists() method is used to check whether the specified file is present or not. The canWrite() method is used to check whether we can write the data into the file or not. The createNewFile() method is used to create a new empty file. The canRead() method is used to check whether we can read the data of the file or not. To get more knowledge about the stream, click here. A file handling process with a character stream is a process in which an input is provided and executed with the character data. Character StreamĬharacter Stream is mainly involved with character data. A file handling process with a byte stream is a process in which an input is provided and executed with the byte data. Byte Streamīyte Stream is mainly involved with byte data. In Java, Stream is classified into two types, i.e., Byte Stream and Character Stream. StreamĪ series of data is referred to as a stream. If you have knowledge about both of them, you can skip it. There are several File Operations like creating a new File, getting information about File, writing into a File, reading from a File and deleting a File.īefore understanding the File operations, it is required that we should have knowledge of Stream and File methods. A named location used to store related information is known as a File. In Java, a File is an abstract data type.
0 Comments
Read More
Leave a Reply. |