将源文件复制到目标文件Buffered

 
@Test void copyFile() throws IOException {                                                                   
    File file = new File("D:\\CloudMusic");                                                                  
    File fileout=new File("D:\\aa");                                                                         
    if (!fileout.exists()) {                                                                                 
        fileout.mkdirs();                                                                                    
    }                                                                                                        
    File[] files = file.listFiles();                                                                         
    for (File file1 : files) {                                                                               
        //获取到文件名                                                                                             
        String name = file1.getName();                                                                       
        File file2 = new File(file, name);                                                                   
        File file3 = new File(fileout, name);                                                                
        CopeFileToFile(file2,file3);                                                                         
    }                                                                                                        
}                                                                                                            
public void CopeFileToFile(File file,File newFile) throws IOException {                                      
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));                        
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));                  
        //小bug   bis.available() 获取字节 如果为0则会死循环                                                              
    byte[] buffer =null;                                                                                     
    if (bis.available()==0) {                                                                                
      buffer=new byte[1024];                                                                                 
    }                                                                                                        
    int i=0;                                                                                                 
        while ((i = bis.read(buffer)) != -1) {                                                               
            bos.write(buffer,0,i);                                                                           
        }                                                                                                    
        bos.flush();                                                                                         
        bos.close();                                                                                         
        bis.close();                                                                                         
}                                                                                                            

读出文件每一行后面加一个*

  InputStreamReader isr = new InputStreamReader(new FileInputStream("D://bb.txt"), StandardCharsets.UTF_8);                         
  BufferedReader reader = new BufferedReader(isr);                                                                                  
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://cc.txt"), StandardCharsets.UTF_8));   
  byte[] buffer=new byte[2048];                                                                                                     
  String len="";                                                                                                                    
  while ((len = reader.readLine()) != null) {                                                                                       
      writer.write(len+"*\n");                                                                                                      
  }                                                                                                                                 
  isr.close();                                                                                                                      
 //如果不加上这两个其中一个 都不会输送到文件 字符流还是存在高效区中                                              
writer.flush();                                                                    
writer.close();                                                                                                                                                                                     

对象序列化

    ObjectOutputStream outputStream;                                                                 
    outputStream = new ObjectOutputStream(new FileOutputStream("D:\\cc.txt"));                       
    outputStream.writeObject(new Person("来了老弟"));                                                    
    outputStream.close();                                                                            
    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("D:\\cc.txt"));        
    System.out.println(inputStream.readObject().toString());       
//但是被序列化的类必须要有实现Serializable接口  如果被序列化的类中有   transient(瞬时)修饰则不对其他进行序列化          




public class Person implements Serializable {
    private transient String name;

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        Person person = (Person) o;
        return Objects.equals(getName(), person.getName());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName());
    }
}                       
  //字节                                                                                   
  PrintStream printStream = new PrintStream("D:\\cc.txt");                               
  printStream.println("hello");                                                          
  //字符 需要关闭流缓冲区                                                                          
  PrintWriter printWriter = new PrintWriter(new FileWriter("D:\\cc.txt"));               
  printWriter.println("来了老弟");                                                           
  printWriter.close();                                                                   

利用 commons-io jar包

//Apache 复制文件
        FileUtils.copyFile(new File("D:\\CloudMusic\\aa.txt"),new File("D:\\aa\\aa.txt"));
//复制目录
        FileUtils.copyDirectoryToDirectory(new File("D:\\CloudMusic"),new File("D:\\v"));
发布了62 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_20282955/article/details/91436686
今日推荐