2019.8.8 NIIT第二十三天
字符流与字节流的转换
Reader:
- InputStreamReader可以完成字节输入流转换为字符输入流
FileInputStream fis=new FileInputStream("d:/test.txt");
//新建输入转换流
InputStreamReader isr=new InputStreamReader(fis);
//读入数据
int c=-1;
while((c=isr.read())!=-1) {
System.out.print((char)c);
}
Writer:
- OutputStreamWriter可以完成字节输出流转换为字符输出流
public static void main(String[] args) throws IOException {
//1、新建一个字节输出流关联到文件
FileOutputStream fos=new FileOutputStream("d:/test.txt");
//2、把字节流转换成字符流
OutputStreamWriter osw=new OutputStreamWriter(fos);
//3、输出
osw.write("hello 中国");
//4、把数据从内存中刷出并关闭
osw.close();
}
Charset字符集
A:乱码:
- 当字符与字节转换过程中使用了不同的码表,会造成乱码的情况。 B:在字符串中:
- 当我们将字符串转为对应的数字字节时,需要指定码表,则存储为该 字符该码表对应的数字字节,如果使用了其他码表重写翻译回字符串,则拼写的新字符串会乱码。 C:在IO中:
- 与字符串编码表使用类似,当以某个码表写出字节数据时,又使用另外 码表展示,会出现乱码。
package com.igeek1;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* @author jerryHe
* @create 2019-08-08 10:17
*/
public class TestCharSet {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
String s1 = "abc";
byte[] bs = s.getBytes("iso-8859-1");
System.out.println(Arrays.toString(bs));
String s2 = new String(bs,"iso-8859-1");
System.out.println(s2);
System.out.println("-------------------");
byte[] bs1 = s.getBytes("utf-8");
System.out.println(Arrays.toString(bs1));
String s3 = new String(bs1,"utf-8");
System.out.println(s3);
}
}
package com.igeek1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author jerryHe
* @create 2019-08-08 10:30
*/
public class TestCharSet1 {
public static void main(String[] args) throws IOException {
//不知道别人传给你的数据的字符集?
//经验:挨个字符集去尝试 ISO-8859-1 gb2312 gbk utf-8
FileInputStream fis = new FileInputStream("d:\\java82\\test2.txt");
//新建输入转换流
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
System.out.println(isr.getEncoding());
//读数据
int c = -1;
while((c=isr.read())!=-1){
System.out.print((char)c);
}
}
}
序列化与反序列化
- 概述
- 用于向流中写入对象的操作流 ObjectOutputStream称为序列化流
- 用于从流中读取对象的操作流 ObjectInputStream称为反序列化流
- 特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。
- ObjectOutputStream序列化流
-
public final void writeObject(Object obj) - ObjectInputStream反序列化流
-
public final Object readObject() - 每个被序列化操作的类型必须实现Serializable接口(具备序列化功能的标记接口),让其具备序列化能力。
package com.igeek2;
import java.io.*;
/**
* @author jerryHe
* @create 2019-08-08 11:24
*/
public class TestPerson {
public static void main(String[] args) throws IOException, ClassNotFoundException {
writeObject();
//读对象
//1.新建对象输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\java82\\object.txt"));
//2.读数据
//必须按照保存顺序去读
System.out.println(ois.readUTF());
System.out.println(ois.readUTF());
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readObject());
System.out.println(ois.readObject());
}
private static void writeObject() throws IOException {
//把Person对象保存到硬盘上
//1.新建对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\java82\\object.txt"));
//2.输出内容
//字符串
oos.writeUTF("hello");
oos.writeUTF("香港");
//基本类型
//int
oos.writeInt(888);
//boolean
oos.writeBoolean(false);
//Person对象
Person p = new Person("蒂姆·罗宾斯",28,100000000);
//java.io.NotSerializableException: com.igeek2.Person
//Person对象没有被序列化
oos.writeObject(p);
Person p1 = new Person("娜塔莉·波特曼",38,30000000);
oos.writeObject(p1);
//3.关闭流
oos.close();
}
}
注意:
- 对象必须实现Serializable接口
- 怎么写就怎么读
在序列化的过程当中,被transient修饰的属性,不会保存在硬盘
打印流
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式 打印流只有输出,没有输入
- PrintStream 字节打印流,调用println方法时自动刷新
- PrintWriter 字符打印流,指定自动刷新开关后,调用println方法时自动刷新,无需手动调用flush()方法。
第三方工具CommonsIO
导入classpath A:加入classpath的第三方jar包内的class文件才能在项目中使用 B:创建lib文件夹
- 将commons-io.jar拷贝到lib文件夹
- 右键点击commons-io.jar,Build Path→Add to Build Path FilenameUtils 这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题。 常用方法:
- getExtension(String path):获取文件的扩展名;
- getName():获取文件名;
- isExtension(String fileName,String ext):判断fileName是否是ext后缀名; FileUtils 提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。 常用方法:
- readFileToString(File file):读取文件内容,并返回一个String;
- writeStringToFile(File file,String content):将内容content写入到file中;
- copyDirectoryToDirectory(File srcDir,File destDir):文件夹复制;
- copyFile(File srcFile,File destFile):文件复制; 下载架包:www.apache.org