java学习之字节流与字符流

字节流

字节输出流【OutPutStram】

java.io.OutputStream抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。

  • public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
  • public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
  • public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
  • public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
  • public abstract void write(int b) :将指定的字节输出流。

小贴士:

close方法,当完成流的操作时,必须调用此方法,释放系统资源。

FileOutputStream类

构造方法

  • public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。
  • public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。

当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。

public class FileOutputStreamConstructor throws IOException {
	public static void main(String[] args) {
		// 使用File对象创建流对象
		File file = new File("a.txt");
		FileOutputStream fos = new FileOutputStream(file);
		
		// 使用文件名称创建流对象
		FileOutputStream fos = new FileoutputStream("b.txt");
	}
}

写出字节数据

写出字节write(int b) 方法,每次可以写出一个字节数据,代码使用演示

public class FOSWrite {
	public static void main(String[] args) throws IOException {
		FileOutputStream fos = new FileOutputStream("fos.txt");
		// 写出数据
		fos.write(98);
		fos.write(97);
		fos.write(96);
		// 关闭资源
		fos.close();
	}
}

写出字节数组write(byte[] b),每次可以写出数组中的数据,代码使用演示:

public class FOSWrite {
	public static void main(String[] aegs) throws IOException {
		// 使用文件名称创建流对象
		FileOutputStream fos = new FileOutputStream("fos.txt");
		// 字符串转换为字节数组
		byte[] b = "三生三世".getBytes();
		// 写出字节数组数据
		fos.write(b);
		// 关闭资源
		fos.close();
	}
}

写出指定长度字节数组write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节,代码使用演示:

public class FOSWrite {
    public static void main(String[] args) throws IOException {
    		// 使用文件名创建流对象
    		FileOutputStream fos = new FileOutputStream("a.txt");
    		// 字符串转换为字节数组
    		bytes[] b = "abcde".getBytes();
    		// 写出从索引2开始,2个字节。
    		fos.write(b,2,2);
    		// 关闭资源
    		fos.close();
        }
}

数据追加续写

经过以上的演示,每次程序运行,创建输出流对象,都会清空目标文件中的数据。

  • public FileOutputStream(File file, boolean append): 创建文件输出流以写入由指定的 File对象表示的文件。
  • public FileOutputStream(String name, boolean append): 创建文件输出流以指定的名称写入文件。

这两个构造方法,参数中都需要传入一个boolean类型的值,true 表示追加数据,false 表示清空原有数据。这样创建的输出流对象,就可以指定是否追加续写了,代码使用演示:

public class FOSWrite {
	public static void main(String[] args) throws IOException {
		// 使用文件名称创建流对象
		FileOutputStream fos = new FileOutputStream("a,txt",true);
		// 字符串转换为字节数组
		bytep[] b = "abcde".getBytes();
		//写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
		fos.write();
		// 关闭资源
		fos.close();
	}
}

写出换行

public class FOSWrite {
	public class static void main(String[] args) throws IOExceptions{
		// 使用文件名创建流对象
		FileOutoutStream fos = new FileOutputStream("fos.txt");
		// 遍历数组
		for (int i = 0; i < words.length; i++) {
			// 写出一个字节
			fos.write(words[i]);
			// 写出一个换行,换行符转成数组写出
			fos.write("\r\n".getBytes());
		}
		// 关闭资源
		fos.close();
	}
}

字节输入流【InputStream】

读取字节数据

  1. 读取字节read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1,代码使用演示:
public class FISRead {
    public static void main(String[] args) throws IOException{
      	// 使用文件名称创建流对象
       	FileInputStream fis = new FileInputStream("read.txt");
      	// 读取数据,返回一个字节
        int read = fis.read();
        System.out.println((char) read);
        read = fis.read();
        System.out.println((char) read);
        read = fis.read();
        System.out.println((char) read);
        read = fis.read();
        System.out.println((char) read);
        read = fis.read();
        System.out.println((char) read);
      	// 读取到末尾,返回-1
       	read = fis.read();
        System.out.println( read);
		// 关闭资源
        fis.close();
    }
}
输出结果:
a
b
c
d
e
-1

循环改进读取方式,代码使用演示:

public class FISRead {
    public static void main(String[] args) throws IOException{
      	// 使用文件名称创建流对象
       	FileInputStream fis = new FileInputStream("read.txt");
      	// 定义变量,保存数据
        int b ;
        // 循环读取
        while ((b = fis.read())!=-1) {
            System.out.println((char)b);
        }
		// 关闭资源
        fis.close();
    }
}
输出结果:
a
b
c
d
e

小贴士:

  1. 虽然读取了一个字节,但是会自动提升为int类型。
  2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
  1. 使用字节数组读取read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,代码使用演示:
public class FISRead {
    public static void main(String[] args) throws IOException{
      	// 使用文件名称创建流对象.
       	FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
      	// 定义变量,作为有效个数
        int len ;
        // 定义字节数组,作为装字节数据的容器   
        byte[] b = new byte[2];
        // 循环读取
        while (( len= fis.read(b))!=-1) {
           	// 每次读取后,把数组变成字符串打印
            System.out.println(new String(b));
        }
		// 关闭资源
        fis.close();
    }
}

输出结果:
ab
cd
ed

错误数据d,是由于最后一次读取时,只读取一个字节e,数组中,上次读取的数据没有被完全替换,所以要通过len ,获取有效的字节,代码使用演示:

public class FISRead {
    public static void main(String[] args) throws IOException{
      	// 使用文件名称创建流对象.
       	FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
      	// 定义变量,作为有效个数
        int len ;
        // 定义字节数组,作为装字节数据的容器   
        byte[] b = new byte[2];
        // 循环读取
        while (( len= fis.read(b))!=-1) {
           	// 每次读取后,把数组的有效字节部分,变成字符串打印
            System.out.println(new String(b,0,len));//  len 每次读取的有效字节个数
        }
		// 关闭资源
        fis.close();
    }
}

输出结果:
ab
cd
e

小贴士:

使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率,建议开发中使用。

字节流练习:图片复制

public class Copy {
    public static void main(String[] args) throws IOException {
    		// 创建流对象
    		// 指定数据源
    		FileInputStream fis = new FileInputStream("D:\\test.jpg");
    		// 指定目的地
    		FileOutoutStream fos = new FileOutputStream("test_copy.jpg");
    		// 读数据
    		bytep[] b = new byte[1024];
    		int len;
    		while ((len = fis.read(b)!=-1)) {
    			// 写数据
    			fos.write(b,0,len);
    		}
    		//关闭资源
    		fos.close();
    		fis.close();
       }
}

小贴士:

流的关闭原则:先开后关,后开先关。

字符流

字符输入流【Reader】

java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。

  • public void close() :关闭此流并释放与此流相关联的任何系统资源。
  • public int read(): 从输入流读取一个字符。
  • public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。
发布了19 篇原创文章 · 获赞 0 · 访问量 153

猜你喜欢

转载自blog.csdn.net/qq_42586468/article/details/104088441