您的位置 首页 java

java之文件流与转换流

java之文件流与转换流

文件字节流

FileInputStream:文件字节输入流

  • 定义 :public class FileInputStream extends InputStream
  • 构造方法:public FileInputStream(String name) throws FileNotFoundException
  • public FileInputStream(File file) throws FileNotFoundException
  • FileIOutputStream:文件字节输出流

  • 定义:public class FileOutputStream extends OutputStream
  • 构造方法:public FileOutputStream(String name) throws FileNotFoundExceptio
  • public FileOutputStream(String name, boolean append)throws FileNotFoundException //标记是否追加,默认fase覆盖
  • public FileOutputStream(File file) throws FileNotFoundException
  • public FileOutputStream(File file, boolean append)throws FileNotFoundException //标记是否追加,默认fase覆盖
  • 文件字符流

    FileReader:文件字符输入流

  • 定义:public class FileReader extends InputStreamReader //中间有个转换流
  • public class InputStreamReader extends Reader
  • 构造方法:public FileReader(String fileName) throws FileNotFoundException
  • public FileReader(File file) throws FileNotFoundException
  • FileWriter:文件字符输出流

  • 定义: public class FileWriter extends OutputStreamWriter //中间有个转换流
  • public class OutputStreamWriter extends Writer
  • 构造方法:public FileWriter(String fileName) throws IOException
  • public FileWriter(String fileName, boolean append) throws IOExceptio//标记是否追加,默认fase覆盖
  • public FileWriter(File file) throws IOException
  • public FileWriter(File file, boolean append) throws IOException//标记是否追加,默认fase覆盖
  • 转换流:

    转换流,是将字节流转为字符流的一种中间流

    字节输入流转为字符输入流: InputStreamReader

    定义:public class InputStreamReader extends Reader

    构造方法:public InputStreamReader(InputStream in) //字节流构造传入

    public InputStreamReader(InputStream in, String charsetName) //增加编码设置

    字节输出流转为字符输出流: OutputStreamWriter

    定义:public class OutputStreamWriter extends Writer

    构造方法:public OutputStreamWriter(OutputStream out)

    public OutputStreamWriter(OutputStream out, String charsetName)

    编码示例:从文件读取txt文件,将其内容按字节流读出,并转为字符流,在打印台显示;

    将字符串输出到文件中

     class MyStream{    
        //读取文件内容并打印在控制台    
        public static void printTxt(File file) throws Exception {      
          if (file.exists()){      
            InputStream in = new FileInputStream(file);       
            //转为字符流       
            Reader reader = new InputStreamReader(in);      
            char[] c = new char[1024];        
            int b;           
            while ((b = reader.read(c)) != -1){        
              System.out.println(c);      
            }           
            //注意关闭顺序,先关闭外围,再关闭内围       
            reader.close();         
            in.close();      
          }    
        }    
      //将字符串输出到文件中保存    
      public static void save(File file)throws Exception{   
        if(!file.exists()){          
          //文件不存在,创建目录和创建文件  
          file.getParentFile().mkdirs();       
          file.createNewFile();  
        }      
        FileOutputStream out = new FileOutputStream(file);     
        //转为字会输出流      
        Writer writer = new OutputStreamWriter(out);     
        writer.write("今天日期是:"+ LocalDate.now().getYear()+"-"+LocalDate.now().getMonthValue()+"-"+LocalDate.now().getDayOfMonth());   
        //注意关闭顺序,先关闭外围,再关闭内围     
        writer.close();       
        out.close();  
      }
    }  

    文章来源:智云一二三科技

    文章标题:java之文件流与转换流

    文章地址:https://www.zhihuclub.com/174747.shtml

    关于作者: 智云科技

    热门文章

    网站地图