文件操作
//用缓冲字节流实现文件读写
packageDataStream;
importjava.io.*;
publicclassBufferedStreamTest{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
try{
FileOutputStreamfos=new FileOutputStream("res/bufferFile");
BufferedOutputStreambos=newBufferedOutputStream(fos);
DataOutputStreamdos=newDataOutputStream(bos);
dos.writeUTF("helloworld");
dos.writeBoolean(false);
dos.writeInt(125);
dos.flush();//清空缓冲区
fos.close();
fos.close();
dos.close();
/*需要注意的是,由于读写顺序是从最外面开始读取也就是说从数据输出流DataOutputStream开始读取,其次是缓冲输出流BufferedOutputStream,再其次是文件输出流FileOutStream,所以如果不写d os.flush只要保证关闭的顺序正确也是可以正确运行的,即:
dos.close();
bos.close();
fos.close();
*/
FileInputStreamfis=new FileInputStream("res/bufferFile");
BufferedInputStreambis=newBufferedInputStream(fis);
DataInputStreamdis=newDataInputStream(bis);
System.out.println(dis.readUTF()+"\n"+dis.readBoolean()+"\n"+d is.readInt());//输出时注意与输入时保持一致
fis.close();
bis.close();
dis.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}//新增文档bufferFile直接查看会出现乱码问题,这是正常的,由于文档将字符型、布尔型、整型等放在一起使文档无法识别出现乱码,但是文档输出是正确的。
在写这段代码的时候,我们遇到了问题,由于没有提前清空缓冲数据,导致程序运行错误,后来找到了错误的原因,原因是:
Flush()主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还有一部分有可能会留在内存这个缓冲区中。这时候如果你调用了Close()方法关闭了读写流,那么这部分数据就会丢失,所以应该在关闭读写流之前先Flush(),先清空数据。
文件操作类
文件类(File)提供处理文件的方法,包括:更改文件名、删除文件、列出目录下的文件以及文件对象属性的描述信息等。
publicFile(Stringpathname);根据parent抽象路径名和child路径名字符串创建一个新的File对象
publicFile(Stringpath,Stringname);根据parent路径名字符串和child路径名字符串创建一个新File对象
publicFile(Fileparent,Stringchile);通过指定路径名字字符串转换为抽象路径名来创建一个新的File对象
感觉有点绕啊.......通过实例来解决,其实还是很简单的
Source.txt
Backup\source.txt
执行备份:
1、判断目录备份是否存在,如无建立目录①backup②将源文件复制到backup;如有:判断是否需要备份,如有必要:将源文件复制到backup,如没必要
其他方法
(1)访问文件对象
l publicStringgetName()//返回文件对象名,不包含路径名
l publicStringgetPath()//返回相对路径名,包含文件名
l publicStringgetAbsolutePath()//返回绝对路径名,包含文件名
l publicStringgetParent()//返回父文件对象的路径名
l publicFilegetParentFile()//返回父文件对象
(2)获得文件属性
l publiclonglength() //返回指定文件的字节长度
l publicbooleanexists()//测试指定的文件是否存在
l publiclonglastModified()//返回指定文件最后被修改的时间
(3)文件操作
l publicbooleanrenameTo(Filedest)//文件重命名
l publicbooleandelete()//删除空目录
(4)目录操作
l publicbooleanmkdir()//创建指定日录,正常建立时返回true
l publicString[]list()//返回目录中的所有文件名字符串
l publicFile[]listFiles()//返回指定目录中的所有文件对象
感觉有点绕啊.......通过实例来解决,其实还是很简单的
例子如下:
//生成目录文件和输出目录文件
由于操作非常简单,按照方法就能实现,在这里就不做演示了
//利用File自动更新文件
packageDataStream;
importjava.io.*;
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassFileUpdateTest{
publicstaticvoidmain(String[]args)throwsIOException{
//待复制的文件名
Stringfname="source.txt";
//目标目录名
Stringdestdir="backup";
update(fname,destdir);
}
privatestaticvoidupdate(Stringfname,Stringdestdir)throwsIOException{
//TODOAuto-generatedmethodstub
Filef1,f2,dest;
//在当前目录中创建文件对象f1,dest
f1=newFile(fname);
dest=newFile(destdir);
if(f1.exists()){
//dest不存在时创建目录
if(!dest.exists()){
System.out.println("dest不存在,创建backup");
dest.mkdir();
}
//在目录dest中创建文件f2
f2=newFile(dest,fname);
longd1=f1.lastModified();
longd2=f2.lastModified();
//f2不存在时或存在但日期较早时
if((!f2.exists())||(f2.exists()&&(d1>d2))){
copy(f1,f2);//复制
}
showFileInfo(f1);
showFileInfo(dest);
}else{
System.out.println(f1.getName()+"filenotfound");
}
}
publicstaticvoidshowFileInfo(Filef1)throwsIOException{
//TODOAuto-generatedmethodstub
SimpleDateFormatsdf;
sdf=newSimpleDateFormat("yyyy年MM月dd日hh时mm分");
if(f1.isFile()){
Stringfilepath=f1.getAbsolutePath();
Dateda=newDate(f1.lastModified());
Stringmat=sdf.format(da);
System.out.println("<文件:>\t"+filepath+"\t"+f1.length()+"\t"+mat);
}else{
System.out.println("<目录:>"+"\t"+f1.getAbsolutePath());
File[]files=f1.listFiles();
for(inti=0;i<files.length;i++){
showFileInfo(files[i]);
}
}
}
publicstaticvoidcopy(Filef1,Filef2)throwsIOException{
//TODOAuto-generatedmethodstub
//创建文件输入流对象
FileInputStreamfis=newFileInputStream(f1);
//创建文件输入流对象
FileOutputStreamfos=newFileOutputStream(f2);
intcount,n=512;
bytebuffer[]=newbyte[n];
//从输入流读取数据
count=fis.read(buffer,0,n);
while(count!=-1){
//写出输出流数据
fos.write(buffer,0,count);
count=fis.read(buffer,0,n);
}
System.out.println("复制文件"+f2.getName()+"成功!");
//关闭输入\输出流
fis.close();
fos.close();
}
}