通过几个小实例来抛析文件IO的读写

第一个:小实例对文件的基本操作和经常遇到的相关APi的简介

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//创建文件对象
		//存放文件的基本信息
		File file = new File("E:/a.txt");
		//测试文件是否存在
		System.out.println(file.exists());
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//得到文件大小,以字节为单位
		System.out.println(file.length());
		//得到文件最近一次的修改日期
		System.out.println(file.lastModified());
		//简单的日期格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		//创建日期对象
		Date date = new Date(file.lastModified());
		//执行格式化操作
		System.out.println(sdf.format(date));
		
		File director = new File("E:/my");
		if(!director.exists()){
			director.mkdir(); //创建文件夹
		}
		
		//File对象既可以表示文件也可以表示文件夹
		//得到my文件夹下所有文件对象。
		File files[] = director.listFiles();
		for (int i = 0; i < files.length; i++) {
			System.out.println(files[i].getName()); //得到文件对象的文件名
		}
		
	}

}
第二个小实例:对io流的操作,字符流和字节流的读写


public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		File file = new File("E:/a.txt");
		//使用字符流读取文本文件中的内容
		try {
			FileReader fileReader = new FileReader(file);
			char c[] = new char[2];
			StringBuffer sb = new StringBuffer();
			int len = 0;
			//将读到的内容存放在字符数组c中
			//len存放读到的字符个数,当读到最后时,得到-1。
			while((len = fileReader.read(c)) != -1){
				String s = new String(c, 0, len);
				sb.append(s);
				System.out.println("len = " + len);
				System.out.println("s = " + s);
			}
			System.out.println(sb.toString());
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		//字符流写数据
		File fileB = new File("E:/b.txt");
		if(!fileB.exists()){
			try {
				fileB.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			FileWriter fileWrite = new FileWriter(fileB);
			String s = "abcccccc\r\nabc";
			//写到缓冲区
			fileWrite.write(s);
			//刷新
			fileWrite.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//字节流读数据
		byte b[] = null;
		File filePic = new File("E:/emoticon_01.png");
		try {
			FileInputStream fis = new FileInputStream(filePic);
			//fis.available() 有效字节数
			System.out.println(fis.available());
			b = new byte[fis.available()];
			fis.read(b);
			System.out.println(fis.available());
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//字节流写数据
		File fileCopy = new File("E:/my/emoticon_01.png");
		if(!fileCopy.exists()){
			try {
				fileCopy.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			FileOutputStream fps = new FileOutputStream(fileCopy);
			fps.write(b);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	

}

第三个小实例:缓冲读写
public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		BufferedReader 缓冲读(一行一行的读)
		
		File file = new File("E:/a.txt");
		
		try {
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			String line = null;
			StringBuffer sb = new StringBuffer();
			while((line = br.readLine()) != null){
				sb.append(line+"\n");
			}
			System.out.println(sb.toString());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//BufferedWriter 缓冲写(一行一行的写)
		
		File fileW = new File("E:/c.txt");
		try {
			FileWriter fw = new FileWriter(fileW);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write("line\r\n");
			bw.write("one");
			//关闭流 (最先打开的流,最后关闭)
			bw.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

第四个小实例:字节流和字符流的转换

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//字节流和字符流的转换
		
		//使用字节流读文本数据
		try {
			FileInputStream fis = new FileInputStream(new File("E:/a.txt"));
			InputStreamReader isr = new InputStreamReader(fis, "utf-8");
			char c[] = new char[1024];
			int len = 0;
			while((len = isr.read(c)) != -1){
				String s = new String(c, 0, len);
				System.out.println(s);
			}
			
//			byte b[] = new byte[fis.available()];
//			fis.read(b);
//			
//			//将字节数组转化为字符串
//			String s = new String(b);
//			System.out.println(s);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


第五个小实例:对实现序列化接口的类进行读写,下面有两个类,一个book类,一个测试类,以及注释供大家参考

book.java

//当前类实现序列化接口。
//序列化接口用来压缩对象
public class Book implements Serializable {
	private String name;

	public Book(String name) {
		// TODO Auto-generated constructor stub
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
}

下面是测试类

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("E:/object.txt");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			FileOutputStream fos = new FileOutputStream(file);
			//写对象字节流
			ObjectOutputStream oss = new ObjectOutputStream(fos);
			Book book = new Book("think in java");
			oss.writeObject(book);
			oss.close();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			FileInputStream fis = new FileInputStream(file);
			//读对象字节流
			ObjectInputStream ois = new ObjectInputStream(fis);
			Book book = (Book)ois.readObject();
			System.out.println(book.getName());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值