java_IO 学习日志

对文件的基础创建

原因

	文件的查找以及路径的正确输入方式都将成为是否找得到文件的关键

java中的两种路径书写方式

File file = new File("D:/java/Workspace/TextIOstream/IOTry.jpg");//这是第一种,也是推荐使用的路径方式

如果所需文件就在本目录下则可以使用直接标记的方式

File file = new File("IOTry.jpg");//这两种方式都是可行的
File file = new File("D:\\java\\Workspace\\TextIOstream\\IOTry.jpg");//这种双左划线的使用方式不推荐

File类的方法应用

直接代码:

/**
		 * 对IO中的文件方法路径进行测试
		 * getPath():获得路径
		 * getName():获得名字
		 * getAbsoludePath():获得绝对路径
		 * getPerent():获得父类路径,如果没有则返回null
		 */
		File file = new File("D:/java/Workspace/TextIOstream/IOTry.jpg");//打开相应文件
		System.out.println(file.getPath());
		System.out.println(file.getName());
		System.out.println(file.getAbsolutePath());
		System.out.println(file.getParent());
		System.out.println("++++++++++++++++++++++++++以上分行++++++++++++++++++++++++++++++++++++");
		/**
		 * 对一下的方法进行测试
		 * isFile()
		 * isDirectory()
		 * length()
		 * createNewFile()
		 * delete()
		 */
		File src = new File("D:/java/Workspace/TextIOstream/1.txt");
		System.out.println(src.createNewFile());//用来判断是否当前文件夹是否存在文件新的创建
		System.out.println("isFile:"+src.isFile());
		System.out.println("isDirectory:"+src.isDirectory());
		System.out.println("Length:"+src.length());//它只能显示的是文件的大小
		System.out.println("IOTry.length:"+file.length());
		src.delete();//删除文件
		System.out.println("++++++++++++++++++++++++++以上分行++++++++++++++++++++++++++++++++++++");
		
		/**
		 * 对创建文件的mkdir 和 mkdirs的使用说明:
		 * 对列出下级菜单list():列出下级名称
		 * listFile()列出下级的File对象
		 * 列出所有盘符.listRoot()
		 * 
		 */
		File src_1 = new File("D:/java/Workspace/TextIOstream");
//		System.out.println(src_1.getPath());
		src_1.mkdirs();//创建文件夹的话是不能用file来创建的,可以通过mkdirs来创建.
//		System.out.println(src_1.getPath());
		String[] str = src_1.list();//list是搜索当前目录下的子目录名字
		for(String s : str) {
			System.out.println(s);
		}
		

FileInputStream 和 FileOutputStream 的分开使用

package com.text.ioStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * IO流的标准格式使用
 * 1.创建源
 * 2.选择流
 * 3.操作
 * .read()方法为使用的自动坐标下移从文件的第一个位置开始.
 * 4.关闭流
 * @author 墨雷
 *
 */
