java 文件的读写操作

package temp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class FileDemo {

	public static void main(String[] args) {
	

		
		 try{
//	            /**//*查找目录,如果不存在,就创建*/
	            File dirFile = new File("e:\\test");
	            if(!dirFile.exists()){
	                if(!dirFile.mkdir())
	                    throw new Exception("目录不存在,创建失败!");
	            }
	            /**//*查找文件,如果不存在,就创建*/
	            File file = new File("e:\\test\\text1.txt");
	            if(!file.exists())
	                if(!file.createNewFile())
	                    throw new Exception("文件不存在,创建失败!");
//	            Date date = new Date(file.lastModified());
//	            String time =  SimpleDateFormat.getTimeInstance(DateFormat.LONG,Locale.CHINA).format(date);
//	            System.out.println(time);
	            byte b[] = new byte[1024];
	            FileInputStream fs =new FileInputStream(file);
	            while(fs.read()!= -1)
	            {
	            	fs.read(b);
	            }
	            
	            String s = new String(b);
	            fs.close();
	            System.out.println(s);
	            
	      }catch(Exception e){
	            System.out.println(e.getMessage());

		 }

         


}
	
	
}

 

1、必须创建目录之后才能创建文件。

2、目录被看做是目录列表的文件。

3.Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种所以一共四个):InputStream,OutputStream,Reader,Writer。Java中其他多种多样变化的流均是由它们派生出来的:

42134

42135

42136

 

42137

 

浅谈Java的输入输出流 比较好的文章。

转载于:https://www.cnblogs.com/phpzxh/archive/2010/02/06/1664999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值