Thinking in java 4th Edition 读书笔记-I/O(3)

1.input & output is based on decorator pattern

2.the InputStream and OutputStream classes still provide valuable functionality in the form of byte-oriented I/O, whereas the Reader and Writer classes

provide Unicode-compliant, character-based I/O.The most important reason for the Reader and Writer hierarchies is for internationalization. The old I/O

stream hierarchy supports only 8-bit byte streams and doesn’t handle the 16-bit Unicode characters well.So the most sensible approach to take is to try to

use the Reader and Writer classes whenever you can. You’ll discover the situations when you have to use the byte-oriented libraries because your code won’t

compile.

3.Whenever you want to use readLine( ), you shouldn’t do it with a DataInputStream (this is met with a deprecation message at compile time), but instead use

a BufferedReader.

4.RandomAccessFile is not part of the InputStream or OutputStream hierarchy. However, it has no association with those hierarchies other than that it happens

to implement the DataInput and DataOutput interfaces (which are also implemented by DataInputStream and DataOutputStream). It doesn’t even use any of the

functionality of the existing InputStream or OutputStream classes;The reason for this may be that RandomAccessFile has essentially different behavior than

the other I/O types, since you can move forward and backward within a file. In any event, it stands alone, as a direct descendant of Object.

5.public final class StringBuilder extends Object implements Serializable, CharSequence
一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的一个简易替换,用在字符串缓冲区被单个线程使用的时候(

这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数实现中,它比 StringBuffer 要快。将 StringBuilder 的实例用于多个线程是不安全的。如果需要这样的同步,

则建议使用 StringBuffer。

 

Exercise 7: (2) Open a text file so that you can read the file one line at a time. Read each line as a String and place that String object into a LinkedList.

Print all of the lines in the LinkedList in reverse order.

Exercise 8: (1) Modify Exercise 7 so that the name of the file you read is provided as a command-line argument.
替换exercise7的main()即可

 

Exercise 9: (1)Modify Exercise 8 to force all the lines in the LinkedList to uppercase and send the results to System.out.
替换exercise7第11行
temp.add(s.toUpperCase() + "/n");

 

Exercise 10: (2) Modify Exercise 8 to take additional command-line arguments of words to find in the file. Print all lines in which any of the words match.

 

6.DataInputStream, which is a byteoriented I/O class (rather than char-oriented). Thus you must use all InputStream classes rather than Reader classes.
7.Java SE5 added a helper constructor to PrintWriter so that you don’t have to do all the decoration by hand every time you want to create a text file and

write to it.You still get buffering, you just don’t have to do it yourself.

 

Exercise 12: (3) Modify Exercise 8 to also open a text file so you can write text into it. Write the lines in the LinkedList, along with line numbers (do not

attempt to use the "LineNumber" classes), out to the file.

 

Exercise 13: (3) Modify BasicFileOutput.java so that it uses LineNumberReader to keep track of the line count. Note that it’s much easier to just keep track

programmatically.

 

Exercise 14: (2) Starting with BasicFileOutput.java, write a program that compares the performance of writing to a file when using buffered and unbuffered I/O.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值