文件字符流 FileReader FileWriter
分段读取 文件字符输入流
package cn.dym;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* 四个步骤: 分段读取 文件字符输入流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
* @author
*
*/
public class IOTest505 {
public static void main(String[] args) {
//1、创建源
File src = new File("abc.txt");
//2、选择流
Reader reader =null;
try {
reader =new FileReader(src);
//3、操作 (分段读取)
char[] flush = new char[1024]; //缓冲容器
int len = -1; //接收长度
while((len=reader.read(flush))!=-1) {
//字符数组-->字符串
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if(null!=reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件字符输出流
package cn.dym;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 四个步骤: 文件字符输出流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
* @author
*
*/
public class IOTest06 {
public static void main(String[] args) {
//1、创建源
File dest= new File("dest.txt");
//2、选择流
Writer writer =null;
try {
writer =new FileWriter(dest);
//3、操作 (分段读取)
//写法一
// String msg="IO is so easy\r\n代止兮欢迎你";
// char[] datas=msg.toCharArray();//字符串——》字符数组
// writer.write(datas, 0, datas.length);
// writer.flush();
//写法二
// String msg="IO is so easy\r\n代止兮欢迎你";
// writer.write(msg); //字符也是字符数组
// writer.flush();
//写法三
writer.append("IO is so easy\r\n").append("哈哈哈\r\n").append("代止兮欢迎你");
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if(null!=writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节数组流
字节数组输入流
package cn.dym;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
/**
* 四个步骤: 字节数组输入流
* 1、创建源 : 字节数组 不要太大
* 2、选择流
* 3、操作
* 4、释放资源: 可以不用处理
* @author
*
*/
public class IOTest07 {
public static void main(String[] args) {
//1、创建源
byte [] src="talk is cheap show me the code".getBytes();
//2、选择流
InputStream is=null;
is=new ByteArrayInputStream(src);
}
}
字节数组输出流 ByteArrayOutputStream
package cn.dym;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 字节数组输出流 ByteArrayOutputStream
*1、创建源 : 内部维护
*2、选择流 : 不关联源
*3、操作(写出内容)
*4、释放资源 :可以不用
*
* 获取数据: toByteArray()
*
*/
public class IOTest07 {
public static void main(String[] args) {
//1、创建源
byte[] dest =null;
//2、选择流 (新增方法)
ByteArrayOutputStream baos =null;
try {
baos = new ByteArrayOutputStream();
//3、操作(写出)
String msg ="show me the code";
byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
baos.write(datas,0,datas.length);
baos.flush();
//获取数据
dest = baos.toByteArray();
System.out.println(dest.length +"-->"+new String(dest,0,baos.size()));
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源
try {
if (null != baos) {
baos.close();
}
} catch (Exception e) {
}
}
}
}
IO_综合_对接流实例
package cn.dym;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 1. 图片读取到字节数组中
* 2. 字节数组写出到文件
* 获取数据: toByteArray()
*
*/
public class IOTest08 {
public static void main(String[] args) {
//图片转成字节数组
byte[] datas = fileToByteArray("p.png");
System.out.println(datas.length);
byteArrayToFile(datas,"p-byte.png");
}
// 1. 图片读取到字节数组中
// 1). 图片到程序 FileInputStream
// 2). 程序到字节数组 ByteArrayOutputStream
public static byte[] fileToByteArray(String filePath) {
//1、创建源与目的地
File src = new File(filePath);
byte[] dest =null;
//2、选择流
InputStream is =null;
ByteArrayOutputStream baos =null;
try {
is =new FileInputStream(src);
baos = new ByteArrayOutputStream();
//3、操作 (分段读取)
byte[] flush = new byte[1024*10]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
baos.write(flush,0,len); //写出到字节数组中
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 2、字节数组写出到图片
* 1)、字节数组到程序 ByteArrayInputStream
* 2)、程序到文件 FileOutputStream
*/
public static void byteArrayToFile(byte[] src,String filePath) {
//1、创建源
File dest = new File(filePath);
//2、选择流
InputStream is =null;
OutputStream os =null;
try {
is =new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
//3、操作 (分段读取)
byte[] flush = new byte[5]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //写出到文件
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
}
}
}
}