一、File类
File file = new File("c://abc//aa.txt");
File filedir = new File("c://abc");
注:1、文件和目录都是File类,不能删除非空目录
二、流
流入流出的方向是站在内存的角度来看,也即是程序的角度
1、字节流:
(抽象类)InputStream,OutputStream
(实现类)FileInputStream,FileOutputStream
字节输入流:
public void inputStream(String source){
try {
InputStream input = new FileInputStream(source);
int temp = input.read();
while(temp != -1){
System.out.print((char)temp);
temp = input.read();
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
字节输出流:
public void outputStream(String dest){
String str = "I love you!";
byte[] words = str.getBytes();
try {
OutputStream output = new FileOutputStream(dest);
output.write(words, 0, words.length);
output.flush();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copy(String source,String dest){
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(source);
outputStream = new FileOutputStream(dest);
int num = inputStream.read();
while(num != -1){
outputStream.write(num);
num = inputStream.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2、字符流:
(抽象类)Reader,Writer
(实现类) FileReader,FileWrite
//字符流copy
public void copyCharacter(String source,String dest){
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader(source);
writer = new FileWriter(dest);
int num = reader.read();
while(num != -1){
System.out.print((char)num);
writer.write(num);
num = reader.read();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
reader.close();
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、带缓存的输入输出流:
BufferedReader,BufferedWriter;
BufferedInputStream,BufferedOutputStream
public static void bufferReader(String dest){
Scanner sc = new Scanner(System.in);
ArrayList<Emplyee> emplyees = new ArrayList<Emplyee>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = null;
String flag = "y";
while("y".equalsIgnoreCase(flag)){
Emplyee emp = new Emplyee();
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入工资:");
int in = sc.nextInt();
Date date = new Date();
time = sdf.format(date);
emp.setName(name);
emp.setIncome(in);
emplyees.add(emp);
System.out.println("还要输入吗?(y/n)");
flag = sc.next();
}
System.out.println("共"+emplyees.size()+"个有员工");
System.out.println("姓名"+"\t"+"工资"+"\t"+"录入时间");
for(int i = 0;i < emplyees.size();i++){
Emplyee e = emplyees.get(i);
System.out.println(e.getName()+"\t"+e.getIncome()+"\t"+time);
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(dest));
bw.write("共"+emplyees.size()+"个有员工"+"\r\n");
bw.write("姓名"+"\t"+"工资"+"\t"+"录入时间"+"\r\n");
for(int j = 0;j < emplyees.size();j++){
Emplyee m = emplyees.get(j);
bw.write(m.getName()+"\t"+m.getIncome()+"\t"+time+"\r\n");
}
bw.flush();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void bufferCopyByte(String source,String dest){
try {
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(dest));
int num = bi.read();
while(num != -1){
bo.write(num);
num = bi.read();
}
bi.close();
bo.flush();
bo.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
三、Java属性文件
//登录方法
public void readProperties(String source,String name,String secret){
Properties pro = new Properties();
try {
pro.load(new FileReader(source));
if("admin".equals(name)){
if(pro.getProperty(name).equals(secret)){
System.out.println("登录成功");
f = 1;
}else{
System.out.println("密码错误");
f = -1;
}
}else{
System.out.println("登录失败");
f = -1;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//注册方法
public void writeProperties(String source,String name,String secret){
Properties properties = new Properties();
try {
properties.load(new FileReader(source));
if(properties.getProperty(name) == null){
properties.setProperty(name, secret);
properties.store(new FileWriter(source), "");
}else{
System.out.println("用户名已注册");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
四、对象序列化
Serializable接口是一个标记性接口,它告诉JVM将实现他的对象转换成一系列二进制字节,并可在以后完全恢复回原来的样子。
1、序列化对象的输出流 ObjectOutputStream 。Object
1、需要序列化的对象要实现接口 Serializable。
2、使用ObjectOutputStream 对象保存对象 方法使用wirteObject();
3、使用ObjectInputStream 把序列化文件还原为java对象 方法是 readObject
2、序列化解释
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
3、编辑本段序列化的目的
1、以某种存储形式使自定义对象持久化;
2、将对象从一个地方传递到另一个地方。 方便网络传输
3、使程序更具维护性