Java2-03Files
Java2-03Files
1
Java Files
22
File handling
3
File handling Cont…
import java.io.File
4
File Class Methods
5
Java Create and Write To Files
6
Create a File
7
Getting File Information
import java.io.File;
import java.io.IOException;
9
Delete a Folder
You can also delete a folder. However, it must be empty:
import java.io.File;
public class DeleteFolder {
public static void main(String[] args) {
File myObj = new File("C:\\Users\\MyName\\Test");
if (myObj.delete()) {
System.out.println("Deleted the folder: " + myObj.getName());
} else {
System.out.println("Failed to delete the folder.");
}
}
}
10
Write to a File
import java.io.FileWriter;
import java.io.IOException;
12
Listing all files in a Folder
(“c:\\programs\\java”);
13
Java OutputStream / InputStream class
14
BufferedReader
1515
Create a BufferedReader
// Creates a FileReader
FileReader file = new FileReader(String file);
// Creates a BufferedReader
BufferedReader buffer = new BufferedReader(file);
• read()
- reads a single character from the internal buffer of the reader
• read(char[] array)
- reads the characters from the reader and stores in the specified
array
• read(char[] array, int start, int length)
- reads the number of characters equal to length from the reader
and stores in the specified array starting from the position start
1717
Methods of BufferedReader (Cont…)
• skip()
- To discard and skip the specified number of characters, we
can use the skip() method.
• close() Method
- - To close the buffered reader. Once the close() method is
called, we cannot use the reader to read the data
1818
BufferedReader Example
package testProject;
import java.io.FileReader;
import java.io.BufferedReader;
• The while loop in the below code will read the file until it
has reached the end of file
2121
BufferedWriter
2222
Create a BufferedWriter
// Creates a FileWriter
FileWriter file = new FileWriter(String name);
// Creates a BufferedWriter
BufferedWriter buffer = new BufferedWriter(file);
2323
Methods of BufferedWriter
• write()
▪ writes a single character to the internal buffer of the writer
• write(char[] array)
▪ writes the characters from the specified array to the writer
• write(String data)
▪ writes the specified string to the writer
• Flush()
▪ To clear the internal buffer. This method forces the writer to
write all data present in the buffer to the destination file.
2424
Methods of BufferedWriter (cont’d)
• close()
▪ To close the buffered writer, Once the close() method is
called, we cannot use the writer to write the data.
• newLine()
▪ inserts a new line to the writer
• append()
▪ inserts the specified character to the current writer
2525
BufferedWriter Example
import java.io.FileWriter;
import java.io.BufferedWriter;
2626
BufferedWriter Example (cont’d)
// Writes data to the file
output.write(data);
// Flushes data to the destination
output.flush();
System.out.println("Data is flushed to the file.");
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
2727
More Write Statements
demowrite.write(69); // Printing E
demowrite.newLine(); // inserts a new line
demowrite.write(“Object Programming); // inserts string
demowrite.write(‘B’); // Printing B
2828
InputStreamReader class
2929
Creating an InputStreamReader
3030
Methods of InputStreamReader
3131
Reading input.txt file
package testProject;
import java.io.InputStreamReader;
import java.io.FileInputStream;
public class InputStreamReaderDemo {
3333
getEncoding() Method
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates an InputStreamReader with default encoding
InputStreamReader input1 = new InputStreamReader(file);
// Creates an InputStreamReader specifying the encoding
InputStreamReader input2 = new InputStreamReader(file,
Charset.forName("UTF8"));
3434
getEncoding() Method (cont’d)
// Returns the character encoding of the input stream
System.out.println("Character encoding of input1: " +
input1.getEncoding());
System.out.println("Character encoding of input2: " +
input2.getEncoding());
// Closes the reader
input1.close();
input2.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
3535
Other Methods of InputStreamReader
3636
Create an OutputStreamWriter
• // Creates an OutputStream
FileOutputStream file = new FileOutputStream(String path);
• // Creates an OutputStreamWriter
OutputStreamWriter output = new OutputStreamWriter(file);
// Creates an OutputStreamWriter specifying the character encoding
OutputStreamWriter output = new OutputStreamWriter(file,
Charset cs);
3737
Methods of OutputStreamWriter
3838
OutputStreamWriter Example
try {
OutputStream outputStream = new
FileOutputStream("output.txt");
Writer outputStreamWriter = new
OutputStreamWriter(outputStream);
outputStreamWriter.write("Hello World");
outputStreamWriter.close();
} catch (Exception e) {
e.getMessage();
}
} } 3939
Another Example
public static void main(String[] args) {
try {
OutputStream outputStream = new
FileOutputStream("myOutput.txt");
OutputStreamWriter outputStreamWriter = new
OutputStreamWriter(outputStream, "UTF8");
char[] chars = new char[]{'A','B','C','D','E'};
outputStreamWriter.write(chars);
outputStreamWriter.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
4040