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

Java Text IO (Group 4) - A

This document discusses Java file input/output (I/O). It describes the Java File class, which represents file paths, and I/O stream classes like Scanner and PrintWriter for reading from and writing to files. Text files store data in human-readable form while binary files use binary. The document provides examples for reading a file using Scanner and writing a file using PrintWriter. It also briefly mentions the JFileChooser class for displaying file dialogs in GUI applications.

Uploaded by

OLI
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)
10 views

Java Text IO (Group 4) - A

This document discusses Java file input/output (I/O). It describes the Java File class, which represents file paths, and I/O stream classes like Scanner and PrintWriter for reading from and writing to files. Text files store data in human-readable form while binary files use binary. The document provides examples for reading a file using Scanner and writing a file using PrintWriter. It also briefly mentions the JFileChooser class for displaying file dialogs in GUI applications.

Uploaded by

OLI
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/ 2

ARBAMINCH UNIVERSITY DEP’T OF SOFTWARE ENG

JAVA FILE

JAVA FILE I/O


An I/O Stream is a sequence of data does not require encoding and
that is read from a source and write decoding.
to a destination  The Scanner class uses for
reading text data from a file
 Java File class represents the
and the Print Writer class uses
files and directory path names
for writing text data to a file.
in an abstract manner. The File
class has different methods
which can be used to • Scanner class is:
manipulate the files.
Scanner input = new Scanner
Absolute path: An absolute
(new File(filename));
file name (or full name)
contains a file name with its •
complete path and drive letter.
Relative path: A relative file File Dialogs

name is in relation to the •
• The JFileChooser class is a
current working directory. Scanner input = new Scanner(new
GUI component which is
File(filename));
• Print Writer class is: used to displaying a file
Print Writer output = new dialog.
Print Writer(filename  The JFileChooser class is found
 There are two types of files: in the java package
Text file or Binary file. javax.swing.JFileChooser.
1. Data stored in a Text file
are represented in
human-readable form.
2. Data stored in a binary
file are represented in
binary form. You cannot
read binary files.

 Binary files I/O are more


efficient to process than text
files I/O, because binary file I/O
ARBAMINCH UNIVERSITY DEP’T OF SOFTWARE ENG
JAVA FILE

JAVA FILE I/O

EXAMPLE Write file


Read file import java.io.File;
import java.io.IOException;
import java.io.File;
import public class CreateFile {
java.io.FileNotFoundException; public static void main(String[]
import java.util.Scanner; args) {
try {
public class ReadFile { File myObj = new
public static void main(String[] File("filename.txt");
args) { if (myObj.createNewFile()) {
try { System.out.println("File
File myObj = new created: " + myObj.getName());
File("filename.txt"); } else {
Scanner myReader = new System.out.println("File
Scanner(myObj); already exists.");
while }
(myReader.hasNextLine()) { } catch (IOException e) {
String data = System.out.println("An error
myReader.nextLine(); occurred.");
System.out.println(data); e.printStackTrace();
} }
myReader.close(); }
} catch (FileNotFoundException }
e) { The output is
System.out.println("An error
occurred.");
e.printStackTrace();
}
}
}
The output is

Group Members

1. Abenezer Mulugeta NSR/094/13

2. Hajira Siraj NSR/1190/13

3. Helina Solomon NSR/1255/13

You might also like