JavaIO笔记
本文最后更新于 1335 天前,其中的信息可能已经有所发展或是发生改变。

1. 什么是流

内存与存储设备之间传输数据的管道

2. 流的分类

  • 按方向
    • 输入流
    • 输出流
  • 按单位
    • 字节流

      以字节为单位,可以读写所有数据

    • 字符流

      以字符为单位,只能读写文本数据

  • 按功能

    • 节点流

      具有实际传输数据的读写功能

    • 过滤流

      在节点流的基础上增强功能

3. FileInputStream

FileInputStream fileInputStream = new FileInputStream("src\\main\\resouces\\ioFile\\test.txt");
int count=0;
byte[] bytes = new byte[3];
while((count= fileInputStream.read(bytes))!= -1){
    System.out.print(new String(bytes,0, count));
}

4. FileOutPutStream

FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resouces\\ioFile\\test.txt",true);
String str = "sadkl快乐了看看了\r\ndsfdsas放到";
fileOutputStream.write(str.getBytes());
fileOutputStream.close();

5. 缓冲流

  • 提高IO效率,减少磁盘的访问次数

  • 数据存在缓冲区,flush是将缓冲区的内容写入到文件,也可以直接close(内部调用flush)

  • 默认缓冲字节数组为8k

  • 缓冲区在内存

    FileInputStream inputStream = new FileInputStream("java-training-base/src/main/resouces/ioFile/file.txt");
    InputStreamReader fileReader = new InputStreamReader(inputStream, "utf-8");
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String tempString;
    while ((tempString=bufferedReader.readLine()) != null) {
        System.out.println(tempString);
    }
    bufferedReader.close();
    
    out=new BufferedWriter(
         new OutputStreamWriter(
                    new FileOutputStream(file,true)
         )
    );
    out.write(conent+"\r\n");
    

6. 序列化

  • Serializable是个标记接口
  • transient修饰符修饰的属性和静态属性不会被序列化

7. 字符流

  • unicode是可变长度的字符编码
    • 一个英文字符为1字节
    • 一个汉字为3字节
    FileReader fileReader = new FileReader("src\\main\\resouces\\ioFile\\test_chinese.txt");
    int count = 0;
    char[] ch=new char[2];
    while ((count=fileReader.read(ch))!= -1){
        System.out.println(new String(ch,0,count));
    }
    fileReader.close();
    FileWriter fileWriter = new FileWriter("src\\main\\resouces\\ioFile\\test_chinese.txt", true);
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
    String str = "sda快了快了快乐看了看看来";
    bufferedWriter.write(str);
    bufferedWriter.close();
    

8. 打印流

PrintWriter

9. 转换流

FileInputStream in = new FileInputStream("src\\main\\resouces\\ioFile\\test_chinese.txt");
InputStreamReader utf8 = new InputStreamReader(in, "utf8");
FileOutputStream fos = new FileOutputStream("src\\main\\resouces\\ioFile\\test_chinese.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "utf8");
  • 这里运用了装饰器模式
    • 装饰器模式是GOF23种设计模式中较为常用的一种模式。它可以实现对原有类的包装和装饰,使新的类具有更强的功能。

      我这里有智能手机iphone, 我们可以通过加装投影组件,实现原有手机功能的扩展。这就是一种“装饰器模式”。 我们在未来给普通人加装“外骨骼”装饰,让普通人具有力扛千斤的能力,也是一种“装饰器模式”。

      class Iphone {
          private String name;
          public Iphone(String name) {
              this.name = name;
          }
          public void show() {
              System.out.println("我是" + name + ",可以在屏幕上显示");
          }
      }
      
      class TouyingPhone {
          public Iphone phone;
          public TouyingPhone(Iphone p) {
              this.phone = p;
          }
          // 功能更强的方法
          public void show() {
              phone.show();
              System.out.println("还可以投影,在墙壁上显示");
          }
      }
      
      public class TestDecoration {
          public static void main(String[] args) {
              Iphone phone = new Iphone("iphone30");
              phone.show();
              System.out.println("===============装饰后");
              TouyingPhone typhone = new TouyingPhone(phone);
              typhone.show();
          }
      }
      

10. File和FileFilter

File file = new File("src\\main\\resouces\\ioFile\\file1.jpg");
file.createNewFile();
file = new File("src\\main\\resouces\\ioFile\\");
for (File fileItem : file.listFiles()) {
    System.out.println(fileItem.getName());
}
File[] jpgs = file.listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        if (pathname.getName().endsWith("jpg")) {
            return true;
        }
        return false;
    }
});
for (File Item:jpgs) {
    System.out.println(Item.getName());
}

11. Properties

  • 和流相关的集合
  • 键值都是String
Properties properties = new Properties();
properties.setProperty("user","bob");
properties.setProperty("age","41");
PrintWriter printWriter = new PrintWriter("src\\main\\resouces\\ioFile\\test.txt");
System.out.println(properties);
properties.list(printWriter);
printWriter.close();

FileOutputStream fileOutputStream = new FileOutputStream("src\\main\\resouces\\ioFile\\test.txt",true);
properties.store(fileOutputStream,"哈哈");
fileOutputStream.close();

FileInputStream fileInputStream = new FileInputStream("src\\main\\resouces\\ioFile\\test.txt");
Properties properties1 = new Properties();
properties1.load(fileInputStream);
System.out.println(properties1);

12. FileUtils和IOUtils

  • 需要用IO的地方,最好是使用apache的工具类
  • FieUtils类中常用方法的介绍
    1. cleanDirectory:清空目录,但不删除目录。
    2. contentEquals:比较两个文件的内容是否相同。
    3. copyDirectory:将一个目录内容拷贝到另一个目录。可以通过FileFilter过滤需要拷贝的 文件。
    4. copyFile:将一个文件拷贝到一个新的地址。
    5. copyFileToDirectory:将一个文件拷贝到某个目录下。
    6. copyInputStreamToFile:将一个输入流中的内容拷贝到某个文件。
    7. deleteDirectory:删除目录。
    8. deleteQuietly:删除文件。
    9. listFiles:列出指定目录下的所有文件。
    10. openInputSteam:打开指定文件的输入流。
    11. readFileToString:将文件内容作为字符串返回。
    12. readLines:将文件内容按行返回到一个字符串数组中。
    13. size:返回文件或目录的大小。
    14. write:将字符串内容直接写到文件中。
    15. writeByteArrayToFile:将字节数组内容写到文件中。
    16. writeLines:将容器中的元素的toString方法返回的内容依次写入文件中。
    17. writeStringToFile:将字符串内容写到文件中。
  • FieUtils类中常用方法的介绍
    1. buffer方法:将传入的流进行包装,变成缓冲流。并可以通过参数指定缓冲大小。
    2. closeQueitly方法:关闭流。
    3. contentEquals方法:比较两个流中的内容是否一致。
    4. copy方法:将输入流中的内容拷贝到输出流中,并可以指定字符编码。
    5. copyLarge方法:将输入流中的内容拷贝到输出流中,适合大于2G内容的拷贝。
    6. lineIterator方法:返回可以迭代每一行内容的迭代器。
    7. read方法:将输入流中的部分内容读入到字节数组中。
    8. readFully方法:将输入流中的所有内容读入到字节数组中。
    9. readLine方法:读入输入流内容中的一行。
    10. toBufferedInputStream,toBufferedReader:将输入转为带缓存的输入流。
    11. toByteArray,toCharArray:将输入流的内容转为字节数组、字符数组。
    12. toString:将输入流或数组中的内容转化为字符串。
    13. write方法:向流里面写入内容。
    14. writeLine方法:向流里面写入一行内容。
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