IO基本内容:InputStream、OutPutStream、Writer、Reader、Buffer..

IO

Test1_Stream

import java.io.*;
public class Test1_Stream {
    public static String path = "src/main/java/com/hello/demo/io/fos.txt";
    /**
     * //写入之前会情况数据
     */
    public void test1() throws IOException {
        try (FileOutputStream fos = new FileOutputStream(path)) {
            // 写出数据
            fos.write(97); // 写出第1个字节
            fos.write(98); // 写出第2个字节
            fos.write(99); // 写出第3个字节
            //fos.close(); //TODO  自动关闭资源,无需手动关闭, try-with-resource
        }
        //输出结果,abc
    }
    /**
     * 追加
     */
    public void test2() throws IOException {
        try (FileOutputStream fos = new FileOutputStream(path, true);) {
            fos.write("追加内容".getBytes()); // 写出第3个字节
        }
        //输出结果,abc
    }
    /**
     * 读取
     */
    public void test3() throws IOException {
        try (FileInputStream inputStream = new FileInputStream(path);) {
            int b;
            while ((b = inputStream.read()) != -1) {
                System.out.println(b);
            }
        }
    }
    //复制图片
    public void test4() throws IOException {
        try (InputStream inputStream = new FileInputStream("d://0.png");
             OutputStream outputStream = new FileOutputStream("d://2.png")) {
            byte[] b = new byte[1024];
            while ((inputStream.read(b)) != -1) {
                outputStream.write(b);
            }
        }
    }
}

WriterReader


import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Test2_WriterReader {
    public static String path = "src/main/java/com/codeying/demo/io/fos.txt";

    public void test1() throws IOException {
        try(Reader reader = new FileReader(path)){
            int n;
            while ((n=reader.read())!=-1){
                System.out.println(n+":"+(char)n);
            }
        }
    }
}

读取Properties

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
 * java.util.Properties 继承于 Hashtable ,来表示一个持久的属性集。
 * 它使用键值结构存储数据,每个键及其 对应值都是一个字符串。
 * 该类也被许多Java类使用,比如获取系统属性时
 * ystem.getProperties 方法就是返回 一个 Properties 对象。
 */
public class Test3_Properties {

    public static String propertiesPath = "src/main/resources/test.properties";
   
    public void test1() throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(propertiesPath));
        Set<String> strings = properties.stringPropertyNames();
        for (String key : strings) {
            System.out.println(key + " ‐‐ " + properties.getProperty(key));
        }
    }
}

Buffer


import java.io.*;

public class Test4_Buffer {
    public static String path = "src/main/java/com/codeying/demo/io/fos.txt";

    /**
     * 缓冲流,也叫高效流,是对4个基本的 FileXxx 流的增强,所以也是4个流,按照数据类型分类:
     * 字节缓冲流: BufferedInputStream , BufferedOutputStream
     * 字符缓冲流: BufferedReader , BufferedWriter
     */

    //BufferedInputStream
    //TODO 复制对象,使用缓冲流 + 字节数组,可以提高效率!
    public void test1() {
        try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"));
             BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe"));) {
            int len;
            byte[] bytes = new byte[8 * 1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //TODO BufferedReader BufferedWriter
    public void test2() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(path));
        // 定义字符串,保存读取的一行文字
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            System.out.println("---");
        }
        br.close();
    }

    public void test3() throws Exception {
        BufferedWriter bw = new BufferedWriter(new FileWriter(path, true));
        bw.newLine();//换行
        bw.write("追加内容!!");
        bw.newLine();
        bw.write("换行后追加!");
        bw.close();
    }
}

InputStreamReader


public class Test5_InputStreamReader {
    /**
     * TODO 转换流 :java.io.InputStreamReader ,是Reader的子类,是从字节流到字符流的桥梁。
     * 它读取字节,并使用指定 的字符集将其解码为字符。
     * 它的字符集可以由名称指定,也可以接受平台的默认字符集。
     */
    public String FileName = "src/main/java/com/codeying/demo/io/hello.txt";

    //读取
    public void test1() throws Exception {
//        InputStreamReader isr2 = new InputStreamReader(new FileInputStream(FileName),"GBK");//采用GBK。
        InputStreamReader isr2 = new InputStreamReader(new FileInputStream(FileName));//默认UTF-8
        int read;
        while ((read = isr2.read()) != -1) {
            System.out.print((char) read);
        }
        isr2.close();
    }

    //追加写入
    public void test2() throws Exception {
        OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream(FileName, true), "UTF-8");
        isr2.write("UTF-8内容:我我我我是UTF-8内容啊!aaa");
        isr2.close();

    }
}

PrintStream

替换掉原本输出到控制台的打印流。


public class test7_PrintStream {
    /**
     * 平时我们在控制台打印输出,是调用 print 方法和 println 方法完成的,
     * 这两个方法都来自于 java.io.PrintStream 类,
     * 该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
     */
    public static String path = "src/main/java/com/codeying/demo/io/fos.txt";
    @Test
    public void test1() throws FileNotFoundException {
        System.out.println(97);
        try (PrintStream printStream = new PrintStream(path,"UTF-8")
        ){
            System.setOut(printStream);
            System.out.println("hello");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值