今天简单的用Java代码来应用一下IO流:
package com.zking.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 需求:
* 指定一个文件:
* 如果这个文件存在,则读取这个文件中的内容
* 如果这个文件不存在,则写内容到这个文件中
* @author ZKai
*
*/
public class TestFile {
public static void main(String[] args) {
File file=new File("test.txt");
//判断 该文件是否存在
if(!file.exists()){
//不存在:创建文件 写内容
System.out.println("该文件不存在,正在创建...");
//创建文件
try {
boolean b=file.createNewFile();//判断文件是否创建成功
if(b){
System.out.println("创建成功了...");
//写内容
FileWriter fw=new FileWriter(file);
fw.write("你好!");
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}else{
//存在:读取内容
System.out.println("该文件存在,文件内容如下:");
//读
try {
FileReader fr=new FileReader(file);
char cbuf[]=new char[1024];
int len=0;
while((len=fr.read(cbuf))!=-1){
//字符数组转成字符串
String s=new String(cbuf, 0, len);
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
以上代码就是利用IO简单的创建文件,以及读取和写入文件。
接下来我说一下如何将对象序列化到硬盘中以及如何反序列化:
1.首先,我们创建一个实体类Person
package com.zking.entity;
import java.io.Serializable;
public class Person implements Serializable{ //注意实体类必须实现Serializable接口,不然是无法实现序列化的
private String uname;//账号
private String upass;//密码
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String uname, String upass) {
super();
this.uname = uname;
this.upass = upass;
}
}
2.测试
package com.zking.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.zking.entity.Person;
public class TestSer {
public static void main(String[] args) {
//序列化(将对象 存到硬盘中)
Person person=new Person("admin", "123");//创建一个person对象
try {
//序列化
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("user.exe"));//FileOutputStream中放入的是需要将对象序列化为什么格式的文件
oos.writeObject(person);
oos.close();
//反序列化(将存档从硬盘中读出来)
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("user.exe"));
Person person2=(Person) ois.readObject();
System.out.println(person2);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我们还可以利用IO实现文件的复制:
package com.zking.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
/**
* 实现文件复制
* 1.来一个文件(字节流)
* 2.读文件
* 3.写文件
* @author ZKai
*
*/
public class TestCopy {
public static void main(String[] args) {
File file=new File("Y2-1.1-IO使用字节流和字符流进行文件的读写和File操作.webm");//这是需要复制的文件
File fileOut=new File("C:\\Users\\Administrator\\Desktop\\a");//复制到哪里去
try {
//文件字节输入流(读)
FileInputStream fis=new FileInputStream(file);
//文件字节缓冲输入流
BufferedInputStream bis=new BufferedInputStream(fis,1024);
//文件字节输出流(写)
FileOutputStream fos=new FileOutputStream(fileOut+"\\"+file.getName());
//文件字节缓冲输出流
BufferedOutputStream bos=new BufferedOutputStream(fos);
int len=0;
byte[] b=new byte[1024];
//获取系统时间
long before=System.currentTimeMillis();
//没有使用缓冲流
// while((len=fis.read(b))!=-1){
// //写
// fos.write(b, 0, len);
// }
//使用缓冲流
while((len=bis.read(b))!=-1){
bos.write(b, 0, len);
}
bos.close();
bis.close();
long after=System.currentTimeMillis();
System.out.println("复制OK:"+(after-before));//时间用于查看输入输出流和缓冲输入输出流的区别
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上就是IO简单的应用,写的不好的地方请各位指教