.txt
).xml
).json
).csv
)H
β 'H'
(character representation)'ΰ€
'
β stored correctly in fileReader
(FileReader
):
import java.io.FileReader;
import java.io.IOException;
public class ReadFileUsingReader
{
public static void main(String[] args)
{
try
{
// Open file for reading
FileReader fr = new FileReader("d://aaa.txt");
int i;
System.out.println("----- Reading data from file using FileReader -----");
while ((i = fr.read()) != -1)
{
System.out.print((char) i); // read character by character
}
fr.close();
}
catch (IOException e)
{
System.out.println("Error reading file: " + e.getMessage());
}
}
}
aaa.txt
file in D drive (d://).
Writer
(FileWriter
):
import java.io.FileWriter;
import java.io.IOException;
public class WriteFileUsingWriter
{
public static void main(String[] args)
{
try
{
// Open file for writing
FileWriter fw = new FileWriter("d://bbb.txt");
String data = "Hello, this is my first Java Program using FileWriter to write data in file....";
fw.write(data); // write string directly
fw.close();
System.out.println("Data successfully written to bbb.txt");
}
catch (IOException e)
{
System.out.println("Error writing file: " + e.getMessage());
}
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.