首页 > 代码库 > oop.io流的应用

oop.io流的应用

io流分为    字节流与字符流

 字节流读写的是字节二进制  使用于 图片、视频

字节输入流 inputstream    实现类  FileInputstream 

字节输出流 outputstream  实现类 Fileoutputstream

inputstream 子类fileinputstream 子类datainputstream与dataoutputstream

 

字符流读写的是字符文本 实用于文本的读写  

字符输入流  reader  实现类 FileReader  

字符输出流  writer   实现类 FileWriter

带缓存区的字符流适合大文本读取,按一行一行的读取 bufferadreader 和bufferadwriter 

技术分享
package oop.io.file;

import java.io.File;
import java.io.IOException;

public class FileDemo {
    /**
     * 创建文件夹
     */
    public static void createFile(File file) {

        /**
         * 判断是否有文件
         */
        if (file.exists()) {
            if (file.isFile()) {
                System.out.println("是文件");
            } else if (file.isDirectory()) {
                System.out.println("是文件夹");
            }
        } else
        /**
         * 没有文件夹
         */
        {

            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    /**
     * 查看文件家
     * 
     */
    public static void getFileInfo(File file) {
        System.out.println("文件名" + file.getName());
        System.out.println("文件大小" + file.length());
        System.out.println("文件相对路径" + file.getPath());
        System.out.println("文件绝对路径" + file.getAbsolutePath());

    }
    
    /**
     * 删除文件
     * 
     */
    public static void deleteFile(File file){
        if(file.exists()){
             if (file.isFile()) {
                file.delete();
                System.out.println("文件已删除");
            }
            
        }else{
            
            System.out.println("文件不存在");
        }
        
        
    }
}
FileDemo
技术分享
package oop.io.file;

import java.io.File;

public class Test {
  public static void main(String[] args) {
    File file=new File("D://holly.txt");
    FileDemo.createFile(file);      
    FileDemo.getFileInfo(file);      
    //FileDemo.deleteFile(file); 
      
}
}
Test
技术分享
package oop.io.inoutstram;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import oop.io.file.FileDemo;

public class InOutStream {
    public static void main(String[] args) throws IOException {
        File file=new File("D://bizhi.jpg");
        /*readerFile(file); 
        writeFile(file);*/
        File file2=new File("D://ouchanglu.jpg");
        FileDemo.createFile(file2);
        copyPater(file2, file);
    }
    
    /**
     * 文件复制粘贴
     */
    /**
     * 字节输入流,写的
     * @throws IOException 
     * 
     */
   public static void copyPater(File file2,File file) throws IOException{
       
       InputStream inputStream=new FileInputStream(file);
       byte[] bsw=new byte[(int) file.length()];
       inputStream.read(bsw);
       
       OutputStream outputstream=new FileOutputStream(file2);
       outputstream.write(bsw);
       
       inputStream.close();
       outputstream.close();
       
       
   }
    
    
    
    
   public static void writeFile(File file) throws IOException{
      //创建输入流
       OutputStream outputStream=new FileOutputStream(file);
          
          String str="abcdefghijklmnopqrst";
          byte[] bsw=str.getBytes();
          outputStream.write(bsw);
          outputStream.close();
   }
    
   /**
    * 
    * 
    * 读取文件流
    */
    
   public static void readerFile(File file) throws IOException{
      
       InputStream inputStream=new FileInputStream(file);
        
       byte[] bws=new byte[(int) file.length()];
          inputStream.read(bws);       
       for (byte b : bws) {
        System.out.print(b);
    }   
       inputStream.close();
   }
   
   
   
   
}
inoutstream
技术分享
package oop.io.readerwrite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class ReadQWrite {
   public static void main(String[] args) throws IOException {
        copy("D://holly.txt", "D://ouchanglu.txt");
}
 /**
  * 复制粘贴  
 * @throws IOException 
  */
    
   public static void copy(String path2,String path) throws IOException{
      Reader reader=new FileReader(path2);
      char[] cs=new char[path2.length()];
      reader.read(cs);
      Writer writer=new FileWriter(path);
      writer.write(cs);
      reader.close();
      writer.close();
   }
   
   
   
    
}
readerWrete
技术分享
package oop.io.readerwrite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class ReadQWrite {
   public static void main(String[] args) throws IOException {
        copy("D://holly.txt", "D://ouchanglu.txt");
}
 /**
  * 复制粘贴  
 * @throws IOException 
  */
    
   public static void copy(String path2,String path) throws IOException{
      Reader reader=new FileReader(path2);
      char[] cs=new char[path2.length()];
      reader.read(cs);
      Writer writer=new FileWriter(path);
      writer.write(cs);
      reader.close();
      writer.close();
   }
   
   
   
    
}
bufferedwriter
技术分享
package oop.io.readerwrite;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class ReadQWrite {
   public static void main(String[] args) throws IOException {
        copy("D://holly.txt", "D://ouchanglu.txt");
}
 /**
  * 复制粘贴  
 * @throws IOException 
  */
    
   public static void copy(String path2,String path) throws IOException{
      Reader reader=new FileReader(path2);
      char[] cs=new char[path2.length()];
      reader.read(cs);
      Writer writer=new FileWriter(path);
      writer.write(cs);
      reader.close();
      writer.close();
   }
   
   
   
    
}
datainoutstream

 

oop.io流的应用