IO流

- 核心类

  1. File–文件类
public class PathDemo01 {
	public static void main(String[] args) {
		String path = "C:/Users/GK/Desktop/IO.jpg";
		path = "IO.jpg";//相对于当前工程,即相对于"user.dir"
		System.out.println(File.separatorChar);
//		path = "C:" + File.separator + "Users" + File.separator + "...";//常量拼接
		
		File src = new File(path);
		System.out.println(src.getAbsolutePath());
		System.out.println(System.getProperty("user.dir"));//
		
		src = new File("C:/Users/GK/Desktop","IO.jpg");
		
		src = new File(new File("C:/Users/GK/Desktop"),"IO.jpg");//
		
		System.out.println(src.length());
		
	}
}

package org.gk;

import java.io.File;
import java.io.IOException;

public class FileDemo03 {

	public static void main(String[] args) throws IOException {
		
		File src = new File("C:/Users/GK/Desktop","IO.jpg");
		
		System.out.println("name: " + src.getName());
		System.out.println("path: " + src.getPath());//绝对(相对)路径返回绝对(相对)路径
		System.out.println("absolutepath: " + src.getAbsolutePath());
		System.out.println("parent: " + src.getParent());//返回名称分隔符的前一部分
		System.out.println(src.getParentFile().getAbsolutePath());
		
		System.out.println("ifexist: " + src.exists());
		
		//存在的条件下才能判断是否为File/Directory
		System.out.println("是否文件:" + src.isFile());
		System.out.println("是否为文件夹:" + src.isDirectory());
		
		if(null == src || !src.exists()) {//空指针引用
			System.out.println("notExist: " + src.length());
		}else {
			if(src.isFile()) {
				System.out.println("fileLength: " + src.length());
			}else {
				System.out.println("00000000");
			}
		}
		
		File src01 = new File("C:/Users/GK/Desktop/aa.txt");//文件名不能为平台(系统)关键字
		
		boolean flag = src01.createNewFile();
		System.out.println(flag);
		
		boolean flag01 = src.delete();
		System.out.println(flag01);

	}

}

package org.gk;

import java.io.File;

public class DirTest {
	public static void main(String[] args) {
		
		File dir = new File("C:/Users/GK/Desktop/dir/test");
		boolean flag = dir.mkdirs();//dir.mkdir();--父目录须存在
		System.out.println(flag);
		
		File dir01 = new File("C:/Users/GK/Desktop");
		
		//name
		String[] subName = dir01.list();
		for(String s : subName) {
			System.out.println(s);
		}
		
		//file obj
		File[] subobj = dir01.listFiles();
		for(File f : subobj) {
			System.out.println(f.getAbsolutePath());
		}
		
		//所有盘符
		File[] lr = dir01.listRoots();
		for(File f : lr) {
			System.out.println(f.getAbsolutePath());
		}
		
		printNum(1,99);
		
		
		
	}
	
	//用递归打印n-m的数
			public static void printNum(int n,int m) {
				if(n>m) {
					return;
				}
				System.out.println(n);
				printNum(n+1,m);
			}
}

package org.gk;

import java.io.File;

public class DirCount {
	
	private long len;
	private File src;
	private int fileSize;
	private int dirSize;

	public DirCount(String path) {
		this.src = new File(path);
		count(this.src);
		
	}
	
	
	private  void count(File src) {
		if(null!=src && src.exists()) {
			if(src.isFile()) {
				len+=src.length();
				this.fileSize++;
			}else {
				for(File f : src.listFiles()) {
					count(f);
				}
				this.dirSize++;
			}
		}
	}
	
	public static void main(String[] args) {
		DirCount dir = new DirCount("C:/Users/GK/Desktop");
		System.out.println(dir.getLen());
		System.out.println(dir.getDirSize());
	}


	public long getLen() {
		return len;
	}



	public int getFileSize() {
		return fileSize;
	}



	public int getDirSize() {
		return dirSize;
	}

	
}

package org.gk;

import java.io.UnsupportedEncodingException;

public class ContentEncode {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String str = "hello五";
		String str01 = "hello五";
		
		//字符串到字符数组
		byte[] b = str.getBytes();
		System.out.println(b.length);
		
		b = str01.getBytes("UTF-16LE");
		System.out.println(b.length);
		
		//字符数组到字符串
		String msg = new String(b,0,b.length,"utf-16le");
		System.out.println(msg);
	}
}

  1. InputStream–字节输入流
package com.gk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
 * IO流的基本操作
 * @author GuoKe
 *
 */
