流的概念
流:流动,流向,是一个抽象,动态的概念,是一连串连续动态的额数据集合。常见的如L数据库,文件,内存,网络连接。IO设备。但是得记住,一切以程序为中心。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190519000224628.png?x-oss-process=image/请求
IO最重要的5个类和三个接口。
流:
功能分类:
- 节点流:可以直接从数据源或者目的地读写数据。
- 处理流:即包装流,不直接连接数据源和目的地,是其他流进行封装,目的主要是简化操作和提高性能。
两者关系:
1,节点流处于io操作的第一线,所有的操作必须通过他们来进行
2,处理流可以对其他流进行处理
数据分类:
1,字节流:按照字节读取数据(InputStream,OutputStream)
2,字符流:按照字符读取数据(Reader,Writer),因为文件编码的不通,从而有了对字符进行高效操作的字符流对象
原理:底层还是字节流操作,自动搜寻了指定的码表。
文件和目录:
public class FileLength {
public static void main(String[] args) throws IOException {
File src=new File("E:\\Study\\dy04\\gg.txt");
System.out.println("长度"+src.length());
src=new File("E:\\Study\\dy04");
System.out.println("长度"+src.length());
//创建文件(不存在才创建)
src=new File("E:\\Study\\dy04\\io.txt");
boolean flag=src.createNewFile();
System.out.println(flag);
//con,com3是操作系统关键字,不能创建
src=new File("E:\\Study\\dy04");
System.out.println(src.list());
boolean flag2=src.createNewFile();
System.out.println(flag2);
boolean flag1=src.delete();
System.out.println(flag1);
}
}
public class FileDemo2 {
public static void main(String[] args) {
File file=new File("D:/bb.txt");
System.out.println("名称:"+file.getName());
System.out.println("路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("父路径"+file.getParent());
System.out.println("父对象"+file.getParentFile());
System.out.println("是否存在"+file.exists());
System.out.println("是否是文件"+file.isFile());
System.out.println("是否是文件夹"+file.isDirectory());
file=new File("gg.text");
System.out.println("名称:"+file.getName());
System.out.println("路径:"+file.getPath());
System.out.println("绝对路径:"+file.getAbsolutePath());
System.out.println("父路径"+file.getParent());
System.out.println("是否存在"+file.exists());
System.out.println("是否是文件"+file.isFile());
System.out.println("是否是文件夹"+file.isDirectory());
File src=new File("xxx");
if(src.exists()){
System.out.println("文件不存在");
}else{
if(src.isFile()){
System.out.println("文件操作");
}else{
System.out.println("文件夹操作");
}
}
}
}
字符编码:
字符到字节 编码-----人的识别到机器
字节到字符 解码-----机器到人的识别
字符集:Java字符使用16位双字节存储。
```
` /**
* 解码。字节到字符串
*/
public class ContentDecode {
public static void main(String[] args) throws UnsupportedEncodingException {
String msg="性命生命使命a";
//编码
byte[] datas=msg.getBytes();
System.out.println(datas.length);
//乱码,字节数不够
msg=new String(datas,0,datas.length-2,"utf-8");
System.out.println(msg);
msg=new String(datas,0,datas.length,"utf-8");
System.out.println(msg);
//字符街编码解码不同一
msg=new String(datas,0,datas.length,"gbk");
System.out.println(msg);
//编码其他字符街
datas=msg.getBytes("UTF-16LE");
System.out.println(datas.length);
msg=new String(datas,0,datas.length,"UTF-16LE");
System.out.println(msg);
}
}
输入输出流总览图:
节点流:file开头byte开头,其他一般都为处理流,处理流以来与节点流存在。
FileInputStream和FileOutputStream复制文件:
//使用文件输入流和输出流打到文件的拷贝
public class CopyFile {
public static void main(String[] args) {
copy("lsd.txt","lsd1.txt");
}
public static void copy(String srcPath,String destPath){
//源头
File src=new File(srcPath);
InputStream is=null;
//目的地
File dest=new File(destPath);
OutputStream os=null;
try{
is=new FileInputStream(src);
os=new FileOutputStream(dest,false);
byte[] flush=new byte[1024];
int temp=-1;
while((temp=is.read(flush))!=-1){
os.write(flush,0,temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader和FileWriter:
public class InReader {
public static void main(String[] args) {
copy("dest.txt","dest1.txt");
}
public static void copy(String srcPath,String destPath){
//源头
File src=new File(srcPath);
Reader is=null;
//目的地
File dest=new File(destPath);
Writer os=null;
try{
is=new FileReader(src);
os=new FileWriter(dest,false);
char[] flush=new char[1024];
int temp=-1;
while((temp=is.read(flush))!=-1){
os.write(flush,0,temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ByteArrayOutputStream和ByteArrayInputStream:
public class zonghe {
public static void main(String[] args) {
byte[] data=fileToByteArray("IO.png");
System.out.println(data.length);
byteArrayToFile(data,"1.png");
}
public static byte[] fileToByteArray(String filePath){
//分段读取
File src=new File(filePath);
InputStream is=null;
ByteArrayOutputStream os=null;
try {
is=new FileInputStream(src);
os=new ByteArrayOutputStream();
byte[] flush=new byte[1024*10];
int temp=-1;
while((temp=is.read(flush))!=-1){
os.write(flush,0,temp);
}
os.flush();
return os.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void byteArrayToFile(byte[] src,String filePath){
ByteArrayInputStream is=null;
//创建园
File dest=new File(filePath);
OutputStream os=null;
try {
is=new ByteArrayInputStream(src);
os=new FileOutputStream(dest);
byte[] flush=new byte[1024*10];
int temp=-1;
while((temp=is.read(flush))!=-1){
os.write(flush,0,temp);
}
os.flush();
}catch (IOException e) {
e.printStackTrace();
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
装饰者模式:
1,抽象组件,需要装饰的抽象对象(接口或抽象类)
2,具体条件:需要装饰的对象
3,抽象装饰类:包含了对抽象组件的引用以及装饰共有的方法
4,具体装饰类:被装饰的对象
public class DecorateTest02 {
public static void main(String[] args) {
Drink coffee=new Coffee();
Drink suger=new Suger(coffee);
System.out.println(suger.cost());
System.out.println(suger.info());
Drink milk=new Milk(coffee);
System.out.println(milk.cost());
System.out.println(milk.info());
milk=new Milk(suger);
System.out.println(milk.cost());
System.out.println(milk.info());
}
}
//抽象组件 inputStram
interface Drink{
double cost();
String info();
}
//具体组件 FileInputStream
class Coffee implements Drink{
String name="原味咖啡";
@Override
public double cost() {
return 10;
}
@Override
public String info() {
return name;
}
}
//抽象装饰类FilterInputStream
abstract class Decrator implements Drink{
private Drink c;
Decrator(Drink c){
this.c=c;
}
@Override
public double cost() {
return this.c.cost();
}
@Override
public String info() {
return this.c.info();
}
}
//具体修饰类 类似bufferstream
class Milk extends Decrator{
Milk(Drink c) {
super(c);
}
@Override
public double cost() {
return super.cost()*4;
}
@Override
public String info() {
return super.info()+"加入了牛奶";
}
}
class Suger extends Decrator{
Suger(Drink c) {
super(c);
}
@Override
public double cost() {
return super.cost()*2;
}
@Override
public String info() {
return super.info()+"加入了这趟";
}
}
字节缓冲流:处理流,底层为节点流,只需要释放处理流就回自动释放节点流,手动释放为从里到外一次释放。
//使用文件输入流和输出流打到文件的拷贝
public class CopyFile {
public static void main(String[] args) {
Long t1=System.currentTimeMillis();
copy("装饰器设计模式.mp4","装饰器设计模式1.mp4");
Long t2=System.currentTimeMillis();
System.out.println(t2-t1);
}
public static void copy(String srcPath,String destPath){
//源头
File src=new File(srcPath);
InputStream is=null;
//目的地
File dest=new File(destPath);
OutputStream os=null;
try{
is=new BufferedInputStream(new FileInputStream(src));
os=new BufferedOutputStream(new FileOutputStream(dest,false));
byte[] flush=new byte[1024];
int temp=-1;
while((temp=is.read(flush))!=-1){
os.write(flush,0,temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字符缓冲流:
public class BufferOutputStreamTest01 {
public static void main(String[] args) {
copy("lsd.txt","lsd_copy.txt");
}
public static void copy(String srcPath,String destPath){
//源头
File src=new File(srcPath);
BufferedReader is=null;
//目的地
File dest=new File(destPath);
BufferedWriter os=null;
try{
is=new BufferedReader(new FileReader(src));
os=new BufferedWriter(new FileWriter(dest,false));
String line=null;
while((line=is.readLine())!=null){
os.write(line);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
InputStreamReader/OutputStreamWriter:字节流和字符流之间的桥梁,将字节流转换为字符流,指定字符集,可以处理一个个字符。处理纯文本内容。
public class ConcertTest02 {
public static void main(String[] args) throws IOException {
test2();
}
public static void test2() throws IOException {
BufferedReader isr=null;
BufferedWriter bsw=null;
//下载百度源代码
try{
isr=new BufferedReader(
new InputStreamReader(new URL("http://www.baidu.com").openStream(),"UTF-8"));
bsw=new BufferedWriter(
new OutputStreamWriter(new FileOutputStream("bai.html"),"UTF-8"));
String temp;
while((temp=isr.readLine())!=null){
bsw.write(temp);
bsw.flush();
}
}catch(IOException e){
System.out.println("操作异常");
}finally{
isr.close();
}
}
}
DataOutputStream/DataInputStream:顺序写入和写出
public class DataTest {
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(baos));
dos.writeUTF("编码辛酸泪");
dos.writeInt(18);
dos.writeBoolean(false);
dos.writeChar('a');
byte[] datas=baos.toByteArray();
DataInputStream dis=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
String msg=dis.readUTF();
int age=dis.readInt();
boolean flag=dis.readBoolean();
char ch=dis.readChar();
System.out.println(msg+" "+age+" "+flag+" "+ch);
}
}
序列化和反序列化:ObjectOutputStream 和ObjectInputStream 持久化
public class ObjectTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream dos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("lsddd")));
dos.writeUTF("编码辛酸泪");
dos.writeInt(18);
dos.writeBoolean(false);
dos.writeChar('a');
dos.writeObject("谁解其中味");
dos.writeObject(new Date());
dos.flush();
ObjectInputStream dis=new ObjectInputStream(new BufferedInputStream(new FileInputStream("lsddd")));
String msg=dis.readUTF();
int age=dis.readInt();
boolean flag=dis.readBoolean();
char ch=dis.readChar();
String str=(String)dis.readObject();
Date date=(Date) dis.readObject();
System.out.println(msg+" "+age+" "+flag+" "+ch+" "+str+" "+date);
}
}
PrintStream:打印流
public class PrintStreamTest {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps=System.out;
ps.println("sss");
ps.println(true);
ps=new PrintStream(new BufferedOutputStream(new FileOutputStream("print.txt")),true);
ps.println("sss");
ps.println(true);
System.setOut(ps);
System.out.println("change");
//重定向会控制台
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
System.out.println(1111);
ps.close();
}
}
``RandomAccessFile:随机访问文件
public class RandomTest01 {
public static void main(String[] args) throws IOException {
//应该分多少块
File src=new File("src/io/CopyFile.java");
long len=src.length();
int blockSize=1024;
int size=(int)Math.ceil(len*1.0/blockSize);
System.out.println(size);
int beginPos=0;
int actualSize=(int)(blockSize>len?len:blockSize);
for(int i=0;i<size;i++){
beginPos=i*blockSize;
if(i==size-1){
actualSize=(int)len;
}else{
actualSize=blockSize;
len-=actualSize;
}
System.out.println(i+"--->"+beginPos+"--->"+actualSize);
split(i,beginPos,actualSize);
}
}
public static void split(int i,int beginPos,int actualSize) throws IOException{
RandomAccessFile raf=new RandomAccessFile(new File("src/io/CopyFile.java"),"r");
RandomAccessFile raf2=new RandomAccessFile(new File("src/io/CopyFi1e"+i+".java"),"rw");
//随机读取
raf.seek(beginPos);
byte[] flush=new byte[1024];
int len=-1;
while((len=raf.read(flush))!=-1){
if(actualSize>len){
raf2.write(flush,0,len);
System.out.println(new String(flush,0,len));
actualSize-=len;
}else{
System.out.println(new String(flush,0,actualSize));
raf2.write(flush,0,actualSize);
break;
}
}
}
public static void test1() throws IOException {
RandomAccessFile raf=new RandomAccessFile(new File("src/io/CopyFile.java"),"r");
int beginPos=2;
int actualSize=1026;
//随机读取
raf.seek(beginPos);
byte[] flush=new byte[1024];
int len=-1;
while((len=raf.read(flush))!=-1){
if(actualSize>len){
System.out.println(new String(flush,0,len));
actualSize-=len;
}else{
System.out.println(new String(flush,0,actualSize));
}
}
}
}