File in JAVA

File in JAVA

File in JAVA

File in JAVA

The file is a container which stores data permanently
Data can be stored in two different ways
:-For a temporary period with RAM memory
:-For permanently period with HDD
Data store in variable array link list is temporary because all allocate memory from RAM
Tor store data permanently programming language support two different ways

–>With the help of file
–>With the database

File

File is a predefined class present in java.io package
This class is responsible to create and manipulate the file.
The file class cannot do any read-write operation.

Constructor of file class:-

file(String Fil_name) –> By default location is the current location(pwd).
File(String path, String File_name) –> To create locate fike other than current folder.

boolean createNewFile()–>This method is responsible to create new file and returns boolean value.If successfully create return true otherwise false.

boolean mkdir()–>It is responsible to create a new folder, On successfully create it returns true otherwise false.

boolean exists()–>It is used for file existence

boolean canRead() –> It is used to check the file is readable or not

void setReadOnly()–>This method makes a file read-only mode.

long lastModified() –> It returns the last modified time in the form of the millisecond.

boolean canWrite() –>It checks whether the file is writable or not.

boolean is Hidden() –>It checks whether the file is hidden or not.

boolean renameTo(File object)–>This method rename a file.

isFile()–>This method always check given path contains a file or not.

String getAbsolutePath()–> This method always returns the full path of the file.

String getName() –>Return the name of the file.

bolean is Directory() –>It check the given path contains a directory or not.

boolean mkdir() –> This method change the permission of a file.\

PROGRAM
import java.io.File;
public class File1 
{
public static void main(String[] args) throws Exception
{
File f1=new File("hello.txt");
File f2=new File("hi.txt");
System.out.println(f1.createNewFile());
System.out.println(f1.length());
System.out.println(f1.getName());
System.out.println(f1.getAbsolutePath());
System.out.println(f1.isFile());
System.out.println(f1.exists());
System.out.println(f1.delete());
System.out.println(f1.getAbsolutePath());
System.out.println(f1.exists());
System.out.println(f1.renameTo(f2));
System.out.println(f1.getName());
}

}

Read Write Operation with File in Character Format:-

java.io package provides two different classes to read and write in a character format.

1)FileInputStream
2)FileOutputStream

FileInputStream:-

This class is used to read data from the file by the help of read() method.
syntax:-
FileInputStream fis=new FileInputStream(String FIle_name/File Object);
It always checks the file existence. If file not available it terminates the program by throwing FileNotFound Exception.

FileOutputStream:-

This class is used to write data into the file by the help of write().
syntax:-
FileOutputStream fos=new FileOutputStream(String File_Name/File Object);
It creates a new file if the file is not available otherwise replace the existing file.

PROGRAM:-
//java program for write and read data from the file in character format
import java.io.*;
import java.util.*;
public class File2 
{
public static void main(String[] args) throws Exception 
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the name of the file name");
String name=sc.next();
File f1=new File(name);
if(!f1.exists())
{
System.out.println("file not available");
System.exit(0);
}
System.out.println("enter the destination");
String des=sc.next();
FileInputStream fis=new FileInputStream(f1);
FileOutputStream fos=new FileOutputStream(des);
int ch;
while((ch=fis.read())!=-1)
{
System.out.println((char)ch);
fos.write(ch);
Thread.sleep(4000);
}
fis.close();
fos.close();
}
}

Read and write operation in string format:-
java.io package provides two different classes to read and write operation in string format.
1)BufferedReader
2)BufferedWriter

Bufferedreader:-
This data is used to read data from file by the help of readLine() method. It always checks the file existence, if the file is not available then it terminates the program with the help of FileNotFoun Exception.

syntax:-
BufferedReader br=new BufferedReader(new FileReader(File Object/String File_Name));

BufferedWriter:-

This class always write data in a string format with the help of write() method.

syntax:-

BufferedWriter bw=new BufferedWriter(new FileWriter(File Object/String File_Name/true));

This above syntax creates a new file if the file is not available otherwise append the existing.

PROGRAM:-
import java.io.*;
import java.util.Scanner;
public class File3 
{
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws Exception 
{
File f1=new File(args[0]);
if(!f1.exists())
{
System.out.println("the system cannot find the specified file");
System.exit(0);
}
BufferedReader br=new BufferedReader(new FileReader(args[0]));
System.out.println("enter the destination");
String des=sc.next();
BufferedWriter bw=new BufferedWriter(new FileWriter(des));
String line;
while((line=br.readLine())!=null)
{
System.out.println(line);
Thread.sleep(4000);
bw.write(line);
bw.write("\n");
}
bw.close();
br.close();
}
}
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community