0% found this document useful (0 votes)
2 views

Java-File-Handling

The document provides an overview of file handling in Java, including methods for creating, reading, updating, and deleting files using the File class. It also explains the StringBuffer and StringBuilder classes, highlighting their mutable nature and various methods such as append, insert, replace, delete, and capacity management. Additionally, it contrasts StringBuffer and StringBuilder, noting that StringBuilder is non-synchronized and available since JDK 1.5.

Uploaded by

ramkumarlaksh74
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java-File-Handling

The document provides an overview of file handling in Java, including methods for creating, reading, updating, and deleting files using the File class. It also explains the StringBuffer and StringBuilder classes, highlighting their mutable nature and various methods such as append, insert, replace, delete, and capacity management. Additionally, it contrasts StringBuffer and StringBuilder, noting that StringBuilder is non-synchronized and available since JDK 1.5.

Uploaded by

ramkumarlaksh74
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Java has several methods for creating, reading, updating, and deleting files.

Java File Handling


------------------
The File class from the java.io package, allows us to work with files.

To use the File class, create an object of the class, and specify the filename or
directory name:

Creating a File :
----------------

import java.io.File; // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

Create and Reading a File:


--------------------------

import java.io.File;
import java.io.IOException;

public static void main(String[] args) {


try {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
File from Different Source:
--------------------------

File myObj = new File("C:\\Users\\MyName\\filename.txt");

Write a File:
------------

public static void main(String[] args) {


try {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Files in Java might be tricky, but it is fun enough!");
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

Read the Files:


--------------
import java.util.Scanner;

public static void main(String[] args) {


try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}

myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}

Get File Information


--------------------

public static void main(String[] args) {


File myObj = new File("filename.txt");
if (myObj.exists()) {
System.out.println("File name: " + myObj.getName());
System.out.println("Absolute path: " + myObj.getAbsolutePath());
System.out.println("Writeable: " + myObj.canWrite());
System.out.println("Readable " + myObj.canRead());
System.out.println("File size in bytes " + myObj.length());
} else {
System.out.println("The file does not exist.");
}
}

Delete a File:
--------------

public static void main(String[] args) {


File myObj = new File("filename.txt");
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
}

String Buffer:
--------------

Java StringBuffer class is used to create mutable (modifiable) String objects. The
StringBuffer class in Java is the same as String class except it is mutable i.e. it
can be changed.

Constructor Description
StringBuffer() It creates an empty String buffer with the
initial capacity of 16.
StringBuffer(String str) It creates a String buffer with the specified
string..
StringBuffer(int capacity) It creates an empty String buffer with the specified
capacity as length.

What is a mutable String?


A String that can be modified or changed is known as mutable String. StringBuffer
and StringBuilder classes are used for creating mutable strings.

StringBuffer Class append() Method :


----------------------------------
The append() method concatenates the given argument with this String.

public static void main(String args[])


{
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}

StringBuffer insert() Method


----------------------------
The insert() method inserts the given String with this string at the given
position.

public static void main(String args[]){


StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}

StringBuffer replace() Method


-----------------------------
The replace() method replaces the given String from the specified beginIndex and
endIndex.

public static void main(String args[]){


StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}

StringBuffer delete() Method


----------------------------
The delete() method of the StringBuffer class deletes the String from the specified
beginIndex to endIndex.

public static void main(String args[]){


StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}

StringBuffer reverse() Method


-----------------------------
The reverse() method of the StringBuilder class reverses the current String.

public static void main(String args[]){


StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}

StringBuffer capacity() Method


------------------------------

The capacity() method of the StringBuffer class returns the current capacity of the
buffer. The default capacity of the buffer is 16. If the number of character
increases from its current capacity, it increases the capacity by
(oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.

public static void main(String args[]){


StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
}

StringBuffer ensureCapacity() method


------------------------------------
The ensureCapacity() method of the StringBuffer class ensures that the given
capacity is the minimum to the current capacity. If it is greater than the current
capacity, it increases the capacity by (oldcapacity*2)+2. For example if your
current capacity is 16, it will be (16*2)+2=34.

public static void main(String args[]){


StringBuffer sb=new StringBuffer();
System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70
}

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------

String Builder:

Java StringBuilder class is used to create mutable (modifiable) String. The Java
StringBuilder class is same as StringBuffer class except that it is non-
synchronized. It is available since JDK 1.5.

StringBuilder() It creates an empty String Builder with the initial capacity of


16.
StringBuilder(String str) It creates a String Builder with the specified
string.
StringBuilder(int length) It creates an empty String Builder with the specified
capacity as length.
StringBuilder append() method
-------------------------------
The StringBuilder append() method concatenates the given argument with this String.

public static void main(String args[]){


StringBuilder sb=new StringBuilder("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}

StringBuilder insert() method


The StringBuilder insert() method inserts the given string with this string at the
given position.

public static void main(String args[]){


StringBuilder sb=new StringBuilder("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}

StringBuilder replace() method


The StringBuilder replace() method replaces the given string from the specified
beginIndex and endIndex.

public static void main(String args[]){


StringBuilder sb=new StringBuilder("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}

StringBuilder delete() method


-----------------------------
The delete() method of StringBuilder class deletes the string from the specified
beginIndex to endIndex.

StringBuilder sb=new StringBuilder("Hello");


sb.delete(1,3);
System.out.println(sb);//prints Hlo

StringBuilder reverse() method


The reverse() method of StringBuilder class reverses the current string.

public static void main(String args[]){


StringBuilder sb=new StringBuilder("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}

StringBuilder capacity() method


The capacity() method of StringBuilder class returns the current capacity of the
Builder. The default capacity of the Builder is 16. If the number of character
increases from its current capacity, it increases the capacity by
(oldcapacity*2)+2. For example if your current capacity is 16, it will be
(16*2)+2=34.

StringBuilder sb=new StringBuilder();


System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("Java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2

StringBuilder ensureCapacity() method


The ensureCapacity() method of StringBuilder class ensures that the given capacity
is the minimum to the current capacity. If it is greater than the current capacity,
it increases the capacity by (oldcapacity*2)+2. For example if your current
capacity is 16, it will be (16*2)+2=34.

StringBuilder sb=new StringBuilder();


System.out.println(sb.capacity());//default 16
sb.append("Hello");
System.out.println(sb.capacity());//now 16
sb.append("Java is my favourite language");
System.out.println(sb.capacity());//now (16*2)+2=34 i.e (oldcapacity*2)+2
sb.ensureCapacity(10);//now no change
System.out.println(sb.capacity());//now 34
sb.ensureCapacity(50);//now (34*2)+2
System.out.println(sb.capacity());//now 70

You might also like