java I/O系统
File类
File file=new File("f:");
System.out.println(file.getCanonicalPath());//返回当前路径
File files=new File(file,"demo.txt");
if(!files.exists()){
files.mkdir();//创建文件夹
}
System.out.println(files.canRead());
File[] file1=file.listFiles(new FileFilter() {//加入过滤器,只返回“.txt”文件。FilerFilter是接口,要new时需要实现其方法
@Override
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return pathname.getName().endsWith(".txt")&&pathname.isFile();
}
});
System.out.println(Arrays.toString(file1));
字节流
用于处理二进制文件
FileInputStream类
方法
public int read(byte[] b) throws IOException//读取文件,将读取的数据储存在byte数组b中
public void close()throws IOException
FileInputStream fileInput = null;
try {
fileInput=new FileInputStream("E:/朗沃/课件/笔记/Calendar.txt");//选择要读取的文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件没有找到");
}
byte []b=new byte[4096];//声明byte类型数组b,用于存放读取的数据
int num;
try {
while((num=fileInput.read(b))!=-1){//当num不等于-1,继续读取。
System.out.println(new String(b,"gbk"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream类
public void write(int b) throws IOException//往文件中写入数据
/*
* 字节流读、写操作
*/
FileInputStream fileInput=null;
FileOutputStream fileOut=null;
try {
fileInput=new FileInputStream(new File("f:/test.txt"));//字节输入流传入读取的文件
fileOut=new FileOutputStream(new File("f:/我的copy.doc"));//字节输出流,将读取到的字节写入文件路径中
int num1=0;
try {
while((num1=fileInput.read())!=-1){//边读边写
fileOut.write(num1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileOut.close();//文件关闭,放在finally中,最后文件都会关闭
fileInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
处理流BufferedInputStream及BufferedOutputStream
/*
* 处理流操作
*/
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
bis=new BufferedInputStream(new FileInputStream("f:/test.txt"));
bos=new BufferedOutputStream(new FileOutputStream("f:/wode.txt"));
int num2=0;
try {
while((num2=bis.read())!=-1){
bos.write(num2);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bis.close();
bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
字符流
用于处理文本文件
BufferedReader及BufferedWriter类
BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
BufferedWriter将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
应用:
BufferedReader br=null;
BufferedWriter bw=null;
try {
br=new BufferedReader(new FileReader("f:/test.txt"));
bw=new BufferedWriter(new FileWriter("f:/文件拷贝.txt"));
int num4=0;
while((num4=br.read())!=-1){
bw.write(num4);
}
bw.flush();//刷新该流的缓冲
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
br.close();
bw.close();
}
对象序列化
为了保存在内存中各个对象的状态,并且可以把对象再读取出来
方法
ObjectOutputStream类方法writeObject(Object) 序列化对象
ObjectInputStream类方法 readObject() 对象的反序列化
Student student=new Student();//Student必须实现Serializable接口才能序列化
student.setAge(19);
student.setName("张三");
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("f:/user.txt"));
oos.writeObject(student);//对象序列化
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("f:/user.txt"));
try {
Student s=(Student) ois.readObject();//对象反序列化
System.out.println(s.getName()+","+s.getAge());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
ois.close();
}