IO之字节流 与字符流
字节流与字符流有神魔区别呢?
字节流传输任何数据 图片 视频 文字 等等
而字符流只能处理纯文本(纯文本时也建议使用字符流)
电脑也是一样的哦,所以千万不要用字符流处理非字符文件。
因为本文只是IO的开篇所以先简单介绍
1.思想
主题把握四点
//1 选择源
//2 选择流
//3 操作
// 4 释放资源
2.代码
字节流
package IO;
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 选择流
//3 操作
// 4 释放资源
public class 标准版文件字节流 {
public static void main(String[] args) {
标准版文件字节流 f= new 标准版文件字节流();
f.out();
}
public void read() {//字节文件读取流
//选择源
File file = new File("king.txt");
//选择流
InputStream in= null;
try {
in = new FileInputStream(file);
//操作(分段读取)
byte [] flush =new byte [128];//缓冲容器
int len = -1;//接收容器
while((len = in.read(flush))!=-1) {
String str=new String(flush,0,len);//字节数组-->字符串 解码
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//释放资源
if(null!=in) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void out() {
//选择源
File file =new File("king.txt");
//选择流
OutputStream output =null;
try {
output = new FileOutputStream(file ,true);
//操作
String s ="我又来了";
byte [] n=s.getBytes();//编码
output.write(n, 0, n.length);
output.flush();//刷新
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//关闭流
if(null!=output) {
try {
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字节流文件的拷贝
package IO;
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 文件的拷贝 {
public static void main(String[] args) {
文件的拷贝 f = new 文件的拷贝();
f.copy("1.jpg", "测试的文件.jpg");
}
public void copy(String srcpath,String destpath) {
//选择源
//File file1 =new File("1.jpg");
//File file2 =new File("copy.jpg");
File file1 =new File(srcpath);
File file2 =new File(destpath);
//选择流
InputStream in =null;
OutputStream out = null;
try {
in=new FileInputStream(file1);
out =new FileOutputStream(file2);
//操作
byte [] flush = new byte [1024];
int len =-1;
while((len=in.read(flush))!=-1) {
out.write(flush, 0, flush.length);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//释放资源
try {
if(null!=out) {
out.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if(null!=in) {
in.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
字符流
package IO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class 文件字符输入输出流 {
public static void main(String[] args) {
文件字符输入输出流 f= new 文件字符输入输出流();
f.out();
}
public void in() {
//选择源
File file = new File("king.txt");
//选择流
Reader read=null;
try {
read=new FileReader(file);
char [] flush = new char [128];
int len =-1;
while((len=read.read(flush))!=-1) {
System.out.println(flush);
}
//操作
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {//释放资源
if(null!=read) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void out() {
File file =new File("文件字符流测试.txt") ;
Writer write =null;
try {
write=new FileWriter(file);
String test ="我实在不想玩了";
char [] flush =test.toCharArray();//字符串->数组
write.write(flush, 0,flush.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(null!=write) {
try {
write.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}