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:
- Characteristic currents
- 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.
2 3 4 5 6 7 8 9 10 11 12 13 |
import java.io.FileWriter; import java.io.IOException; public class FileOperation { public static void main(String[] args) throws IOException { String fileName= "sample.txt"; FileWriter fileWriter= new FileWriter(fileName); fileWriter.write("hello "); fileWriter.write("tahasivaci.com "); fileWriter.close(); } } |
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
2 |
hello tahasivaci.com |
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
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TextFileRead{ public static void main (String[] args){ String fileName= "sample.txt"; String line; try{ BufferedReader read= new BufferedReader(new FileReader(fileName)); line = read.readLine(); while (line!= null) { System.out.println(line); line= read.readLine(); } read.close(); } catch (IOException iox){ System.out.println(fileName+" file is not read."); } } } |
As in the following, we see that the contents of the text file sample.txt are listed:
2 |
hello tahasivaci.com |
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.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class BinaryFile{ public static void main(String[] args) { //specify the path of files. String sourceFile= "source file path"; String targetFile= "target file path"; try { //Create your InputStream and OutputStream objects for copying. FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(targetFile); //Specify the location of the first letter of the line to be copied. int offset = 0; //Define the size of the line to be copied. int lineSize= 0; //create a array for line to copied byte[] line= new byte[1024]; //Read the first line and get the size lineSize= fis.read(line); //If the size of the read lines is not -1, that is the end, copy. while (lineSize!= -1) { //write to target file. fos.write(line, offset, lineSize); //Yeni bir satır okuyun. lineSize= fis.read(line); } //Close both files. fis.close(); fos.close(); } catch (IOException e) { //In the event of an error, alert your program. System.out.println("Something went wrong " + e); } } } |
if you have question do not forget to write from the chat button next to it or from the comment
Sweet web site, super pattern, real clean and employ genial.
Sweet website , super design and style, very clean and apply pleasant.
Thank you 🙂