QA's approach 2 Java - File Handling -FileReader & FileWriter

File Handling - Learn through Examples


To have an insights into File Handling theory, follow the link - File Handling Theory  

Byte Stream implementation examples:

Example1. Read Data from file & write to another file using FileReader & FileWriter



package fileHandling;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CharacterStream {
public static void main(String[] args) throws IOException {

String sourcePath = "C:/Users/user/workspace/New folder/javaForQA/src/fileHandling/Input.txt";
String destinationPath = "C:/Users/user/workspace/New folder/javaForQA/src/fileHandling/Output.txt";

FileReader inputStream = null;
FileWriter outputStream = null;

try {
inputStream = new FileReader(sourcePath);
outputStream = new FileWriter(destinationPath);

int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}


Execute the Program:

Shift+Alt+x, J

Output.txt file content is overidden by Input.txt file content.




Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?