Home » Java programming language

What are java files?

In this tutorial, we are going to learn about the java files, what are java files, how to work with files in java, how to read and write files in java?
Submitted by Preeti Jain, on July 01, 2019

Java files

The file is a class of java.io package.

If we create a file then we need to remember one thing before creating a file. First, we need to check whether a file exists of the same name or not. If a file of the same name exists then we can’t create a file of same name else we can create a file of same.

We will study three things:

  • Creating a file
  • Reading a file
  • Writing a file

1) Creating a file

To create a file by using createNewFile() method and the return type of this method is Boolean so it returns true or false. It returns true during successful creation of the file and It returns false during the creation of file failure.

Example:

// import the File class because we will use File class methods
import java.io.File;
//import the Exception class because it may raise 
//an exception when working with files
import java.lang.Exception;

public class CreateFile {
    public static void main(String[] args) {
        try {

            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");

            // createNewFile() returns true if file is successfully 
            // created and then we will get the name of the file 
            // using getName() method and return false if the file 
            // is already exists then we will get the message

            if (file.createNewFile()) {
                System.out.println("File created: " + file.getName());
            } else {
                System.out.println("File already exists of same name!! Please try to create from other name ");
            }
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac CreateFile.java

D:\Programs>java 
File created: myjava.txt

2) Writing a file

To write a file by using write() method of FileWriter class.

Example:

// import the FileWriter class because 
// we will use FileWriter class methods write()
import java.io.FileWriter;
//import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class WriteFile {
    public static void main(String[] args) {
        try {
            // Create an object of FileWriter class 
            FileWriter fw = new FileWriter("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt ");

            // To write a file by using write() method      
            fw.write("We are going to write a file by using write()");

            // After writing a file then we need to close safely
            fw.close();

            //After successfully written of file then display a message for the user 
            System.out.println("File has been written successfully");
        } catch (Exception e) {
            System.out.println("An error occurred");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac WriteFile.java

D:\Programs>java WriteFile
File has been written successfully

3) Reading a file

To read a file by using nextLine() method of Scanner class.

Example:

// import the File class because we will 
// use File class methods
import java.io.File;
//import the Exception class because it may 
// raise an exception when working with files
import java.lang.Exception;
// import the Scanner class to read file from user
import java.util.Scanner;

public class ReadFile {
    public static void main(String[] args) {
        try {
            File fr = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt ");
            Scanner sc = new Scanner(fr);
            while (sc.hasNextLine()) {
                String file_read = sc.nextLine();
                System.out.println(file_read);
            }
            sc.close();
        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

D:\Programs>javac ReadFile.java

D:\Programs>java ReadFile
We are going to write a file by using write()





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.