0% found this document useful (0 votes)
9 views10 pages

1 File Handling

Uploaded by

Salam Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

1 File Handling

Uploaded by

Salam Abdulla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

University of sulaimani

College of science
Department of Computer Science

Compiler
File handling in java

2023-2024

Mzhda Hiwa Hama


Writing a file in Java

• In Java programming language text files can be written


using three classes File, FileWriter which should usually
be wrapped in BufferedWriter.

• FileWriter: The class is used for writing streams of


characters.

• This class has several constructors to create required


objects

• Following syntax creates a FileWriter object given a file


name.
FileWriter(String fileName)
To write a text file, simply follow these steps

• Define a new object from File


File file = new File(“filename”); // write path of the file

• Create a fileWriter object (Assume default encoding).


FileWriter fileWriter = new FileWriter(file);

• Wrap FileWriter in BufferedWriter.


BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

• Use write() method to write a text in the newly created file.


bufferedWriter.write("Hello there,");
• Use newLine() method to append a new line character.
bufferedWriter.write(" here is some text.");
bufferedWriter.newLine();

• Use close() at the end of the program to close the file .


bufferedWriter.close();
Sample Code
public class WriteToFile {
public static void main(String [] args) {
String fileName = "temp.txt"; // write path of the file you want to open
try {
File file = new File (fileName);
// Assume default encoding.
FileWriter fileWriter = new FileWriter( file);
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("Hello there,");
bufferedWriter.newLine();
bufferedWriter .write(" here is some text.");
bufferedWriter.close(); // Always close files.
}
catch(IOException ex) {
System.out.println("Error writing to file '"+ fileName + "'");
}
}
}
Reading a file in Java

• To read a text file, basically all the same stuff (steps


for writing) applied, except you’ll deal with classes
named FileReader and BufferedReader.

• FileReader for text files in your system’s default


encoding.

• We can also use FileInputStream for binary files


and text files that contain ‘weird’ characters.
Reading a text file

• Specify the name of the file to open


String fileName = “ ";
• Create a fileReader Object
FileReader fileReader = new FileReader(fileName);
• Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
• Read the file line by line
String line = null;
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
Sample code

public class ReadingFile {


public static void main(String [] args) {
String fileName = "temp.txt"; // The name of the file to open.
String line = null; // This will reference one line at a time
try {
FileReader fileReader = new FileReader(fileName); // FileReader reads
text files in the default encoding.
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close(); // Always close files.
}
catch(FileNotFoundException ex) {
System.out.println( "Unable to open file '" + fileName + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + fileName + "'");
// Or we could just do this: ex.printStackTrace();
}
}
}

You might also like