import javax.imageio.IIOException; import java.io.*; import java.util.Date; //字符输入流 public class FileManagerChar { public static void readCharFile(File file){ FileReader fileReader=null;//文本输入流 if(file.exists()){ try { fileReader = new FileReader( file );//基于目标存在的文本文档输出流 char[] chs=new char[50];//字符零时缓冲区 int count=0;//存储实际读取的字符数量 while((count=fileReader.read(chs,0,chs.length))!=-1){ String s=new String( chs,0,count ); System.out.println( s ); } }catch (IOException e){ e.printStackTrace(); }finally { try { fileReader.close(); }catch(IOException e){ e.printStackTrace(); } } } } //使用文本缓冲流读取文件 public static void useBufferReader(File file){ FileReader read=null;//基于文件的普通输入流 BufferedReader br=null;//基于某个reader建立的字符缓冲流 if(file.exists()){ try { read=new FileReader( file );//基于文件建立普通文本输入流 br=new BufferedReader( read );//基于某个read建立文本缓冲流 char[] chs=new char[25]; int count=0; while ((count=br.read(chs,0,chs.length))!=-1){ String s=new String( chs,0,count ); System.out.println( s ); } }catch (IOException e){ e.printStackTrace(); }finally { try { br.close(); read.close(); System.out.println( "关闭成功" ); }catch (IOException e){ e.printStackTrace(); } } } } //字节输出流 public static void binaryOutStream(String filePath){ String str="start=E:\\BaiduNetdiskDownload\\baidu6\\1.mp4"; byte[] bys=str.getBytes();//将字符串转换为字节数组 OutputStream out=null; try { out = new FileOutputStream( filePath); out.write(bys); }catch (IOException e){ e.printStackTrace(); }finally { try { out.close(); System.out.println( "资源关闭" ); }catch (IOException e){ e.printStackTrace(); } } } //使用字节缓冲输出流 public static void useBufferedOutput(File file){ OutputStream out=null; BufferedOutputStream bs=null; String str="日照香炉生紫烟,\n遥看瀑布挂前川。\n飞流直下三千尺,\n以适应河洛就停"; byte[] bys=str.getBytes(); if(file.exists()){ try { System.out.println( file.getAbsolutePath() ); out = new FileOutputStream( file.getAbsoluteFile()+"/李白诗.doc" ); bs=new BufferedOutputStream( out );//基于某个outputstream建立缓冲输出流 bs.write( bys ,0,bys.length);//写入目标文件 }catch (IOException e){ e.printStackTrace(); }finally { try { bs.close(); out.close(); }catch (IOException e) { e.printStackTrace(); } } } } //字符输出流bufferwrite //file文件存储的目录 //filename 文件名称 //content 文件内容 public static void useBufferedWriter(File fir,String fileName,String content){ File file=null; Writer writer=null; BufferedWriter bw=null; if(fir.exists()){ file=new File(fir,fileName ); char chs[]=content.toCharArray(); try { writer=new FileWriter( file ); bw=new BufferedWriter( writer );//基于Writer实例创建字符缓冲流 bw.write(chs);//将char型数组所有内容写入到目标文件中 }catch (IOException e){ e.printStackTrace(); }finally { try { bw.close(); writer.close(); }catch (Exception e){ e.printStackTrace(); } } }else{ //创建目录后写入内容 System.out.println( "目标文件目录没找到" ); } } //文件复制 public static void copyFile(File target,File dir){ InputStream in=null; OutputStream out=null; File copyFile=null;//目标写的文件对象 if(target.exists()){//判断目标文件是否存在 if(!dir.exists()){ dir.mkdirs();//如果目标文件不存在,创建目录 } try { in = new FileInputStream( target );//基于文件建立输入流 String fileName=target.getName();//获取文件源名称 //避免文件重名 copyFile=new File(dir+"/"+new Date().getTime()+fileName);//基于目标写入文件对象 out=new FileOutputStream( copyFile );//基于目标文件建立输出流 byte[] bys=new byte[1024];//临时存储字节数据的缓冲区 int count=0;//记录读取内容的临时变量 while ((count=in.read(bys,0,bys.length))!=-1){ System.out.println( "文件赋值读写中,请稍后----" ); out.write( bys,0,count );//将临时缓冲区内容写入到目标文件中 } System.out.println( "目标赋值完成" ); }catch (IOException e){ e.printStackTrace(); }finally { try { out.close(); in.close(); }catch (IOException e){ e.printStackTrace(); } } } } //emp序列化对象 //target//序列化对象的目标文件 //java序列化,将员工对象保存到文件中 public static void javaSer(Employeee emp,File target){ OutputStream out=null; ObjectOutputStream oos=null;//序列化输出流 if(emp!=null){ try { out = new FileOutputStream( target ); oos = new ObjectOutputStream( out ); oos.writeObject( emp );//将序列化对象保存到文件中 }catch (IOException e){ e.printStackTrace(); }finally { try { oos.close(); out.close(); }catch (Exception e){ e.printStackTrace(); } } } } //反序列化 public static Employeee deser(File target) { InputStream in = null; ObjectInputStream ois = null; Employeee emp = null; if (target.exists()) { try { in = new FileInputStream( target ); ois = new ObjectInputStream( in ); //进行反序列化 Object obj = ois.readObject(); emp = obj != null ? (Employeee) obj : null;//如果不为空进行类型转换 } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { ois.close(); in.close(); }catch (IOException e){ e.printStackTrace(); } } } return emp;//返回对象 } }测试类 import java.io.File; public class test99 { public static void main(String[] args){ File target=new File( "E:/files2/歌谣.docx" ); File dir=new File( "d:/files/" ); FileManagerChar.copyFile( target,dir); } }