public class IOStreamMain {
	public static void main(String[] args) {
		// create new file 
		File file = new File("ioText.txt");
		InputStream io = null;
		try {
			//选择流
			io = new FileInputStream(file);
			//操作流(逐字读取)
//			int i;
//			while ((i = io.read()) != -1) {
//				System.out.println((char)i);
//			}
			//操作流(使用容器进行缓冲处理)
			byte[] flush = new byte[1024*1];//作为缓冲所使用的字节数组
			int len = -1;//定义长度用于循环read是否为null
			while((len = io.read(flush)) != -1) {
				String str = new String(flush,0,flush.length);
				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 {
			try {
				io.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		System.out.println("<--------------------------------------outputStream-------------------------------------------------->");
		File file_1 = new File("ioOutput.txt");
		//选择对应的流对象
		FileOutputStream op = null;
		try {
			op = new FileOutputStream(file_1);//在此处如果加入一个布尔值类型则表示是否为末尾追加,默认为false.
			//创建缓冲流
			String str_1 = new String("nice everyday I hope I will make more money");//用于转换的字节数
			byte[] flush_1 = new byte[1024*1];
			flush_1 = str_1.getBytes();
			op.write(flush_1);
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				op.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

文件的拷贝

文件的拷贝是在Input和Output的基础上利用先提取再存的相应的文件中的,需要注意的是先打开的流要后关闭,后打开的流要先关闭。

package CopyIOStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 对文件的copy的练习
 * @author 墨雷
 *
 */
public class CopyMain {
	public static void main(String[] args) {
		//选择源-->此处选择打开两个源,即目标源和开始源
		File src = new File("IOTry.jpg");//被选图片地址
		File end = new File("copyIOTry.jpg");//被copy的图片位置
		
		//打开流
		FileInputStream in = null;
		FileOutputStream out = null;//此处只是先创建变量,不做其他事情
		//操作
		try {
			in = new FileInputStream(src);
			out = new FileOutputStream(end);//catch
			//建立一个容器用来存储字节,并记录长度
			byte[] flush = new byte[1024];
			int length;
			while((length = in.read(flush))!= -1) {//读取
				out.write(flush,0 , length);//写入
			}
			out.flush();
			
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			//关闭相应的流
			try {
				if(in != null) {
					in.close();					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(in != null) {
					out.close();					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


}

此处的先打开为使用到该实例时的“打开”而并非创建对象的先后,此处先关闭in是因为它调用时是比out先被调用的

.Reader 和 .Writer 文件字符输入输出流

该流与上面的stream十分类似,都是对文件进行操作,主要区别在于一个是字节,另外一个是字符。字符流对于txt类型的文件可以直接读取而不用编码,译码,但是不能用于图片等类型不然会破坏文件。而字节流则可以,但字符的编译和解码是需要的。
字节流写法:

byte[] flush = new byte[1024*1];//作为缓冲所使用的字节数组

字符流写法

char[] flush = new char[1024*1];//作为缓冲所使用的字符数组

因为它可以直接导入字符,所以在使用Writer时可以直接使用如下方式:
截图

字节数组流(ByteArrayInputStream & ByteArrayOutputStream)

ByteArrayInputStream即字节数组的输入输出
想知道具体的可以点击此处
在这里插入图片描述
这里的ByteArrayOutputStream不需要加源,由内部容器进行存储。

对接流

理论图

package CopyIOStream;

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;

public class DockStream {
	public static void main(String[] args) {
		
		byte[] date = fileToArray("IOTry.jpg");
		arrayToFile(date, "Array.jpg");
	}
	/**
	 * 由文件到字节数组
	 * 先获取文件源
	 */
	public static byte[] fileToArray(String fileadd) {
		
		FileInputStream src = null;
		ByteArrayOutputStream bo = null;
		int length = 0;
		try {
			src = new FileInputStream(fileadd);
			bo = new ByteArrayOutputStream();
			byte[] flush = new byte[1024*10];
			while((length = src.read(flush))!= -1) {
				bo.write(flush, 0, length);
			}
			bo.flush();
			return bo.toByteArray();
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}catch(IOException e) {
			e.printStackTrace();
		}finally {
			if(src != null) {
				try {
					src.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static void arrayToFile(byte[] date,String address) {
		ByteArrayInputStream ip = new ByteArrayInputStream(date);
		FileOutputStream saveFile = null;
		try {
			saveFile = new FileOutputStream(address);
			byte[] copy = new byte[1024];
			int length = 0;
			while((length = ip.read(copy))!=-1) {
				saveFile.write(copy, 0, length);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			if(saveFile != null) {
				
				try {
					saveFile.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}

IO 工具类

自己写。。。。。
注意Closeable的使用和try…with…resource

字节缓冲流(BufferedInputStream & BufferedOutputStream)

为更好地了解字节缓冲流,建议先理解装饰器
↓为我找的一个人写的介绍
装饰器的说明
在这里插入图片描述
使用缓冲流可以使对文件的操作的性能加快。

//打开流
		InputStream in = null;
		OutputStream out = null;//此处只是先创建变量,不做其他事情
		//操作
		try {
			in = new BufferedInputStream(new FileInputStream(src));
			out = new BufferedOutputStream(new FileOutputStream(end));//catch
			......

↑-----------------------------------------------------------

字符输入(输出)流

在这里插入图片描述

转换流

在这里插入图片描述

		try{
			BufferedReader ip = new BufferedReader(new InputStreamReader(System.in));
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
			
			String s = "" ;
			while(!s.equals("exit")) {
				s = ip.readLine();
				bw.write(s);
				bw.newLine();
				bw.flush();
			}
			
		}catch (IOException e) {
			// TODO: handle exception
		}finally {
			
		}

数据流


			ByteArrayOutputStream o_1 = new ByteArrayOutputStream();
			DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(o_1));
			dos.writeChar(0);
			dos.writeInt(2);
			byte[] flushs = o_1.toByteArray();
			ByteArrayInputStream i_1 = new ByteArrayInputStream(flushs);
			DataInputStream dis = new DataInputStream(new BufferedInputStream(i_1));
			//读入和写出的顺序要一致
			dis.readChar();
			dis.readInt();
			
			

对象流ObjectInputStream和ObjectOutputStream

此处的能够实现对象流的输出的只能被序列化后的对象,而要实现序列化则需要实现java.io.Serializable接口

形式:
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(“address”)))
对象流->缓冲流->写出流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值