`

Java基础恶补——Strings, I/O, Formatting, and Parsing

    博客分类:
  • Java
io 
阅读更多

[SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065)]  chapter6

 

一. String, StringBuffer, StringBuilder
1. String 对象是不变的,String引用变量则不是。
2. 如果创建了1个String对象,但没有引用它,则它就被丢失了。
3. 如果将1个String引用变量重新分配到另一个String对象,则旧的String对象将丢失。
4. String类的方法中所使用的索引是从0开始的,除了substring()的第2个参数。
5. String类是final的,它的方法不能被overridden.

6. 当JVM找到1个String literal, 它将被加入String literal pool.

7. length() 是String的方法,length是Array的属性。

8. StringBuilder 的API和 StringBuffer 是一样的,唯一不同的是:StringBuilder不是线程安全的。

9. StringBuilder 的方法运行速度比 StringBuffer 快。

10. StringBuilder, StringBuffer 均适用的规则:

1) 它们都是可变的,不需要重新创建1个新的对象就可以被改变。

2) StringBuffer methods act on the invoking object, and objects can change
without an explicit assignment in the statement.

3) StringBuffer equals() is not overridden; it doesn't compare values.

11. Remember that chained methods are evaluated from left to right.

12. String methods to remember: charAt(), concat(), equalsIgnoreCase(), length(), replace(), substring(), toLowerCase(), toString(), toUpperCase(), and trim().

13. StringBuffer methods to remember: append(), delete(), insert(), reverse(), and toString().

 

二. File I/O
1. The classes you need to understand in java.io are File, FileReader, BufferedReader, FileWriter, BufferedWriter, PrintWriter, and Console.

2. new File object 不意味着硬盘上有一个新的文件。

3. File 对象可以表示文件或文件夹, File 类可以操作文件或文件夹。

4. The methods createNewFile() and mkdir() add entries to your file system.

5. FileWriter and FileReader are low-level I/O classes. You can use them to write and read files, but they should usually be wrapped.

6. Classes in java.io are designed to be "chained" or "wrapped." (This is a common use of the decorator design pattern.)

7. It's very common to "wrap" a BufferedReader around a FileReader or a BufferedWriter around a FileWriter, to get access to higher-level (more convenient) methods.

8. PrintWriters can be used to wrap other Writers, but as of Java 5 they can be built directly from Files or Strings.

9. Java 5 PrintWriters have new append(), format(), and printf() methods.

10. Console objects can read non-echoed input and are instantiated using System.console().

 

三. 序列化
1. The classes you need to understand are all in the java.io package; they include: ObjectOutputStream and ObjectInputStream primarily, and FileOutputStream and FileInputStream because you will use them to create the low-level streams that the ObjectXxxStream classes will use.

2. 一个类必须实现 Serializable 才能被序列化。

3. ObjectOutputStream.writeObject() 用于序列化对象,ObjectInputStream.readObject() 用于反序列化对象。

4. 将一个实例变量标记为 transient, 可以阻止其被序列化。

5. You can supplement a class's automatic serialization process by implementing the writeObject() and readObject() methods. If you do this, embedding calls to defaultWriteObject() and defaultReadObject(), respectively, will handle the part of serialization that happens normally.

6. If a superclass implements Serializable, then its subclasses do automatically.

7. If a superclass doesn't implement Serializable, then when a subclass object is deserialized, the superclass constructor will be invoked, along with its superconstructor(s).

 

四. Dates, Numbers, Currency
1. The classes you need to understand are java.util.Date, java.util.Calendar,
java.text.DateFormat, java.text.NumberFormat, and java.util.Locale.

2. Most of the Date class's methods have been deprecated.

3. A Date is stored as a long, the number of milliseconds since January 1, 1970.

4. Date objects are go-betweens the Calendar and Locale classes.

5. Calendar 提供了许多关于日期的有用的方法,如获得星期、对某个日期增加几个月或几年等。

6. 使用静态方法(getInstance())创建Calendar 实例。

7. The Calendar methods you should understand are add(), which allows you to add or subtract various pieces (minutes, days, years, and so on) of dates, and roll(), which works like add() but doesn't increment a date's bigger pieces. (For example: adding 10 months to an October date changes the month to August, but doesn't increment the Calendar's year value.)

8. 使用静态方法(getInstance() 和 getDateInstance())创建DateFormat 实例。

9. DateFormat 类中有几种格式化样式。

10. DateFormat styles can be applied against various Locales to create a wide array of outputs for any given date.

11. DateFormat.format() 方法用于创建特定格式的日期String.

12. The Locale class is used in conjunction with DateFormat and NumberFormat.

13. Both DateFormat and NumberFormat objects can be constructed with a specific, immutable Locale.

14. For the exam you should understand creating Locales using language, or a combination of language and country.

 

五. Parsing, Tokenizing, Formatting
1. regex is short for regular expressions, which are the patterns used to search for data within large data sources.

2. regex is a sub-language that exists in Java and other languages (such as Perl).

3. regex lets you to create search patterns using literal characters or metacharacters. Metacharacters allow you to search for slightly more abstract data like "digits" or "whitespace".

4. Study the \d, \s, \w, and . metacharacters.

5. regex provides for quantifiers which allow you to specify concepts like: "look for one or more digits in a row."

6. Study the ?, *, and + greedy quantifiers.

7. Remember that metacharacters and Strings don't mix well unless you remember to "escape" them properly. For instance String s = "\\d";

8. The Pattern and Matcher classes have Java's most powerful regex capabilities.

9. You should understand the Pattern compile() method and the Matcher matches(), pattern(), find(), start(), and group() methods.

10. You can use java.util.Scanner to do simple regex searches, but it is primarily intended for tokenizing.

11. Tokenizing is the process of splitting delimited data into small pieces.

12. In tokenizing, the data you want is called tokens, and the strings that separate the tokens are called delimiters.

13. Tokenizing can be done with the Scanner class, or with String.split().

14. Delimiters are single characters like commas, or complex regex expressions.

15. Scanner 类在一个循环中 tokenize data ,你可以随时退出循环。

16. The Scanner class allows you to tokenize Strings or streams or files.

17. The String.split() method tokenizes the entire source data all at once, so large amounts of data can be quite slow to process.

18. New to Java 5 are two methods used to format data for output. These methods are format() and printf(). These methods are found in the PrintStream class, an instance of which is the out in System.out.

19. format() 和 printf() 具有相同的功能。

20. Formatting data with printf() (or format()) is accomplished using formatting strings that are associated with primitive or string arguments.

21. The format() method allows you to mix literals in with your format strings.

22. The format string values you should know are:

1) Flags: -, +, 0, "," , and (

2) Conversions: b, c, d, f, and s

23. If your conversion character doesn't match your argument type, an exception will be thrown.

 

 

关于IO的好文:

说说IO(一)- IO的分层

说说IO(二)- IO模型

说说IO(三)- IO性能的重要指标

说说IO(四)- 文件系统

说说IO(五)- 逻辑卷管理

说说IO(六)- Driver & IO Channel

说说IO(七)- RAID

说说IO(八)- 三分天下

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics