java基础复习07
java基础复习
面向对象
5.1、属性集
-
Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载。
-
Properties集合是一个唯一和I0流相结合的集合
- 可以使用Properties集含中的方法store ,把集含中的临时数据,持久化写入到硬盘中存储
- 可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用
属性列表中每个键及其对应值都是一个字符串。Properties集合是一个双列集合,key和value默认都是字符串
Properties集合有一些操作字符串的特有方法:
- object setProperty(String key,String value)调用Hashtable 的方法 put。
- String getProperty ( String key)通过Key找到value值,此方法相当于Map集合中的get(key)方法
- Set< String> stringPropertyNames()返回此属性列表中的键集,其中该键及其对应值是字符串,此方法相当于Map集合中的keyset方法
private static void show() {
Properties prop = new Properties();
prop.setProperty("马尔咋哈","196");
prop.setProperty("古力娜扎","166");
prop.setProperty("扎克","200");
//使用stringPropertyNames把Properties集合中的键取出,存储到一个set集合中
Set<String> set = prop.stringPropertyNames();
//遍历set集合,取出Properties集合的每一个键
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
}
可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
- void store(outpurtStream out, string comments)
- void store (writer writer, String comments)
参数:
- OutputStream out:字节输出流,不能写入中文
- writer writer:字符输出流,可以写中文
- String comments :注释,用来解释说明保存的文件是做什么用的
- 不能使用中文,会产生乱码,默认是unicode编码
一般使用"空字行串
- 不能使用中文,会产生乱码,默认是unicode编码
使用步骤:
1 、创建Properties集合对象,添加数据
2、创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
3、使用Properties集合中的方法store ,把集合中的临时数据,持久化写入到硬盘中存储
4、释放资源
private static void show() throws IOException {
Properties prop = new Properties();
prop.setProperty("马尔咋哈","196");
prop.setProperty("古力娜扎","166");
prop.setProperty("扎克","200");
//创建字节输出流/字符输出流对象,构造方法中绑定要输出的目的地
FileWriter fw = new FileWriter("F:\\练习\\8.txt");
//使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中存储
prop.store(fw,"save data");
fw.close();
}
}
可以使用Properties集合中的方法Load,把硬盘中保存的文件(键值对),读取到集合中使用
- void Lood(InputStream instreanm)
- void lood( Reader reoder )
参数:
- InputStream inStrecm:字节输入流,不能读取含有中文的键值对
- Recder reader:字符输入流,能读取含有中文的键值对
使用步骤;
1、创建Properties集合对象
2、使用Properties集合对象中的方法Load读取保存键值对的文件
3、遍历Properties集合
注意:
1、存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)
2、存储键值对的文件中,可以使用#进行注释,被注释的键值对不会再被读取
3、存储键值对的文件中,键与值默认都是字符串,不用再加引号
private static void show() throws IOException {
Properties prop = new Properties();
prop.load(new FileReader("F:\\练习\\8.txt"));
Set<String> set = prop.stringPropertyNames();
for (String key : set) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}
}
5.2、缓冲流
字节缓冲输出流构造方法:
-
BufferedOutputStream(OutputStreom out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。
-
BufferedOutputstreom(OutputStreom out,int size)创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层
参数:
- OutputStream out:字节输出流
- 我们可以传递FiLeOutputStream,缓冲流会给FiLeOutputStream增加一个缓冲区,提高FiLeOutputStream的写入效率
- int size:指定缓中流内部缓冲区的大小,不指定默认
使用步骤(重点):
1、创建FileOutputstream对象,构造方法中绑定要输出的目的地
2、创建BufferedoutputStream对象,构造方法中传递FiLeOutputStreom对象,提高FiLeOutputStreom对象效率
3、使用BufferedoutputStream对象中的方法write,把数据写入到内部缓冲区中
4、使用Bufferedoutputstream对象中的方法flush,把内部缓冲区中的数据,刷新到文件中
5、释放资源(会先调用flush方法刷新数据,第4步可以省略)
public class DemoBufferedOutPutStream {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("F:\\练习\\9.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("我把数据写到数据缓冲区".getBytes());
bos.flush();
bos.close();
}
}
字节缓冲输入流构造方法:
-
BufferedInputstream(InputStream in)创建一个BufferedInputstream并保存其参数,即除入流 in,以便将来使用。
-
Bufferedinputstream(Inputstream in, int size)创建具有指定缓冲区大小的BufferedInputStream并保存其参数,
参数:
- inputStreom in :字节输入流
- 我们可以传递FileInputStream,缓冲流会给FiLeInputStreon增加一个缓冲区,提高FiLeinputStream的读取效率
- int size:指定缓冲流内部缓冲区的大小,不指定默认
使用步骤(重点)∶
1、创建FileInputStream对象,构造方法中绑定要读取的数据源
2、创建BufferedInputStream对象,构造方法中传递FiLeInputStream对象,提高FileInputStreom对象的读取效率
3、使用BufferedInputStream对象中的方法read ,读取文件
4、释放资源
public class DemoBufferedInputStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("F:\\练习\\9.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bytes = new byte[1024];//从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中。
int len = 0;//记录每次读取的有效字节个数
while ((len=bis.read(bytes))!=-1){
System.out.println(new String(bytes,0,len));
}
bis.close();
}
}
缓冲流的文件复制
文件复制的步骤:
1、创建字节缓冲输入流对象,构造方法中传递字节输入流
2、创建字节缓冲输出流对象,构造方法中传递字节输出流
3、使用字节缓冲输入流对象中的方法read,读取文件
4、使用字节缓冲输出流中的方法write,把读取的数据写入到内部缓冲区中
5 、释放资源(会先把缓冲区中的数据,刷新到文件中)
public class DemoCopyFile {
public static void main(String[] args) throws IOException {
long s = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\练习\\1.png"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\练习\\1.png"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len= bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
long e = System.currentTimeMillis();
System.out.println("文件复制共耗时"+(e-s)+"毫秒");
}
}
字符缓冲输出流构造方法:
-
Bufferediwriter(writer out)创建一个使用默认大小输出缓冲区的缓冲字符输出流。
-
Bufferedwriter(writer out,int sz)创建一个使用给定大小输出缓冲区的新缓冲字符输出流。
参数:
- iriter out:字符输出流
- 我们可以传递FiLewriter ,缓冲流会给FiLewriter增加一个缓冲区,提高FiLewriter的写入效率
- int sz:指定缓冲区的大小,不写默认大小
特有的成员方法:
- void newline()写入一个行分隔符。会根据不同的操作系统,获取不同的行分隔符
使用步骤:
1、创建字符缓冲输出流对象,构造方法中传递字符输出流
2、调用字符缓冲输出流中的方法write,把数据写入到内存缓冲区中
3、调用字符缓冲输出流中的方法flush,把内存缓冲区中的数据,刷新到文件中
4、释放资源
public class DemoBufferedWriter {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\练习\\10.txt"));
for (int i = 0; i < 10; i++) {
bw.write("德莱厄斯");
bw.newLine();
}
bw.flush();
bw.close();
}
}
字符缓冲输入流构造方法:
- BufferedReader(Reader in)创建一个使用默认大小输入缓冲区的缓冲字符输入流。
- BufferedReader(Reader in, int sz)创建一个使用指定大小输入缓冲区的缓冲字符输入流。
参数:
- Reader in :字符输入流
- 我们可以传递FiLeReader ,缓冲流会给FiLeReader增加一个缓冲区,提高FileReader的读取效率
特有的成员方法:
- String readLine() 读取一个文本行。读取一行数据
- 行的终止符号:通过下列字符之一即可认为某行已终止:换行("\n '')、回车(''\r")或回车后直接跟着换行(\r\n)。
- 返回值:
- 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回null
使用步骤:
1、创建字符缓冲输入流对象,构造方法中传递字符输入流
2、使用字符缓中输入流对象中的方法read/readLine读取文本
3、释放资源
public class DemoBufferedReader {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("F:\\练习\\10.txt"));
String line;
while ((line = br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
文本排序
1、创建一个HashMap集合对象.,可以:存储每行文本的序号(1,2,3.. .) ;value;存储每行的文本
2、创建字符缓冲输入流对象,构造方法中绑定字符输入流
3、创建字符缓冲输出流对象,构造方法中绑定字符输出流
4、使用字符缓冲输入流中的方法readLine,逐行读取文本
5、对读取到的文本进行切割获取行中的序号和文本内容
6、把切割好的序号和文本的内容存储到HashMap集合中(key序号是有序的,会自动排序1,2,3,4..)
7、遍历HashMap集合,获取每一个键值对
8、把每一个键值对,拼接为一个文本行
9、把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
10、释放资源
public class DemoTest {
public static void main(String[] args) throws IOException {
HashMap<String, String> Map = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("F:\\练习\\11.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\练习\\1.txt"));
String line;
while ((line= br.readLine())!=null){
String[] arr = line.split("\\、");
Map.put(arr[0],arr[1]);
}
for (String key:Map.keySet()){
String value = Map.get(key);
line = key + "、" + value;
bw.write(line);
bw.newLine();
}
bw.close();
br.close();
}
}
5.3、转换流
字符编码character Encoding:就是一套自然语言的字符与二进制数之间的对应规则。
字符集charset:也叫编码表。是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等。
构造方法:
-
OutputStreamWriter(OutputStream out)创建使用默认字符编码的outputStreamwriter。
-
OutputStreamWriter(OutputStream out,String charsetName)创建使用指定字符集的 OutputStreamwriter。
参数:
-
OutputStream out:字节输出流,可以用来写转换之后的字节到文件中
-
String charsetName:指定的编码表名称,不区分大小写,可以是utf-8/UTF-8,gbk/GBK, ...不指定默认使用UTF-8
使用步骤:
1、创建outputStreamwriter对象,构造方法中传递字节输出流和指定的编码表名称
2、使用outputstreamwriter对象中的方法write,把字符转换为字节存储缓冲区中(编码)
3、使用outputStreamwriter对象中的方法flush,把内存缓冲区中的字节刷新到文件中(使用字节流写字节的过程)
4、释放资源
public class DemoOutputStreamWriter {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F:\\练习\\12.txt"),"utf-8");
osw.write("你好");
osw.flush();
osw.close();
}
}
使用步骤:
1、创建InputstreamReader对象.,构造方法中传递字节输入流和指定的编码表名称
2、使用InputStreamReader对象中的方法read读取文件
3、释放资源
public class DemoInputStreamReader {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("F:\\练习\\12.txt"), "UTF-8");
int len = 0;
while ((len= isr.read())!=-1){
System.out.println((char) len);
}
isr.close();
}
}
5.4、序列化
把对象以流的方式写入到文件中保存,叫写对象,也叫对象的序列化
把文件中保存的对象,以流的方式读取出来叫做读对象.也叫对象的反序列化
ObjectOutputStream类
作用:把对象以流的方式写入到文件中保存
特有的成员方法:
- void write0bject(0bject obj)将指定的对象写入0bjectoutputstream
使用步骤:
1、创建objectoutputstream对象,构造方法中传递字节输出流
2、使用objectoutputStream对象中的方法writeobject,把对象写入到文件中
3、释放资源
- 要进行序列化和反序列化的类必须实现Serializable接口,就会给类添加一个标记
public class Person implements Serializable {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class DemoObjectOutputStream {
public static void main(String[] args) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\练习\\person.txt"));
oos.writeObject(new Person("小美女",18));
oos.close();
}
}
0bjectInputStream类
反序列化的前提:
1、类必须实现Serializable
2、必须存在类对应的Class文件
特有的成员方法:
- Object readObject() 从objectInputstream读取对象。(方法抛出了ClassNotFoundException()异常)
使用步骤:
1、创建ObjectInputStream对象,构造方法中传递字节输入流
2、使用ObjectInputStream对象中的方法readobject读取保存对象的文件
3、释放资源
4、使用读取出来的对象(打印)
public class DemoObjectInputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\练习\\person.txt"));
Object o = ois.readObject();
ois.close();
System.out.println(o);
}
}
transient关键字
static关键字:静态关键字
- 静态优先于非静态加载到内存中(静态优先于对象进入到内存中)
被static修饰的成员变量不能被序列化的,序列化的都是对象
transient关键字:瞬态关键字
- 被transient修饰成员变量,不能被序列化
可序列化声明
private static final long serialVersionUID = 1L;
通过声明,防止类变化后的序列化和反序列化出现问题
序列化集合
分析:
1、定义一个存储Person对象的ArrayList集合
2、往Arraylist集合中存储Person对象
3、创建一个序列化流ObjectoutputStream对象
4、使用ObjectoutputStream对象中的方法writeObject,对集合进行序列化
5、创建一个反序列化ObjectInputStream对象
6、使用ObjectInputStream对象中的方法readObject读取文件中保存的集合
7、把object类型的集合转换为ArrayList类型
8、遍历ArrayList集合
9、释放资源
public class DemoTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("张三",19));
list.add(new Person("李四",29));
list.add(new Person("王五",25));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\练习\\list.txt"));
oos.writeObject(list);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\练习\\list.txt"));
Object o = ois.readObject();
ArrayList<Person> list2 = (ArrayList<Person>)o;
for (Person p:list2){
System.out.println(p);
}
ois.close();
oos.close();
}
}
5.5、打印流
PrintStseam特点:
1、只负责数据的输出,不负责数据的读取
2、与其他输出流不同,PrintStream永远不会抛出IOException
3、有特有的方法, print , println
- void print(任意类型的值)
void println(任意类型的值并换行)
构造方法:
- PrintStream( File file):输出的目的地是一个文件
- PrintStream( outputstream out):输出的目的地是一个字节输出流
- PrintStream(String fiLeName):输出的目的地是一个文件路径
注意:
- 如果使用继承自父类的write方法写数据,那么查看数据的时候会查询编码表97->a
如果使用自己特有的方法print/println方法写数据,写的数据原祥输出97->97
public class DemoPrintStream {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream("F:\\练习\\print.txt");
ps.write(97);//a
ps.print(97);
ps.print("HelloWorld");
ps.print(3.2);
ps.close();
}
}
可以改变输出语句的目的地(打印流的流向)
输出语句,默认在控制台输出
使用System.setOut方法改变输出语句的目的地改为参数中传递的打印流的目的地
- static void setOut ( Printstreae out)
重新分配“标准"输出流。
public class Demo01PrintStream {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("我在控制台输出");
PrintStream ps = new PrintStream("F:\\练习\\目的地是打印流.txt");
System.setOut(ps);
System.out.println("我在打印流的目的地输出");
ps.close();
}
}

浙公网安备 33010602011771号