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

java基础-IO流

程序员文章站 2024-03-04 22:49:36
...

java基础-IO流

字节流和字符流

1.区别

  • 字节流:操作的数据单元是8位的字节
  • 字符流:操作的数据单元是16位的字节

2.输入流

  • 字节输入流(InputStream)
int read():从输入流中读取单个字节,返回所读取的字节数据;
int read(byte[] b):从输入流中读取b.length个字节数据,并存储在字节数组b中,返回实际             读取的字节数;
int read(byte[] b , int off , int len):从输入流中最多读取len个字节数据,并将存放在数组b从off位置开始存储数据,返回实际读取到的字节数
  • 字符输入流(Reader)
int read()
int read(char[] cbuf)
int read(char[] cbuf , int off , int len)

3.FileInputStream和Reader

package com.gdy.file;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by RS on 2017/10/16.
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {

        //创建字符输入流
        FileInputStream fis = new FileInputStream("test.txt");

        byte[] read1 = new byte[1024];
        //用于保存实际读取的字节数
        int readlength = 0 ;

        while((readlength = fis.read(read1)) > 0 ){
            System.out.println(new String(read1 , 0 , readlength));
        }

        fis.close();

    }
}
package com.gdy.file;

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

/**
 * @author rs
 * Created by RS on 2017/10/17.
 */
public class FileReadDemo {
    public static void main(String[] args) throws IOException {

        FileReader fileRead = new FileReader("test.txt");
        char[] chbf = new char[32];
        int hasRead = 0 ;
        while ((hasRead = fileRead.read(chbf)) >0){
            System.out.println(new String(chbf,0,hasRead));
        }
    }
}

3.输出流

1.OutputStream和Writer

void write(int c)将指定的字节/字符数组中的数据输出到指定输出流中
void writer(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定输出流中
void wirter(byte[]/char[],int off.int len):将字节数组/字符数组中从off位置开始,长度为len的数据出处到输出流中
void witer(String str):将str字符串中包含的字符输出到指定的输出流中
void witer(String str,int off,int len):将sr字符串中从off位置开始,长度为len的字符输出到指定输出流中

2.FileInputStream复制文件

package com.gdy.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
    public static void main(String[] args) {
        try {
            //创建输入字节流
            FileInputStream fileInputStream = new FileInputStream("data/FileTest.txt");
            //创建输出字节流
            FileOutputStream fileOutputStream = new FileOutputStream("data/FileTest222.txt");
            byte[] bytes = new byte[1024];
            int readLength = 0 ;

            while( (readLength = fileInputStream.read(bytes) ) > 0){
                fileOutputStream.write(bytes , 0 , readLength);
            }


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

3.FileWriter写文件

package com.gdy.file;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by RS on 2017/10/18.
 * 会覆盖之前的文件
 */
public class FIleWriterDemo {
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
             fileWriter = new FileWriter("newtest.txt");
             fileWriter.write("123");
            fileWriter.write("123");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}