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的输入输出流 比较好的文章。

posted @ 2010-02-06 15:46  phpzxh  阅读(891)  评论(0编辑  收藏  举报