对于图片视频等文件用前面的字符流Reader和Writer是不行的,得用下面的字节流。
四步走套路,直接上代码
@Test
public void test(){
FileInputStream fileinputStream = null;
FileOutputStream fileOutputStream = null;
try {
//1.创建File类对象,指明读入写出文件
File srcFile = new File("时间API.png");
File destFile = new File("新时间API.png");
//2.创建流对象
fileinputStream = new FileInputStream(srcFile);
fileOutputStream = new FileOutputStream(destFile);
//3.数据的读入和写出
byte[] bBuf = new byte[20];
int len;
while ((len = fileinputStream.read(bBuf))!=-1){
fileOutputStream.write(bBuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源关闭
if(fileinputStream!=null){
try {
fileinputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
先来了解转换流的基本知识,包括字符集。转换流用起来的结构看上去像一根电线:铜线->胶皮->绝缘编织布,中间是一个流,外面包裹层的就是转换流。
* 1.转换流:属于字符流
* InputStreamReader:将一个字节的输入流转换为字符的输入流
* OutputStreamwriter:将一个字符的输出流转换为字节的输出流
*
* 2.作用:提供字节流与字符流之间的转换
*
* 3.解码:字节、字节数组--->字符数组、字符串
* 编码:字符数组、字符串--->字节、字节数组
*
* 4.字符集
* ASCII:.美国标准信息交换码。用一个字节的7位可以表示。
* IS08859-1:拉丁码表。欧洲码表用一个字节的8位表示。
* GB2312:中国的中文编码表。最多两个字节编码所有字符
* GBK:中国的中文编码表开级,融合了更多的中文文字符号。最多两个字节编码
* Unicode:国际标准码,融合了目前人类使用的所有字符。为每个字符分配唯一的字符码。所有的文字
都用两个字节来表示,落地实施为UTF-8和16,现有32
* UTF-8:变长的编码方式,可用1-4个字节来表示一个字符。
*
@Test
public void test1(){
java.io.InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1.造文件,造流
FileInputStream fis = new FileInputStream("三种建模方法对比和应用.txt");
FileOutputStream fos = new FileOutputStream("三种建模方法对比和应用_gbk.txt");
isr = new java.io.InputStreamReader(fis,"UTF-8");
osw = new OutputStreamWriter(fos,"gbk");
//2.读写过程
char[] cbuf = new char[20];
int len;
while((len = isr.read(cbuf))!= -1){
osw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//3.关闭资源
if(isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练练手:从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入"e"或者“exit"时,退出程序。
public static void main(String[] args) {
BufferedReader br = null;
try {
java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("请输入字符串: ");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序结束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练练手:打印255个ASCII字符
@Test
public void test2(){
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("PrintExer.txt"));
//创建打印输出流,设置为自动刷新模式(写入换行符或字节'\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true) ;
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { //输出ASCII字符
System. out . print((char) i);
if(i%50==0){//每50个数据一行
System. out.println(); //换行
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(ps!=null){
ps.close();
}
}
}
Person类需要满足以下要求,方可序列化
public class Person implements Serializable {
public static final long serialVersionUID = 4758912231L;
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectInputOutputStream {
/*
序列化过程:将内存中的Java对象保存到磁盘中或通过网络传输出去
使用ObjectOutputStream实现
*/
@Test
public void testObjectOutputStream(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("hah.dat"));
oos.writeObject(new String("我喜欢你"));
oos.flush();
oos.writeObject(new Person("小明",22));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos!=null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
反序列化:将磁盘文件中的对象还原为内存中的一个Java对象
使用ObjectInputStream
*/
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("hah.dat"));
Object o = ois.readObject();
String str = (String) o;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(ois!=null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}