AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
We should always remember to close the stream or else it might create dump memory.Ĭode to Write into a File using OutputStreamWriter: package This class writes 8 bits of data at a time. We can use the OutputStreamWriter class of the byte stream to write into a file. We can write into a file using the byte stream class OutputStreamWriter or Character stream class FileWriter. We use the try-catch block to check if the file exists or not. The above class uses methods like getName(), getAbsolutePath(), canWrite(),canRead(),length(), to get various information about the file NewFile.txt. The absolute path of the file is: G:\Internship\File Handling\NewFile.txt ("The size of the file is: " finfo.length()) ("Is file readable: " finfo.canRead()) ("Is file writeable: " finfo.canWrite()) ("The absolute path of the file is: " finfo.getAbsolutePath()) ("The name of the file is: " finfo.getName()) These methods can give information like name, length, path, read-only, write-only, etc.Ĭode to Explain Methods to get various file information: package įile finfo = new File("G:\\Internship\\File Handling\\NewFile.txt") Through the various methods given in File class, we are able to get all sorts of information about a file. The Try-Catch block is used to handle errors if there is already a file with the same name. The File named NewFile.txt was created in the given path. ("An unexpected error is occurred.") įile NewFile.txt is created successfully. ("File is already exist in the directory.") ("File " fcreate.getName() " is created successfully.") If the method returns true, the file has been created successfully, else the file creation was unsuccessful.Ĭode to explain the working of createNewFile(): package įile fcreate = new File("G:\\Internship\\File Handling\\NewFile.txt") We can use the createNewFile() method to create a new file using Java. Now we will discuss the different operations for File Handling in Java using both byte stream and character stream. This method helps us create a new directory(Not a file). This method returns an array, listing all the files present in the present working directory(PWD). This method returns the size of a file in bytes. This method returns the absolute path of the given file. This method helps us find the name of a particular file from the directory. This method is used to delete a file from the directory. This method verifies if the file asked for is present or not in the directory. This method checks whether the file is writable or not,i.e, not a read-only file. This method creates a new file in the desired path. This method checks whether the file is readable or not. Let us discuss them through this easy to comprehend table. The file class contains various methods that perform various important tasks. The methods present in File class(java.io.File): SIMPLE READER AND SIMPLE WRITER IN JAVA PURPOSE HOW TOWe will see how to use them later while discussing the various file operations. The two main classes used in Character Stream are FileReader and FileWriter. Character streams usually use Byte Stream classes to implement operations. It is faster as it can take double the intake as compared to a byte stream. Character Stream takes 2 bytes of data at a time. There is also Character Stream which allows I/O operation on 16bit of Unicode data at a time. The most commonly used Input and Output Stream Classes are FileInputStream and FileOutputStream. Output Stream Classes: This stream helps to give output(Write Data) into the collection in I/O File. Input Stream Classes: This stream helps take input(Read Data) from the collection in I/O File.Ģ. The byte stream contains two stream classes, Input Stream classes and Output Stream Classes.ġ. This stream performs an Input-output operation per 8bit of data. Thus, this stream mainly deals with 8bit of data. We know that one byte is equal to eight-bit. The byte stream deals with mainly byte data. A stream can not be called to be a data structure, rather it just takes input from the collection of I/O.Ī stream can be classified into two types: Byte Stream and Character Stream. Stream is a concept of java that pipelines a sequence of objects to obtain the desired result. Join DataFlair on Telegram!! Stream in Java: Stay updated with latest technology trends
0 Comments
Read More
Leave a Reply. |