Strings , I/O and formatting (Part-4 StringBuffer and StringBuilder,File I/O)


In the previous post we have discussed about some basic facts about StringBuffer and StringBuilder classes.At the end we have instantiated a StringBuffer object,applied some changes and logged the output and saw the differences between String and StringBuffer classes. Same rules apply to the StringBuilder class.
StringBuilder a=new StringBuilder("hasitha");
a.append(" is a good boy");
System.out.println(a);
Prints out "Hasitha is a good boy".Now you are completely aware about how StringBuffer and StringBuilder classes behave when handling strings.Let's study some important methods in these classes.You have already have an idea about the append method.Note that append is a synchronized method for StringBuffer class.As in the previous post I am going to provide you the link for all the methods in StringBuffer and StringBuilder class.(Self study is the best....!!!!)

StringBuffer
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
StringBuilder
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

Now you know about Java String,StringBuffer and StringBuilder classes. Let's move on and study about File I/O.We can generalize the idea and discuss about the techniques provided in Java for handle large data streams. Following you get some important classes which can be used to process file I/Os.
  • File 
  • FileReader
  • BufferReader
  • FileWriter
  • BufferedWriter
  • PrintWriter
  • Console
 File-The API says that the class File is "An abstract representation of file
and directory pathnames." The File class isn't used to actually read or write
data; it's used to work at a higher level, making new empty files, searching for
files, deleting files, making directories, and working with paths.

FileReader-This class is used to read character files. Its read() methods are
fairly low-level, allowing you to read single characters, the whole stream of
characters, or a fixed number of characters. FileReaders are usually wrapped
by higher-level objects such as BufferedReaders, which improve performance
and provide more convenient ways to work with the data.

BufferReader-This class is used to make lower-level Reader classes like
FileReader more efficient and easier to use. Compared to FileReaders,
BufferedReaders read relatively large chunks of data from a file at once, and
keep this data in a buffer. When you ask for the next character or line of data,
it is retrieved from the buffer, which minimizes the number of times that
time-intensive, file read operations are performed.

FileWriter-This class is used to write to character files. Its write()
methods allow you to write character(s) or Strings to a file. FileWriters are
usually wrapped by higher-level Writer objects such as BufferedWriters or
PrintWriters, which provide better performance and higher-level, more
flexible methods to write data.

BufferedWriter-This class is used to make lower-level classes like
FileWriters more efficient and easier to use. Compared to FileWriters,
BufferedWriters write relatively large chunks of data to a file at once,
minimizing the number of times that slow, file writing operations are
performed. The BufferedWriter class also provides a newLine()
method to create platform-specific line separators automatically.

PrintWriter-This class has been enhanced significantly in Java 5. Because
of newly created methods and constructors (like building a PrintWriter with
a File or a String), you might find that you can use PrintWriter in places
where you previously needed a Writer to be wrapped with a FileWriter and/or
a BufferedWriter. New methods like format(), printf(), and append()
make PrintWriters very flexible and powerful.

Console-This new, Java 6 convenience class provides methods to read input
from the console and write formatted output to the console.

Comments