ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

IO流的学习

2022-01-01 16:02:02  阅读:143  来源: 互联网

标签:fis IO read File 学习 file new store


IO流

File对象就不谈了。。

 

InputStream


 

①FileInputStream

缺点就是只能一个字节一个字节读,Ascii码一个字节(7bit有效),但汉字国标码2个B,UTF-8 3个B

public class IOtest01 {
   public static void main(String[] args) {
       File file = new File("");
       try {
           if (file.exists()&&file.length()>0){  //file合法才读
           FileInputStream fis = new FileInputStream(file);  //建立水管
           int ch = fis.read();      //读一个字节
           fis.close();        
           System.out.println(ch);
          }
      }catch(Exception e){
           System.out.println("不存在");
      }

  }
}

怎么读取全部呢?类似于c++的cin , 读完时fis.read 会返回-1

public class IOtest01 {
   public static void main(String[] args) {
       File file = new File("");
       try {
           if (file.exists()&&file.length()>0){  //file合法才读
           FileInputStream fis = new FileInputStream(file);  //建立水管
           
           while (fis.read()!=-1){      //判断读完与否
                int ch = fis.read();
               System.out.print(ch);
          }
           
           fis.close();
          }
      }catch(Exception e){
           System.out.println("不存在");
      }

  }
}

②BufferedInputStream

可以同时读多个字节

public static void main(String[] args) {
       try {
           FileInputStream fis = new FileInputStream(""); //小水管放File文件。作为

           BufferedInputStream bis = new BufferedInputStream(fis);  //大水管需要小水管作为基础,需要InputStream类
              //这里其实是多态的应用,因为FileInputStream extends InputStream

           byte[] store =new byte[1024];
           while(bis.read(store)!=-1){   //bis.read 向 byte[]中写入, 返回 int 含义是读了多少字节,返回-1时读取完毕。
               System.out.println(bis.read());
          }

           bis.close();

      } catch (Exception e) {
           e.printStackTrace();
      }
  }

 

OutputStream


 

①FileOutputStream

public static void main(String[] args) {
       String data = "hello world";  // 要写的东西

       File file = new File(""); //要往哪里写

       try {
           FileOutputStream fos = new FileOutputStream(file,true);  // 通道建立好了,地址也写上了
           // FileOutputStream 默认覆盖文件每次!!! 在构造方法加入第二个参数true,才是续写..不带True从开头写

           fos.write(data.getBytes(StandardCharsets.UTF_8)); //将string转换成byte[]
           //write 会将一个字节数组内容写入到file对象(文件)中 ,如果file不存在,则会自动创建
           fos.close();
      } catch (Exception e) {
           e.printStackTrace();
      }

  }

②BufferedOutputStream

 public static void main(String[] args) {
       try {
           FileInputStream fis = new FileInputStream("");
           BufferedInputStream bis = new BufferedInputStream(fis);
           FileOutputStream fos = new FileOutputStream("");
           BufferedOutputStream bos = new BufferedOutputStream(fos);
           byte[] store = new byte[1024] ;
           
           while (bis.read(store)!=-1){     //向store中读入
               
               bos.write(store,0,bis .read(store));   //从store中读出,从0 开始 到 偏移量 结束
          }
           
           bis.close();
           bos.close();


      } catch (Exception e) {
           e.printStackTrace();
      }
  }

 

FileReader 与 FileWriter

与前面很相似,只是读取写入以字符为单位,可以避免乱码的问题

public static void main(String[] args) {
       try {
           File file1 = new File("");
           FileReader fileReader = new FileReader(file1);

           File file2 = new File("");
           FileWriter fileWriter = new FileWriter(file2,true); //与FileOutputStream一样,可以+true

           char[] store = new char[1024];
           while (fileReader.read(store) != -1){
                //string构造方法,将char[] --> str
               String str = new String(store , 0 , fileReader.read(store));
               fileWriter.write(str);
               System.out.println(str);
          }

           fileReader.close();
           fileWriter.close();

      } catch (Exception e) {
           e.printStackTrace();
      }
  }

 

 

标签:fis,IO,read,File,学习,file,new,store
来源: https://www.cnblogs.com/mengshenjiao/p/15755553.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有