Java File Operations


Today I’m going to show how to do file operations in java.

Information Record to Files

INPUT AND OUTPUT CURVES

Input-Output currents in Java are divided into two main groups:

  1. Characteristic currents
  2. Byte origin currents

Separate classes are defined for each of these.

Character Origins

In this section, we will first review the information record with the characters based on the character stream and the files on the memory. We perform a character type information record on memory using the subclasses of the Writer class.

Using the FileWriter Class

The following sample Java program records information to a text type file named sample.txt with the help of this object by creating an object named filewriter from the FileWriter class. One line of information is written to the file by write method and the end of line is determined by the character articles\ n write.

When we run the application with Eclipse, we see that a sample.txt file is created in the directory where the project is located:

Run Code

We have specified that the content of a text file can be viewed with the help of any text editor program. We’ve seen this example here. We can also display the contents of a file named sample.txt in a Java program. We’re going to see an example of this a little later. However, the content of byte-type or binary files can only be viewed with the help of the Java program.

Text Type Reading Information From File

We use the Reader class and its subclasses to read information from a text-type file.

With the FileReader class, characters from a text-type file on the disk are converted to char in memory. If characters other than the English alphabet are used, supported by the UTF format, a character can be occupied by 3 bytes.

It is generally more efficient to place data read from the disk in a buffer in memory first. The BufferedReader class is used for this purpose.

SAMPLE JAVA PROGRAM: Reading with FileReader and BufferedReader

As in the following, we see that the contents of the text file sample.txt are listed:

BINARY FILES

In text-type files, each byte or byte is interpreted as an ASCII code, while binary files of different lengths are interpreted as different types of data in binary files. This comment is performed by the software. For example, in a Java binary file, an int type data is stored with a 4-byte data group.

The base class for saving data to a binary file is the OutputStream class.

The following code example shows how to copy the binary files to each other.

 

 

if you have question do not forget to write from the chat button next to it or from the comment

You may also like...

3 Responses

  1. zvodret iluret says:

    Sweet web site, super pattern, real clean and employ genial.

  2. folorentorium says:

    Sweet website , super design and style, very clean and apply pleasant.

Leave a Reply

Your email address will not be published. Required fields are marked *