第9章字符串和文本I/OLiang,Introduction toJava Programming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 1 第9章 字符串和文本I/O
动因我们经常会遇到涉及字符串处理和文件输入/输出的问题。假设你需要编写一个程序,该程序用一个新字替换文件中所有出现的某个字。你该如何实现这个功能呢?本章介绍字符串和文本文件,它们可以解决此类问题Liang.Introduction to JavaProgramming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 2 动 因 我们经常会遇到涉及字符串处理和文件输入/输 出的问题。假设你需要编写一个程序,该程序 用一个新字替换文件中所有出现的某个字。你 该如何实现这个功能呢?本章介绍字符串和文 本文件,它们可以解决此类问题
学习目标(第9.2节)使用String类处理定长的字符串使用Character类处理单个字符(第9.3节)。使用StringBuilder/StringBuffer类处理可变长字符串(第9.4节)。区别String、StringBuilder和StringBuffer类(第9.2-9.4节)。学习如何从命令行传参数给main方法(第9.5节)使用File类获取文件的属性、删除和重命名文件(第9.6节)。使用PrintWriter类向文件写数据(第9.7.1节)(第9.7.2节)使用Scanner类从文件读取数据(GUI)使用对话框打开文件(第9.8节)Liang,Introduction to Java Programming,Eighth Edition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 3 学习目标 使用String类处理定长的字符串(第9.2节)。 使用Character类处理单个字符(第9.3节)。 使用StringBuilder/StringBuffer类处理可变长字符串 (第9.4节)。 区别String、StringBuilder和StringBuffer类 (第9.2- 9.4节)。 学习如何从命令行传参数给main方法(第9.5节)。 使用File类获取文件的属性、删除和重命名文件 (第9.6节)。 使用PrintWriter类向文件写数据(第9.7.1节)。 使用Scanner类从文件读取数据(第9.7.2节)。 (GUI)使用对话框打开文件(第9.8节)
字符串类string创建一个字符串对象:String message = "welcome to Java";String message = new String("Welcome to Java");-string s=new String();获取字符串长度和在一个字符串中获取某个单个字符。字符串连接(concat)子串(substring(index)、substring(start, end))比较(equals、compareTo)字符串转换在一个字符串中找出一个字符或一个子串字符串和数组之间的转换将字符和数值转换成字符串Liang,Introduction toJava Programming.EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 4 字符串类String 创建一个字符串对象: – String message = "Welcome to Java“; – String message = new String("Welcome to Java“); – String s = new String(); 获取字符串长度和在一个字符串中获取某个单个字符。 字符串连接(concat) 子串(substring(index)、substring(start, end)) 比较(equals、compareTo) 字符串转换 在一个字符串中找出一个字符或一个子串 字符串和数组之间的转换 将字符和数值转换成字符串
创建字符串对象String newString = new String(stringLiteral))String message = new String("Welcome to Java")因为字符串使用非常频繁,所以Java提供一种简化的方法来初始化一个字符串对象:String message = "Welcome to Java":Liang,Introduction to JavaProgramming,EighthEdition,(c)2011Pearson Education,Inc.Allrightsreserved.0132130807
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved. 0132130807 5 创建字符串对象 String newString = new String(stringLiteral); String message = new String("Welcome to Java"); 因为字符串使用非常频繁,所以Java提供一种简 化的方法来初始化一个字符串对象: String message = "Welcome to Java";