您的位置:首页 > 编程语言 > Java开发

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

2009-03-31 16:29 429 查看
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.
import java.io.*;
import java.util.*;

public class BufferedInputFile {
public static String read(String filename) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
LinkedList temp = new LinkedList();
LinkedList res = new LinkedList();
while ((s = in.readLine()) != null)
temp.add(s + "/n");
in.close();
ListIterator it =temp.listIterator(temp.size());
while(it.hasPrevious())//iterator中没有关于previous的内容,这些是listIterator独有
res.add(it.previous());
return res.toString();
}

public static void main(String[] args) throws IOException {
System.out.print(read(".//src//BufferedInputFile.java"));
}
}


Exercise 8: (1) Modify Exercise 7 so that the name of the file you read is provided as a command-line argument.
替换exercise7的main()即可
public static void main(String[] args) throws IOException {
for(int i=0;i<args.length;i++)
System.out.println(read(args[i]));
}


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.
import java.io.*;
import java.util.*;

public class BufferedInputFile {
public static String read(String filename,String regex) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
LinkedList temp = new LinkedList();
while ((s = in.readLine()) != null){
if(s.indexOf(regex)>-1)
temp.add(s + "/n");
}
in.close();
return temp.toString();
}

public static void main(String[] args) throws IOException {
System.out.println(read(args[0],args[1]));
}
}


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.
import java.io.*;
import java.util.*;

public class BufferedInputFile {
public static String read(String filename) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(filename));
String s;
LinkedList temp = new LinkedList();
LinkedList res = new LinkedList();
int i=1;
while ((s = in.readLine()) != null){
temp.add(i+" : "+s + "/n");
i++;
}
in.close();
return res.toString();
}

public static void main(String[] args) throws IOException {
System.out.print(read(".//src//BufferedInputFile.java"));
}
}


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.
import java.io.*;
public class BasicFileOutput {
static String file = "BasicFileOutput.out";
public static void main(String[] args)
throws IOException {
LineNumberReader in = new LineNumberReader(new StringReader(   //edited
BufferedInputFile.read(".//src//BasicFileOutput.java")));
PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(file)));

String s;
while((s = in.readLine()) != null )
out.println(in.getLineNumber()+":"+s);   //edited
out.close();
// Show the stored file:
System.out.println(BufferedInputFile.read(file));
}
}


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.
import java.io.*;
public class BasicFileOutput {

public static void main(String[] args)
throws IOException {
String s;
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("src//BasicFileOutput.java")));
long start=System.nanoTime();
Writer outWithoutBuffered = new FileWriter("BasicFileOutput1.out");

while((s=in.readLine())!=null)
outWithoutBuffered.write(s);
outWithoutBuffered.close();

long mid=System.nanoTime();
Writer out = new BufferedWriter(new FileWriter("BasicFileOutput.out"));

while((s=in.readLine())!=null)
out.write(s);
out.close();

long end=System.nanoTime();

System.out.println(mid-start);
System.out.println(end-mid);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: