Java Reference
In-Depth Information
61.
62.
//
writes 40 characters from the buffer
63.
//
starting at position 7 of the buffer
64.
65.
fw.write(buffer,7,40);
66.
67.
//
close file
68.
fw.close();
69.
70.
}//
if
71.
else
72.
{
73.
System.out.println("File not created "+writefile.getName());
74.
}
75.
}
76.
catch
(Exception ex)
77.
{
78.
System.out.println("Problem opening file "+writefile.getName());
79.
}
80.
}
81.
82.
public static void
main(String[] args)
83.
{
84.
FileReadWrite RWF =
new
FileReadWrite("testtext1.txt","testtext2.txt");
85.
}
86.
}
10.1.4
Reading text files line by line
Sometimes one would like to access a text file line by line. To do this another
reader is used, a
BufferedReader
.Welist the constructor and some methods:
BufferedReader(FilerReader fReader)
close()
String readLine()
BufferedReader(FilerReader fReader)
constructs a buffered reader. A file
reader is given as argument.
close()
stops the buffered reader and closes the file.
readLine()
returns a single line of text or null if the end of the stream has been
reached. The line-end characters are not returned.
The following program
LineRead
reads a file line-wise. The while loop is terminated
when the string read is
null
, i.e., when the file end is reached.
Search WWH ::
Custom Search