When you open a file with the printwriter class the class can potentially throw an ioexception
The basics: InputStream and OutputStreamThe basic I/O types in Java are
Show
Since
Instead, you
create an instance of one of the concrete subtypes of
There’s also
The last line is important: you should always close an
Working with text: Reader and WriterThe above example shows a really painful way to create a “Hello, world” file. You might wonder why I didn’t write
Go try it, and see what happens. To understand the error message, understand that As long as we only use ASCII text so far (so: English letters, basic punctuation, no accents), we can use one byte per character (in fact, ASCII strings only need 7 bits per character, not the full 8 bits per byte). But there are many other characters: accented letters (common in European languages), ideographs (such as in Japanese or Chinese text), more advanced punctuation (like “smart” quotes or the ellipsis: ‘…’), emoji (like ☺), symbols (like ∫ or ♩), and so on. Diversion: UnicodeClick here to skip the diversion on Unicode and character encodings. The modern way of handling character encoding is Unicode. According to Wikipedia, there are currently about 137 thousand Unicode characters; the Unicode standard can potentially encode about 1 million characters. Java uses an
encoding called UTF-16, which encodes the 137 thousand Unicode characters into two-byte chars. Obviously, it’s not possible to represent 137 thousand characters using only two bytes, so some Unicode characters require two chars. For example, the symbol “ِ𐀀” (Unicode character U+10000) requires to chars: All things considered, Unicode is very nice. Before Unicode, there were many different character encodings in use. Many of them started with ASCII (which defines characters 0 through 127) and added up to 128 additional characters. For example, the encoding ISO-8859-1 added a number of accented Latin characters (e.g. á) and a few extra letters (like the German ß), and this encoding worked pretty well for European languages. But it wasn’t the only encoding in use in Europe, and naturally (for example) Japanese text was written in a different encoding. The result was that if you had a text file, you had to know (or guess) the right encoding to be able to read it. Nowadays when most things are Unicode (usually encoded as UTF-8, which is backwards-compatible with ASCII), you don’t have to worry nearly as much about knowing which encoding is in use. Back to Reader and Writer
When to use InputStream versus ReaderThe decision criterion for Wrappers: BufferedReaders, PrintWriters, et al.We’ve
actually been using a special
Another common type of stream wrapper is the family of
If you want to write to a file instead of to
By the way,
a nice bonus of using ExceptionsMost of the IO methods discussed above can
throw exceptions (specifically, they can throw Generally, When you pass the name of a file to the PrintWriter constructor and the file already?When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.
Which of the following is the method you can use to determine whether a file exist?exists() method. The idea is to use the File. exists() method to determine whether a file denoted by a specified pathname exists. This method returns true if the file exists; false otherwise.
When a method's type is an object what is actually returned by the calling program?When a method's return type is an object, what is actually returned to the calling program? only one copy of the field in memory.
When the break statement is encountered in a loop all the statements in the body of the loop that appear after it are ignored and the loop prepares for the next iteration?When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration. You can use the PrintWriter class to open a file for writing and write data to it. The do-while loop must be terminated with a semicolon.
|