Java Create File
A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in an (enormous information) is large.
Exactly when the record is getting made by suggestion, the hypothetical way is getting made. The record is one way, in which data will be taken care of as per need.
Steps for creating a file in java:
Step1: Firstly to make a record there will be certainly special cases for defeating those us use attempt to get block procedures.
Step2: Now we will import the File class.
// to import java document
Import java.util.File;
// to make another document
File object_name = new File(directory)
To determine the working framework in the operating system the envelope is made in the work area itself however whereas in Linux and Mac the catalog is /clients/Bhoomika/work area/organizer
In windows the registry is gotten to as \\users\\bhoomika\\desktop\\folder
To make a file we have two strategies. We can get to those two strategies either straight by utilizing the File class or by implication by utilizing the file output stream class.
By utilizing File Class
By utilizing FileOutputStreamClass
- File class:
It is a class only utilized for dealing with
Technique: File.createNewFile()
It is utilized for objects which don't have an actual presence
- FileOutputStream class:
A result stream can be written in FileOutputStream JavaDoc
Procedure: FileOutputStream
Model: echo>myFile.txt
It is used for objects which have a genuine presence
- The two classes give a few techniques that are for the most part utilized in tasks with respect to files
// java program to create a file
// importing the files
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Demo
{
Public static void main(String args[])
{
Demo d1= new Demo();
d1.newFile();
}
Public void newFile()//strategy to make another document
{
String strpath = " ", strName =" ";
//utilizing attempt try block {
BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
Sytem.out.println("enter the document name you needed to access:");
//perusing the record name
strName = br.readLine();
Sytem.out.println("enter the record way you needed to access:");
//making the path for the file which we want
file2 = new File(strPath + " "+ strName+" .txt");
//strategy to make a document
file2.createNewFile();
}
//attempt - get block
Get (Exception ex2){
}
}
Explanation of the above program:
1. To make a file in java we utilized file class.
2.BufferedReader and InputStreamReader classes are utilized to make the files
Paths.
3.These classes are put away under java.io package
4.Here class to be specific Demo and inside that class primary strategy is characterized
5.Inside the main object of the class is made
6.Outside the principal the new file is announced
7.As the file has its own name and way giving a name for it is method
8.To follow file name and way from the client as contribution here BufferedReader class and InputStreamReader class are utilized
9.To print the statement println() capability is utilized
10.readLine() strategy is utilized to take information and store it
11.".txt" is the organization of file expansion
12.Lastly attempt get block is utilized on the grounds that techniques like readLine() and createNewFile() strategies produces special case.
13. So to deal with that exception attempt get block is utilized
File createNewFile() method in java with examples:
- The createNewFile() capability is a part of file class in java. This capability makes new void file.
- It returns true if file does not exist and false if file name already exists.
Function signature:
Public Boolean createNewFile()
Syntax:
Boolean var = file.createNewFile();
Parameters:
This doesn’t accept any parameter
Return type:
This function returns Boolean data type
Exception:
This methods throws exception. They are
- IO EXCEPTION: if there is input output error this exception occurs
- SECURITY EXCEPTION: if the write access to file is denied this error occur
//program to illustrate the usage of create new file()
//java program showing createNewFile()
import java.io.*;
public class Question{
public static void main(String args[]){
try{
File f3=new File(“f3:\\programming.txt”);
//creating the new file
//using if-else block
if(f3.createNewFile())
System.out.println(“the file which you want is created”);
else
System.out.println(“the file which you want is already exsisted”);
}
catch(Exception e){
System.err.println(e);
}
}
Saving a file in java:
- Switch to java programming
- .Create a java document or a current file
- If you need to make changes then, at that point, change the file
- When you wrapped up changing the file press save all
Java source is the file type and “.java” is the source
Java byte code is the file type and “.class” is the source
//creating a java file in directory
File f = new File(“C:/c/d/file.txt”);
//Java create file from string
PROCEDURE:
1.Create an example of file
2.Convert the string into a byte exhibit by utilizing string. getBytes technique()
3.write() with file example and byte cluster
//program to save a string to a file
// Java Program to Save a String to a File
// Using Files.writeString() method
// Importing required classes
import java.io.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
// Main class
public class Demo33 {
// Main method
public static void main(String[] args)
{
// Creating an instance of file
Path path= Paths.get("C:\\Users\\VICTUS\\Desktop\\Demo33.txt");
// Custom string as an input
String str= " \nWelcome to javatpoint \n Hello Bhoomika";
// Try block for checking
try {
// calling Files.writeString() method
// with path, matter
Files.writeString(path, str,StandardCharsets.UTF_8);
}
// Catch block
catch (IOException ex) {
// Printing message exception came as
// invalid. directory local path is passed
System.out.print("Invalid Path searching for");
}
}
}
Methods used for the string program:
- writeString()
- write() for file class, file writer and buffered class
- writer()
The file writer extends output stream writer and the output stream writer extend writer. The writer extends object.
It is the simple way of writing data on file.
Creating a new file in memory:
We can use stream based on a byte buffer which resides in memory by using input and output stream.