欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java中的IO流

程序员文章站 2024-03-04 15:23:47
...

java中的IO流

流分字节流与字符流

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import java.io.*;
import java.util.Properties;
import java.util.Set;

/**
 * @description:
 * @author: ljx
 * @time: 2020/7/13 16:35
 */
public class IoLearn {
    public static void main(String[] args) throws IOException {
//        test_io();
//        test_iobuf();
//        test_byte_io();
//        test_system_io();
//        test_system_io1()
        test_properties();

    }

    public static void test_io() throws IOException {
        //流分字节流和字符流,字节流InputStream,OutputStream,字符流抽象基类Reader,Writer
        //InputStream对应于Reader,OutputStream对应于Writer
        FileReader reader = new FileReader("C:\\Users\\hp\\IdeaProjects\\MyTests\\src\\main\\java\\demo.txt");
        FileWriter writer = new FileWriter("demo2.txt");
        char[] buf = new char[1024];
        int len = 0;
//        while((reader.read()!=-1)){
//            writer.write(reader.read());
//        }
        while ((len = reader.read(buf)) != -1) {
            writer.write(buf, 0, len);
        }
        reader.close();
        writer.close();
    }

    public static void test_iobuf() throws IOException {
        FileReader reader = new FileReader("C:\\Users\\hp\\IdeaProjects\\MyTests\\src\\main\\java\\demo.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);
        FileWriter writer = new FileWriter("demo2.txt");
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        char[] buf = new char[1024];
        int len = 0;
//        while((reader.read()!=-1)){
//            writer.write(reader.read());
//        }
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            bufferedWriter.write(line);
//            writer.write("\r\n");
            bufferedWriter.newLine();
            bufferedWriter.flush();
            bufferedWriter.flush();

        }
        reader.close();
        writer.close();
    }

    public static void test_byte_io() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("1.png");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        FileOutputStream fileOutputStream = new FileOutputStream("2.png");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes);
        }
        fileInputStream.close();
        fileOutputStream.close();

    }

    public static void test_system_io() throws IOException {
        //注意:单引号包裹的是char类型
        InputStream is = System.in;
        StringBuilder builder = new StringBuilder();
        int ch = 0;

        while ((ch = is.read()) != -1) {
            char c = (char) ch;
            if(ch=='\r'){
                continue;
            }
            if(ch=='\n') {

                String s = builder.toString();
                if(s.equals("over")){
                    break;
                }
                System.out.println(s.toUpperCase());
                builder.delete(0,builder.length());
            }else{
                builder.append(c);
            }
        }
    }
    public static void test_system_io1() throws IOException {
        //InputStreamReader,将字节流转换为字符流
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String line = null;
        while((line =reader.readLine())!=null){
            if(line.equals("over")){
                break;
            }
            writer.write(line.toUpperCase());
            writer.newLine();
            writer.flush();


        }
        reader.close();
        writer.close();

    }
    public static void  test_file() throws IOException {
        //File类用来将文件或者文件夹封装成对象,方便对文件与文件夹的属性信息进行操作
        File file = new File("demo3.txt");
        //文件分隔符,可根据操作系统进行更换
        System.out.println(File.separator);
        //文件名称
        System.out.println(file.getName());
        //绝对路径
        System.out.println(file.getAbsolutePath());
        //文件名
        System.out.println(file.getPath());
        //父级路径,如果此路径名没有指定父目录,则返回 null
        System.out.println(file.getParent());
        //文件大小
        System.out.println(file.length());
        //创建文件,不存在创建返回true
        System.out.println(file.createNewFile());
        //创建多级目录
        File dir = new File("aa\\bb");
        System.out.println(dir.mkdir());
        //判断文件是否存在,是否是文件,文件夹
        System.out.println(file.exists());
        System.out.println(file.isFile());
        System.out.println(file.isDirectory());
        File[] filse = File.listRoots();
        for(File file1 : filse){
            System.out.println(file1.getPath());
            System.out.println(file1.getTotalSpace());
            System.out.println(file1.getFreeSpace());
            System.out.println(file1.getUsableSpace());

        }
        //获取当前目录下的文件及文件夹名
        File file2= new File(".\\");
        String[] names = file2.list();
        for(String  file3 : names){
            System.out.println(file3);
        }


    }
    public static void test_properties() throws IOException {
        //键值对必须是字符串
        Properties p = new Properties();
        p.setProperty("zhangsan","20");
        p.setProperty("lisi","90");
        FileWriter writer = new FileWriter("a.properties");
        Set<String> names = p.stringPropertyNames();
        for(String name : names){
            System.out.println(p.getProperty(name));
            p.store(writer,name+p.getProperty(name));
        }
        

    }

}

 

相关标签: java基础