`

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(八)- 三分天下

 

 

分享到:
评论

相关推荐

    IOS应用源码——Strings.rar

    在iOS应用开发中,Strings文件是用于存储应用程序中使用的文本字符串的重要组成部分。这些字符串通常包括用户界面中的按钮标签、提示信息、错误消息等。Strings文件采用特定格式编写,便于多语言支持,允许开发者...

    OCA OCP Java SE 7 Programmer I - II Study Guide(SYBEX,2014)

    * I/O and NIO * Advanced OO and design patterns * Generics and collections * Inner classes * Threads * Concurrency * Java Database Connectivity (JDBC) Electronic content includes: * 500+ practice ...

    JavaScript基础语法-strings.pdf

    JavaScript基础语法——strings

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    New I/O 65 Regular Expressions 75 Chapter 2: XML 87 Introducing XML 88 Parsing an XML Document 93 Validating XML Documents 105 Locating Information with XPath 129 Using Namespaces 136 ...

    java7帮助文档

    The java.nio.file package and its related package, java.nio.file.attribute, provide comprehensive support for file I/O and for accessing the file system; see File I/O (featuring NIO.2). NIO stands for...

    android P 9.0添加Ethernet功能(settings+framework).zip

    res\xml\network_and_internet.xml res\drawable\ic_ethernet_cell.xml res\values\strings.xml res\xml\ethernet_settings.xml res\xml\ethernet_static_ip.xml src\...

    Agile Java Crafting Code with Test-Driven Development

    Contains detailed chapters on exceptions and logging, math, I/O, reflection, multithreading, and Swing Offers seamlessly-integrated explanations of Java 5.0's key innovations, from generics to ...

    090830 Java面试题——基础篇60题

    ### Java面试题——基础篇60题解析 #### 1. 作用域 public,private,protected 以及不写时的区别 在Java中,类成员(如变量、方法等)的作用域可以通过不同的修饰符来控制。具体如下: - **public**:公开的,...

    JAVA API文档 1.7 官方中文版.rar_API_JAVA的API中文版_java 1.7开发_java api 1.7

    `java.nio`是新I/O包,提供了一种更高效、更面向缓冲区的I/O模型。`java.net`包则用于网络编程,包括套接字(Socket)和服务器套接字(ServerSocket)等。 在Java 1.7中,引入了一些重要的新特性: 1. **try-with-...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    Strings, I/O, Formatting, and Parsing Working with strings and input/output streams is crucial in Java development: - **Strings**: Manipulating string objects, concatenation, searching, and ...

    android 8.1添加Ethernet功能(settings+framework).zip

    res\xml\network_and_internet.xml res\drawable\ic_ethernet_cell.xml res\values\strings.xml res\xml\ethernet_settings.xml res\xml\ethernet_static_ip.xml src\...

    JAVA_API_1.7中文(最新、最全).chm.7z

    1. **基础类库**:Java的基础类库提供了大量的常用功能,如集合框架(List, Set, Map等),I/O流,字符串处理,日期时间操作,数学计算等。这些类库为编写日常应用程序提供了便利。 2. **多线程**:Java 7中的`...

    JAVA_API_1.7中文版

    4. **NIO.2**:Java 1.7扩展了非阻塞I/O(New I/O)框架,引入了文件系统感知、异步I/O操作以及更好的路径和文件名处理。例如,`java.nio.file`包提供了`Files`和`Paths`类,用于执行文件系统操作。 5. **钻石操作...

    java API 1.7

    1. **多路复用I/O (NIO.2)**: Java 7引入了NIO.2,也称为New I/O 2.0,它扩展了Java的非阻塞I/O功能。NIO.2提供了文件通道、异步I/O操作、文件系统查询以及更好的文件事件监听等特性,使开发者能够编写更高效、更...

    java-leetcode题解之Multiply Strings.java

    java java_leetcode题解之Multiply Strings.java

    Algorithms on Strings, Trees and Sequences 高清英文版

    In recent years their importance has grown dramatically with the huge increase of electronically stored text and of molecular sequence data (DNA or protein sequences) produced by various genome ...

    Google中的Guava源码

    Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...

    c / c++ / cpp / stl 中文帮助文档手册chm格式下载

    Standard C I/O Standard C String & Character Standard C Math Standard C Time & Date Standard C Memory Other standard C functions C++ C++ I/O C++ Strings C++ 标准模板库 C++ Bitsets ...

Global site tag (gtag.js) - Google Analytics