java IO stream study notes ------ (2) the character stream file & byte array stream

java IO stream study notes ------ (2) the character stream file & byte array stream

File character stream FileReader & FileWriter

The FileReader : reading a file by means of the character, only the character file for
FileWriter : by way of written bytes or append data to a file, only suitable character file

The method of the same part of a file byte stream (read (), write () )

Other methods:
FileWriter: the append () writes
name.append ( "Hello");
name.append ( "Hello,") append ( "Friends");.

File character input stream practice

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();
	}
}

File character output stream practice

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();
	
	}
}

File character stream Exercises

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();
	
	}

}

Byte array stream ByteArrayInputStream & ByteArrayOutputStream

ByteArrayInputStream contains an internal buffer, which contains the byte can be read from the stream
ByteArrayOutputStream which implements the byte array when writing data to the output stream when the data write buffer, the buffer autogrowth
ByteArrayInputStream (byte [ ] buf) buf as its buffer array

size () returns the buffer size
toBytearray () to create a new array of bytes allocated

The method of the same part of a file byte stream (read (), write ())

Note: The
byte array stream without closing

Byte array input Exercises

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();	
	}
}

Byte array output Exercises

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();
	
	}

}

Byte array stream Exercises

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();
		
		
	}

	

}
Released three original articles · won praise 6 · views 1032

Guess you like

Origin blog.csdn.net/qq_35577787/article/details/105104983