public class IOTest01 {
	public static void main(String[] args) {
		
		//1.创建源
		File src = new File("C:/Users/GK/Desktop/aa.txt");
		InputStream is = null;
		try {
			//2.选择流
			is = new FileInputStream(src);
			//3.操作
			int temp;
			while((temp=is.read()) != -1) {
				System.out.print((char)temp);//不使用temp变量时只间隔打印字符,因为进行了两次is.read()操作
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(is!=null) {
					//4.释放资源
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
	}
}

package com.gk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
 * 使用字节数组来读取文件
 * @author GuoKe
 *
 */
public class IOTest02 {
	public static void main(String[] args) {
		//1.创建源
				File src = new File("C:/Users/GK/Desktop/aa.txt");
				InputStream is = null;
				try {
					//2.选择流
					is = new FileInputStream(src);
					//3.操作
					byte[] flush = new byte[1024*10];//缓冲容器
					int len = -1;//接收长度
					while((len=is.read(flush)) != -1) {
						String str = new String(flush,0,len);
						System.out.print(str);
					}
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally {
					try {
						if(is!=null) {
							//4.释放资源
							is.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				
	}
}

  1. OutputStream–
package com.gk;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
 * 使用字节数组的方式写入字符串
 * @author GuoKe
 *
 */
public class IOTest03 {

	public static void main(String[] args) {
		
		File dest = new File("C:/Users/GK/Desktop/f.txt");
		
		OutputStream os = null;
		
		try {
			os = new FileOutputStream(dest,true);
			
			String str = "hello";
			byte[] datas = str.getBytes();
			os.write(datas,0,datas.length);
			os.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				if (os != null) {//alt+shift+z
					os.close();
				} 
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

}

package com.gk;

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;
/**
 * 使用InputStream和OutputStream进行文件的拷贝
 * @author GuoKe
 *
 */
public class IOTest4 {

	public static void main(String[] args) {
		cpFile("C:/Users/GK/Desktop/test.jpg","C:/Users/GK/Desktop/test1.jpg");
		
	}

	public static void cpFile(String srcPath, String destPath) {
		File src = new File(srcPath);
		File dest = new File(destPath);
		
		InputStream is = null;
		OutputStream os = null;
		
		try {
			is = new FileInputStream(src);
			os = new FileOutputStream(dest);
			
			byte[] datas = new byte[10];
			int len = -1;
			while((len=is.read(datas)) != -1) {//不要忘记是读一个字符数组is.read(datas)
				os.write(datas,0,len);
			}
			os.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				if (os != null) {
					os.close();
				} 
			} catch (Exception e) {
				e.printStackTrace();
			}
			try {
				if (is != null) {//先打开的流后关闭
					is.close();
				} 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}

  1. Reader–字符输入流
package com.gk;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
 * 使用Reader来读取文本
 * @author GuoKe
 *总结:字节流处理中英混合的文本时容易因为编码/解码时字节数不同而产生乱码,同时字符流也不能使用不同的字符集
 *(例如utf-8>>gbk)不然也会乱码
 */
public class IOTest5 {
	public static void main(String[] args) {
		//1.创建源
				File src = new File("C:/Users/GK/Desktop/aa.txt");
				Reader rd = null;
				try {
					//2.选择流
					rd = new FileReader(src);
					//3.操作
					char[] flush = new char[2];//缓冲容器
					int len = -1;//接收长度
					while((len=rd.read(flush)) != -1) {
						String str = new String(flush,0,len);
						System.out.print(str);
					}
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally {
					try {
						if(rd!=null) {
							//4.释放资源
							rd.close();
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				
	}
}

  1. Writer–
package com.gk;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
 * 使用Writer写入字符串
 * @author GuoKe
 *
 */
public class IOTest6 {

	public static void main(String[] args) {
		
		File dest = new File("C:/Users/GK/Desktop/f.txt");
		
		Writer wt = null;
		
		try {
			wt = new FileWriter(dest);
			
//			String str = "hello + 00中文";
			
			
//			char[] datas = str.toCharArray();//字符串到字符数组
//			wt.write(datas,0,datas.length);
			
			
//			wt.write(str, 0, str.length());
			
			wt.append("aaaaaa").append("2222");
			wt.flush();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally {
			try {
				if (wt != null) {//alt+shift+z
					wt.close();
				} 
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

}

  1. Closeable–关闭流接口
    o
  2. Flushable–刷新流接口
    o
  3. Serializable–序列化接口
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值