package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class IO222 {
public static void main(String[] args) {
//创建源
File src=new File("C:\\Users\\fy\\eclipse-workspace\\Fankai_studyjava\\src\\test\\n.txt");
//选择流
InputStream is = null;//作用域大一点 放在外面
try {
is = new FileInputStream(src);
int temp;
while((temp=is.read())!=-1) {//没有数据的时候read出来的是-1
System.out.println((char)temp);//强转
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读取
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if(null!=is) {//读取的文件是空的时候就不用关了
is.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//释放资源
}
}
分段读取????? 数据读到缓冲里面(字节数组)
//读取
byte[] flush=new byte[1024];//缓冲容器
//每次读取1024个字节
int len=-1;//接收长度
while((len=is.read(flush))!=-1) {//读取到flush里面
//字节数组—————》字符串解码
String str =new String(flush,0,len);//重点
System.out.println(str);
写出去
OutputStream
os = new FileOutputStream(dest);//写出去
//字符串》》》字节数组(编码)
String msg="duqianwenshiwonvpengyou";
byte[]datas=msg.getBytes();//字节数组
os.write(datas,0,datas.length);//一定要会写
os.flush();//刷新
复制文件
byte[] flush=new byte[1024];//缓冲容器
//每次读取1024个字节
int len=-1;//接收长度
while((len=is.read(flush))!=-1) {//读取到flush里面了 ,写出来即可
os.write(flush,0,len);
}
os.flush();
//注意分别关闭流 先打开的后关闭
字节输入流 都转化为字节 方便读取
//创建源
byte[]src="i love you dqw".getBytes();//创建字节数组
//选择流
InputStream is = null;
try {
is = new ByteArrayInputStream(src);//FileInputStream 改为
//ByteArrayInputStraeam
//读取
byte[] flush=new byte[1024];//缓冲容器
//每次读取三个字节
int len=-1;//接收长度
while((len=is.read(flush))!=-1) {//读car
String str =new String(flush,0,len);
System.out.println(str);
}
将图片转化为字节数组
package test1;
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;
//读取图片到字节数组
//字节数组写出到文件
public class TSIO {
public static void main(String[] args) {
byte[]m= fileToByteArray("C:\\Users\\fy\\eclipse-workspace\\Fankai_studyjava\\src\\范凯图片.png");
System.out.println(m.length);
}//1.读取图片到字节数组
//2.图片到程序FileInputStream
//3.程序到字节数组ByteArrayOutputstream
public static byte[] fileToByteArray(String filePath) {
File src=new File(filePath);
byte[]dest=null;
//选择流
InputStream is = null;
ByteArrayOutputStream baos=null;
try {
is = new FileInputStream(src);
baos= new ByteArrayOutputStream();
//读取
byte[] flush=new byte[1024];//缓冲容器
//每次读取三个字节
int len=-1;//接收长度
while((len=is.read(flush))!=-1) {
baos.write(flush, 0, len);//读car
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读取
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if(null!=is) {
is.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public static void ByteArrayTofile(byte[]src,String filePath) {
//字节数组写出到文件
//字节数组到程序ByteArrayInputStream
//程序写出到文件FileOutputStream
//选择流
File dest=new File(filePath);
//新建一个目的地
ByteArrayInputStream bais = null;
FileOutputStream os = null;
try {
bais = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
//读取
byte[] flush=new byte[1024];//缓冲容器
//每次读取三个字节
int len=-1;//接收长度
while((len=is.read(flush))!=-1) {//读car
os.write(flush,0,len);
}
os.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读取
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if(null!=is) {
is.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}