//字节读
FileInputStream fis=new fileInputStream(file);
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1){
.//将字节数组长转成字符串
String s=new String(b,o,len);
}
//字符读
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);
}
//字符写
PrintWriter pw=new PrintWriter(file);
pw.write("新内容");
pw.close();
递归:方法里面调方法。递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。
例:
import java.io.File;
package com.zking.io;
import java.io.File;
public class TestDiGui {
public static void main(String[] args) {
File f=new File("C:\\Users\\Administrator\\Desktop\\a");
read(f);
}
/**
*
* @param f 文件 【文件夹】
*/
public static void read(File f){
if(f.isDirectory()){
//文件夹
//列出该文件夹下所有的文件
File fs[]=f.listFiles();
if(fs.length>0){
//有子目录或者子文件
for (File file : fs) {
if(file.isDirectory()){
read(file);
}else{
System.out.println(file.getAbsolutePath());
}
}
}else{
//没有子目录或者子文件
System.out.println(f.getAbsolutePath());
}
}else{
//文件
System.out.println(f.getAbsolutePath());
}
}
}
序列化:将对象存到硬盘中(乱码)
反序列化:将存档从硬盘中读出(控制台)
具体实现代码:
Person person=new Person("admin", "123");
try {
//序列化
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("user.exe"));
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();
}
}