java IO流学习笔记------(2)文件字符流&字节数组流

java IO流学习笔记------(2)文件字符流&字节数组流

文件字符流FileReader&FileWriter

FileReader :通过字符的方式读取文件,仅适合字符文件
FileWriter :通过字节的方式写出或追加数据到文件中,仅适合字符文件

部分方法同文件字节流(read(),write())

其他方法:
FileWriter:append()写入
name.append(“你好”);
name.append(“你好,”).append(“朋友”);

文件字符输入流练习

package Io;

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

/**
 * @author 赌徒
 * 文件字符输入流练习
 *
 */
public class FileReaderT2 {
	public static void main(String[] args) throws IOException {
		//源头
		File srcfile=new File("data.txt");
		
		//选择流
		FileReader fr=new FileReader(srcfile);
		
		//操作
		char[] c=new char[1024];
		int len=-1;
		while((len=fr.read(c))!=-1) {
			String string=new String(c,0,len);
			System.out.println(string);
		}
		//释放
		fr.close();
	}
}

文件字符输出流练习

package Io;

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

/**
 * @author 赌徒
 * 文件字符输出流练习
 *
 */
public class FileWriterT2 {
	public static void main(String[] args) throws IOException {
		//源头
		File srcFile=new File("data.txt");
		//选择流
		FileWriter fw=new FileWriter(srcFile);//可以加true参数决定是否附加写入
		//操作
		fw.append("你好--");
		fw.append("你好,").append("朋友");
//             另种方法
//		String string="你好呀!";
//		char[] c=string.toCharArray();
//		fw.write(string);

		//刷新
		fw.flush();
		//释放
		fw.close();
	
	}
}

文件字符流综合练习

package Io;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 赌徒
 * 文件字符流综合练习
 *
 */
public class copy2 {
	public static void main(String[] args) throws IOException {
		//源头
		File srcFile=new File("data.txt");
		//目的地
		File srcFile1=new File("copy2.txt");
		//选择流
		FileReader fr=new FileReader(srcFile);
		FileWriter fw=new FileWriter(srcFile1);//可以加true参数决定是否附加写入
		//操作
		char[]c=new char[1024];
		int len=01;
		while((len=fr.read(c))!=-1) {
			fw.write(c,0,len);
		}
		fw.flush();

		//释放----先打开的后关闭
		fw.close();
		fr.close();
	
	}

}

字节数组流ByteArrayInputStream&ByteArrayOutputStream

ByteArrayInputStream 包含一个内部缓冲区,其中包含可以从流中读取的字节
ByteArrayOutputStream 该类实现了将数据写入字节数组的输出流当数据写入缓冲区时,缓冲区自动增长
ByteArrayInputStream​(byte[] buf) buf作为其缓冲区数组

size( )         返回缓冲区大小
toBytearray()      创建一个新分配的字节数组

部分方法同文件字节流(read(),write())

注意:
字节数组流无需关闭

字节数组输入综合练习

package reIo;

import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @author 赌徒
 * 字节数组输入综合练习
 *
 */
public class ByteArrayInputStreamT3 {
	public static void main(String[] args) throws IOException {
		//源头
		byte[] b="你好".getBytes();
		//选择流
		ByteArrayInputStream bis=new ByteArrayInputStream(b);
		//操作
		
		int len=-11;
		while((len=bis.read(b))!=-1) {
			String string=new String(b,0,len);
			System.out.println(string);
		}
		
		//释放--可以省略
		
		bis.close();	
	}
}

字节数组输出综合练习

package reIo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * @author 赌徒
 * 字节数组输出综合练习
 *
 */
public class ByteArrayOutputStreamT3 {
	public static void main(String[] args) throws IOException {
		//源头
		byte[] b=null;
		//选择流
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		//操作
		String string="你好";
		byte[] b1=string.getBytes();
		bos.write(b1,0,b1.length);
		bos.flush();
		b=bos.toByteArray();
		System.err.println(new String(b));
		
		//释放--可以省略
		bos.close();
	
	}

}

字节数组流综合练习

package Io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 赌徒
 * 图片写到字节数组中,字节数组写出到文件
 *
 */
public class copy3 {
	public static void main(String[] args) throws IOException {
		bytearrayTOpicture(pictureTObytearray("1.png"), "2.png");
		
	}
	//图片写到字节数组
	public static byte[] pictureTObytearray(String fileString) throws IOException {
		//源头
		byte[] b = null;
		//流
		FileInputStream fis=new FileInputStream(fileString);
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		//操作
		int len=-1;
		byte[] b1 = new byte[1024];
		
		while((len=fis.read(b1))!=-1) {
			bos.write(b1,0,len);
		}
		b=bos.toByteArray();
		bos.flush();
		//释放
		bos.close();
		fis.close();
		return b;
		
	}
	//字节数组到图片
	public static void bytearrayTOpicture(byte[] element,String fileString1 ) throws IOException {
		
		
		//流
		ByteArrayInputStream bis=new ByteArrayInputStream(element);
		FileOutputStream fos=new FileOutputStream(fileString1);
		
		//操作
		int len=-1;
		byte[] flush=new byte [1024];
		while((len=bis.read(flush))!=-1) {
			fos.write(flush,0,len);
		}
		fos.flush();
		//释放
		fos.close();
		bis.close();
		
		
	}

	

}
发布了3 篇原创文章 · 获赞 6 · 访问量 1032

猜你喜欢

转载自blog.csdn.net/qq_35577787/article/details/105104983