Java的io流

IO

File类

作用

访问和操作文件的类

创建方式
File file = new File( String pathname );
常用方法
boolean exists( )//判断文件是否存在
boolean isFile( )//判断是否是文件
boolean isDirectory( )//判断是否为文件目录
String getPath( )//获取相对路径
String getAbsolutePath( )//获取绝对路径
String getName( )//获取文件或者目录的名称
boolean delete( )//删除文件或者目录
boolean createNewFile( )//创建新的文件
long  length()//返回文件的长度,字节问单位,如果文件不存在返回的是0L
举例
package jbit.io;

import  java.io.*;
public class FileMethods {
	public static void main(String[] args) {
	    FileMethods2 fm=new FileMethods2();
	    File file=null;
	    file=new File("D:\\myDoc\\test.txt");
	    //fm.create(file);
	    fm.showFileInfo(file);
	    //fm.delete(file); 
   }
   /**
    * 创建文件的方法
    * @param file 文件对象
    */
   public void create(File file){
	   if(!file.exists()){
		   try {
			   file.createNewFile();
			   System.out.println("文件已创建!");
		   } catch (IOException e) {
			   e.printStackTrace();
		   }
	   }
   }
   /**
    * 删除文件
    * @param file 文件对象
    */
   public void delete(File file){
	   if(file.exists()){
		   file.delete();
		   System.out.println("文件已删除!");
	   }
   }
   
   /**
    * 显示文件信息
    * @param file 文件对象
    */
   public void showFileInfo(File file){
	   if(file.exists()){ //判断文件是否存在
		   if(file.isFile()){ //如果是文件
			   System.out.println("名称:" +  file .getName());
		       System.out.println("相对路径: " + file.getPath());
		       System.out.println("绝对路径: " + file.getAbsolutePath());   
		       System.out.println("文件大小:" + file.length()+ " 字节");   
		   } 
		   if(file.isDirectory()){
			   System.out.println("此文件是目录");
		   }
	   }else
		   System.out.println("文件不存在");
   }
}



JAVA流的分类

输入流
FileInputStream、DataInputStream、inputStreamReader、BufferedReader
输出流
FileOutputStream、DataOutputStream、ObjectOutputStream、OutputStreamWriter、ufferedWriter
字节流
FileInputStreamFileOutputStreamDataInputStreamDataOutputStreamObjectOutputStream
字符流
inputStreamReader、OutputStreamWriter、BufferedReader、BufferedWriter

inputStream类

常用方法
int read( )//一个字节一个字节的读取文件中的数据,返回的是读取字节处理成int的数值(需要用char强转),如果读完就是-1;
int read(byte[] b)//从输入流中读取b.length个字节到b中,返回读入缓存区的的总字节数,若读完返回-1
int read(byte[] b,int off,int len)//从输入流中读取最多len个字节到b中,从off位置开始存放,返回读入缓存区的的总字节数,若读完返回-1;
void close( )//关闭流
int available()//可以从输入流中读取的字节数目

FileInputStream类

构造方法
FileInputStream(File file)
FileInputStream(String pathname)	
举例
  public static void main(String args[]) throws Exception {
        int size;
        FileInputStream f1 = new FileInputStream("c:/1.txt");
        
        size = f1.available();
        for (int i = 0; i < size; i++) {
         
            System.out.println((char) f1.read());

        }

    }

OutputStream类

常用方法
void write(int c)//将字节一个一个写入文件的输出流中

void write(byte[] buf)//一次将buf.length个字节写入文件的输出流中

void write(byte[] b,int off,int len)//一次将buf.length个字节写入文件的输出流中,参数off代表着从数组下标off开始,参数len表示最终写入的字节个数

void close()//

FileOutputStream类

构造方法
FileOutputStream (File file)
FileOutputStream(String pathname)	
FileOutputStream(String pathname,boolean append)//是否追加true追加,false覆盖
//创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件
举例
public static void main(String args[]) throws Exception {
        FileOutputStream f = new FileOutputStream("c:/4.txt");
    
        byte aa = 97;
        f.write(aa);
        char bb = 'b';
    
        f.write(bb);

        int cc = 99;
        f.write(cc);
 
    }

Reader类

常用方法
int read( )//读取单个字符。 读取的字符,如果已到达流的末尾,则返回 -1 
int read(byte[] c)//读取一个字符数组读取的字符,如果已到达流的末尾,则返回 -1 c- 目标缓冲区
read(char[] c,int off,int len)//将字符读入数组的某一部分,len为要读取的最多字符数,当然可以比他小
void close( )//关闭流

Writer类

常用方法
write(String str)//将str字符串里包含的字符输出到指定的输出流中
write(String str,int off,int len)//将str字符串里从off位置开始长度为len的字符输出到输出流中
void close()//关闭输出流
void flush()//刷新输出流

FileReader类

