Gor510
Eccentric Innovator
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
400 XP
Introduction:
This tutorial demonstrate the method of reading from and writing to a text file in Java. Java has two type of files: Binary and Text. Text file is processed as a sequence of characters where as Binary file is processed a sequence of bytes. This application has 4 JButtons for reading, writing, exiting, and clearing the Display Area. Also It has a display area where we can type the data to be written to text and in the same area content from file are displayed.
Step 1: Creating GUI Components
Create the four buttons, label, text area and text field. Text field is used to take file name from user.
Step 2: Initializing Constructor
In the constructor of class, we add the created components to the frame and add ActionListeners to it. A panel is created with Grid Layout having 2 rows and 3 columns and 1 pixel gap between the components. All the buttons and text field is added to this panel. This panel is added to the North of the main frame. Display area occupies the center area of the frame with scroll pane. Scroll Pane is added when the contents of the component(area) extends the component.
Step 3: Defining read/write functions
The main part is to read and write the data. For reading from a file we require the BufferedReader object which provides buffering for characters input stream(text files). This object takes FileReader object so as to read from a file. try {} catch block is used to handle if there is any error reading the file.
write function works similar except that it write the data of the complete text area at one time. FileWriter object is used to write the data to file. This object takes a file name, if file is already there it overrides the file content and if file is not there then new file is created. If the file can not be created then a message is displayed.
Step 4: Writing Action Listeners for the Buttons
When a user clicks on read, write, clear or exit button, appropriate action is performed. When read button is clicked readTextFile() function is called. Similary all other button's events are handled as follows.
Step 5: Writing main() function
From here, execution starts, object of the class is created and some properties of frame are set.
Download
This tutorial demonstrate the method of reading from and writing to a text file in Java. Java has two type of files: Binary and Text. Text file is processed as a sequence of characters where as Binary file is processed a sequence of bytes. This application has 4 JButtons for reading, writing, exiting, and clearing the Display Area. Also It has a display area where we can type the data to be written to text and in the same area content from file are displayed.
Step 1: Creating GUI Components
Create the four buttons, label, text area and text field. Text field is used to take file name from user.
- JTextArea
display=
new
JTextArea
(
)
;
- JButton
read=
new
JButton
(
"Read From File"
)
;
- JButton
write=
new
JButton
(
"Write To File"
)
;
- JButton
clear=
new
JButton
(
"Clear Display Area"
)
;
- JButton
exit=
new
JButton
(
"Exit"
)
;
- JTextField
name=
new
JTextField
(
20
)
;
- JLabel
prompt=
new
JLabel
(
"File Name : "
, JLabel
.RIGHT
)
;
- JPanel
commandPanel=
new
JPanel
(
)
;
//panel for holding above buttons
Step 2: Initializing Constructor
In the constructor of class, we add the created components to the frame and add ActionListeners to it. A panel is created with Grid Layout having 2 rows and 3 columns and 1 pixel gap between the components. All the buttons and text field is added to this panel. This panel is added to the North of the main frame. Display area occupies the center area of the frame with scroll pane. Scroll Pane is added when the contents of the component(area) extends the component.
- public
TextIO(
)
{
- super
(
"Text IO Demo"
)
;
//Title of the frame
- read.addActionListener
(
this
)
;
- write.addActionListener
(
this
)
;
- clear.addActionListener
(
this
)
;
- exit.addActionListener
(
this
)
;
- commandPanel.setLayout
(
new
GridLayout
(
2
,3
,1
,1
)
)
;
//grid with two rows and three columns
- commandPanel.add
(
prompt)
;
- commandPanel.add
(
name)
;
- commandPanel.add
(
clear)
;
- commandPanel.add
(
read)
;
- commandPanel.add
(
write)
;
- commandPanel.add
(
exit)
;
- display.setLineWrap
(
true
)
;
- this
.setLayout
(
new
BorderLayout
(
)
)
;
- this
.add
(
commandPanel, BorderLayout
.NORTH
)
;
- this
.add
(
new
JScrollPane
(
display)
)
;
- this
.add
(
display,BorderLayout
.CENTER
)
;
- }
Step 3: Defining read/write functions
The main part is to read and write the data. For reading from a file we require the BufferedReader object which provides buffering for characters input stream(text files). This object takes FileReader object so as to read from a file. try {} catch block is used to handle if there is any error reading the file.
- private
void
readTextFile(
JTextArea
display, String
fileName)
- {
- try
{
- BufferedReader
inStream=
new
BufferedReader
(
new
FileReader
(
fileName)
)
;
- String
line=
inStream.readLine
(
)
;
//read the next line every time.
- while
(
line!=
null
)
- {
- display.append
(
line +
"\n
"
)
;
- line=
inStream.readLine
(
)
;
- }
- inStream.close
(
)
;
- }
- catch
(
FileNotFoundException
e)
{
- display.setText
(
"IO Error: "
+
fileName +
" Not Found. "
+
"\n
"
)
;
- e.printStackTrace
(
)
;
- }
- catch
(
IOException
e)
{
- display.setText
(
"IO Error: "
+
e.getMessage
(
)
+
"\n
"
)
;
- e.printStackTrace
(
)
;
- }
- }
write function works similar except that it write the data of the complete text area at one time. FileWriter object is used to write the data to file. This object takes a file name, if file is already there it overrides the file content and if file is not there then new file is created. If the file can not be created then a message is displayed.
- try
{
- FileWriter
ostream=
new
FileWriter
(
fileName)
;
- ostream.write
(
display.getText
(
)
)
;
- JOptionPane
.showMessageDialog
(
null
,"Data written to file"
, "Successful Write Operation"
,JOptionPane
.INFORMATION_MESSAGE
)
;
- ostream.close
(
)
;
- }
- catch
(
IOException
e)
{
- display.setText
(
"IO Error: "
+
fileName +
" Not Found. "
+
"\n
"
)
;
- e.printStackTrace
(
)
;
- }
- }
Step 4: Writing Action Listeners for the Buttons
When a user clicks on read, write, clear or exit button, appropriate action is performed. When read button is clicked readTextFile() function is called. Similary all other button's events are handled as follows.
- String
fileName=
name.getText
(
)
;
//content of text area
- if
(
ae.getSource
(
)
==
read)
// get source of event
- {
- display.setText
(
""
)
;
- readTextFile(
display,fileName)
;
- }
- else
if
(
ae.getSource
(
)
==
write)
- writeTextFile(
display, fileName)
;
- else
if
(
ae.getSource
(
)
==
clear)
- display.setText
(
""
)
;
//clears the display area
- else
if
(
ae.getSource
(
)
==
exit)
- {
- System
.exit
(
0
)
;
- }
Step 5: Writing main() function
From here, execution starts, object of the class is created and some properties of frame are set.
Download
You must upgrade your account or reply in the thread to view the hidden content.