java学习记录分享(十)

File打印文件夹-实战

	在java.io中有一个File类,其中有很多API,下面将根据这些API进行实战
	exists()/isFile()/isDirectory()/isHidden()/lastModified()
	canRead()/canWrite()
	createNewFile()/createTempFile		创建的是文件
	delete()/deleteOnExit()
	getAbsolutePath()/getName()/getPath()
	getParent()/getParentFile()
	list()/listFiles()
	mkdir()/mkdirs()		创建的是文件夹
import java.io.File;
		public class ShowDirectoryInfo{
			File file = new File("C:/Program Files");		//c盘中默认存在文件
			initTab = getTabCount(file);					//计算要展示的文件夹前的空格数,美化缩进。				
			showFileInfo(file);						
		}
		private static int initTab=0;			
		public static void showFileInfo(File file){
			String fileName = file.getName();
			printTab(getTabCount(File));					//打印缩进
			System.out.println(fileName);					//在缩进后打印文件名
			if(file.isDirectory()){							//判断该文件是不是有子文件夹
				File[] childFiles=file.listFiles();
				for(int i = 0;i<child.length;i++){
					File childFile=childFile[i];
					showFileInfo(childFile);				//递归思想的使用
				}
			}
		}
		private static void printTab(int tabCount){			//打印缩进的方法
			for(int i = 0;i<tabCount-initTab;i++){
					System.out.print("\t");
				}
		}
		private static int getTabCount(File file){			//得到应该打印缩进个数的方法
			String absPath=file.getAbsolutePath();
			return absPath.spilt("\\\\").length;
		}

字节流

	当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作。当把持久设备上的数据读取
到内存中的这个动作称为输入(读)Input操作。因此我们把这种输入和输出动作称为IO操作。
流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
输入流代码演示
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.IOException;

public class InputStreamTest{
	public static void main(String[] args) {
		InputStream is=null;
	try {
	 	is = new FileInputStream("D:/hello.txt");
		byte[] bytes=new byte[10];
		int len=0;
		while((len=is.read(bytes))!=-1){
			System.out.println(new String(bytes,0,len));
	}
		}catch(FileNotFoundException e) {
		e.printStackTrace();
		}catch(IOException e) {
		e.printStackTrace();
		}finally {
			if(is!=null) {
				try {
					is.close();//使用io流记得关闭资源
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
}		//IO流代码中需要大量的try-catch,因为异常出现的频率很高

	实战,通过io流来复制图片
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 IOTest1 {
	public static void main(String[] args) {
		InputStream is=null;;
		OutputStream os=null;
		try {
			is=new FileInputStream("D:/dog.jpg");
			os=new FileOutputStream("D:/dog.jpg2");
			int len=-1;
			byte[] bytes=new byte[1024];
			while((len=is.read(bytes))!=-1) {
				os.write(bytes, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(is!=null) {
					is.close();
					}
				if(os!=null) {
					os.close();
						}
			}catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

字符流

	FileWriter: 写入文件 
	FileReader: 读取文件
	文件中存在字符的时候使用字符流传送数据可能会出现乱码
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;

public class IOTest2{
	public static void main(String[] args) throws Exception {
		//读取字符。中文是两个字符,可能会出现乱码,使用Reader
		Reader reader = new FileReader("D:/hello.txt");
		char[] chars = new char[2];
		int len = -1;
		while((len = reader.read(chars))!=-1);{
			System.out.println(new String(chars,0,len));
		}
		reader.close();
		
		//写入字符
		Writer writer = new FileWriter("D:/hi.txt");
		writer.write("我要写入这几个字符:dwqdwddqd");
		writer.close();
	}
}

转换流和缓冲流

public class IOTest3 {
		public static void main(String[] args) throws Exception {
			InputStream is=new FileInputStream("D:/hi.txt");	//字节流
			Reader reader=new InputStreamReader(is);			//转换流
			BufferedReader br=new BufferedReader(reader);		//缓冲流
			
			BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream("D:/hi.txt")));
			//效果与前面三行一样
			
			String str = null;
			while((str=br.readLine())!=null){
				System.out.println(str);
			}
			while((str=br1.readLine())!=null){
				System.out.println(str);		//打印结果相同
			}
		}
}

打印流

	包括PrintStream和PrintWriter,想要熟练掌握IO流就需要多看帮助文档,看各种API的用法。
public class IOTest4 {
	public static void main(String[] args) {
		PrintStream ps=null;
		try {
			ps=new PrintStream("D:/ps.txt");
			ps.print("hello");
			ps.print(" world");
			ps.println("!");
			ps.printf("%s%d","大家好我今年",20);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(ps!=null) {
				ps.close();
			}
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值