构造方法
new FileReader(String pathname);
举例
public static void main(String[] args) {
		//创建 FileReader对象对象.
		Reader fr=null;
		StringBuffer sbf=null;
		try {
			fr = new FileReader("D:\\myDoc\\简介.txt");
			char ch[]=new char[1024];  //创建字符数组作为中转站
			sbf=new StringBuffer();
			int length=fr.read(ch);  //将字符读入数组
	        //循环读取并追加字符
	        while ((length!= -1)) { 
	            sbf.append(ch);   //追加到字符串
	            length=fr.read();
	        }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
   	    } finally{
			try {
				if(fr!=null)
					fr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println(sbf.toString());
	}

BufferedReader

构造方法
Reader fr=new FileReader("C:\\myTest.txt "); 
BufferedReader br=new BufferedReader(fr); 

特殊方法以及优点
br.readLine();//按行去读
//优点:提高字符流读取文本文件的效率
举例
public static void main(String[] args) {
		FileReader fr=null;
		BufferedReader br=null;
		try {
	        //创建一个FileReader对象
	        fr=new FileReader("D:\\myDoc\\hello.txt"); 
	        //创建一个BufferedReader 对象
	        br=new BufferedReader(fr); 
	        //读取一行数据 
	        String line=br.readLine();
	        while(line!=null){ 
	            System.out.println(line);
	            line=br.readLine(); 
	        }
	    }catch(IOException e){
	            System.out.println("文件不存在!");
	    }finally{
	    	 try {
	    	     //关闭 流
	    		if(br!=null)
	    			br.close();
	    		if(fr!=null)
	    			fr.close(); 
	 	     } catch (IOException e) {
				e.printStackTrace();
			 }
	    }
	}

FileWriter类

构造方法
new FileWriter(String pathname); 
举例
public static void main(String[] args) {
		Writer fw=null;
		try {
			  //创建一个FileWriter对象
			  fw=new FileWriter("D:\\myDoc\\简介.txt"); 
			  //写入信息
			  fw.write("我热爱我的团队!"); 	       
			  fw.flush();  //刷新缓冲区
			 
		}catch(IOException e){
			  System.out.println("文件不存在!");
		}finally{
			try {
				if(fw!=null)
					fw.close();  //关闭流
	 	     } catch (IOException e) {
				e.printStackTrace();
			 }
		}
	}

BufferedWriter类

构造方法
FileWriter fw=new
                 FileWriter("C:\\myTest.txt"); 
BufferedWriter bw=new  
                 BufferedWriter(fw);

特殊方法以及优点
 bw.newLine();//换行
//优点:效率高,有效的使用了缓存器将缓存写满以后(或者close以后)才输出到文件中,然而FileWriter是每写一次数据,磁盘就会进行一次写操作,性能差
举例

    public static void main(String[] args) throws IOException {

        FileWriter fw = new FileWriter("buf.txt");

        // 缓冲,将流对象传递给缓冲区
        BufferedWriter bufw = new BufferedWriter(fw);

        for (int i = 0; i < 4; i++) {
            bufw.write("abcde" + i);
            bufw.newLine(); // 跨平台的换行
            bufw.flush(); // 刷新
        }

        // 关闭缓冲区,就已经将流对象一起关闭了
        bufw.close();
    }

DataInputStream类

构造方法
FileInputStream fis=new 
FileInputStream("C:\\HelloWorld.class");
DataInputStream dis=new DataInputStream(fis);

举例
public static void main(String[] args) {
	  FileInputStream fis = null;
      DataInputStream dis = null;
      //创建输入流对象
      FileOutputStream outFile = null;
      DataOutputStream out = null;
	  try {
		  //创建输出流对象
	      fis = new FileInputStream("D:\\myDoc\\图10.2.BMP");
	      dis = new DataInputStream(fis);
	      //创建输入流对象
	      outFile = new FileOutputStream("D:\\myDoc\\temp2.BMP");
	      out = new DataOutputStream(outFile);
	      int temp;
	      //读取文件并写入文件
	      while ( (temp = dis.read()) != -1) {
	        out.write(temp);
	      }
	    }catch (IOException ioe) {
	      ioe.printStackTrace();
	    }finally{
	    	try {
	    		if(dis!=null)
	    			dis.close();
				if(out!=null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}

DataOutputStream类

构造方法
FileOutputStream outFile=new FileOutputStream("C:\\temp.class");
DataOutputStream out=new DataOutputStream(outFile);

举例
public static void main(String[] args) {
	  FileInputStream fis = null;
      DataInputStream dis = null;
      //创建输入流对象
      FileOutputStream outFile = null;
      DataOutputStream out = null;
	  try {
		  //创建输出流对象
	      fis = new FileInputStream("D:\\myDoc\\图10.2.BMP");
	      dis = new DataInputStream(fis);
	      //创建输入流对象
	      outFile = new FileOutputStream("D:\\myDoc\\temp2.BMP");
	      out = new DataOutputStream(outFile);
	      int temp;
	      //读取文件并写入文件
	      while ( (temp = dis.read()) != -1) {
	        out.write(temp);
	      }
	    }catch (IOException ioe) {
	      ioe.printStackTrace();
	    }finally{
	    	try {
	    		if(dis!=null)
	    			dis.close();
				if(out!=null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	    }
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图图大魔王_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值