java io入门程序集合

</pre><pre name="code" class="java">
import java.io.*;

import javax.imageio.stream.FileImageOutputStream;
public class ioTest1 {
 public static void main(String []args){
	
	 //创建一个文件对象
	 File f=new File("f:\\myCounter.txt");
	 //得到文件路径
	 System.out.println("文件路径"+f.getAbsolutePath());
	 //得到文件的大小
	 System.out.println("文件大小:"+f.length());
	 //得到文件是否刻度
	 System.out.println("可读"+f.canRead());
	 
	 
	 
	 //创建文件
	 File f=new File("f:\\shj.txt");
	 if(!f.exists()){
		 //可以创建
		 try {
			f.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }else{
		 	//已经存在
		 System.out.println("文件存在了!");
	 }
	 
	 
	 
	 //创建文件夹
	 File f=new File("f:\\ff");
	 if(f.isDirectory()){
		 //存在
		 
	 }else{
		 //创建
		 f.mkdir();
	 }
	 
	 
	 
	 //查看文件夹下面有多少个文件
	 File f=new File("f:\\ff");
	 if(f.isDirectory()){
	    File lists[]=f.listFiles();
	    for(File i : lists){
	    	System.out.println("文件名:"+i);
	    	i.delete();//删除文件,需要路径
	    }
	 }
	 //file 没有读写的能力,所以需要InputStream
	  
	  
	  
	 
	 File f=new File("f:\\ff.txt");
	 FileInputStream fis=null;
	 try {
		 fis=new FileInputStream(f);
		//定义一个字节数组,相当于缓存
		byte []bytes=new byte[1024];
		int n=0;//实际读取到的字节数
		//循环读取
		
			while((n=fis.read(bytes))!=-1){
				//把字节转成String
				String s=new String(bytes,0,n);
				System.out.println(s);	
			}
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		//关闭文件流,必须放这
		try {
			fis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	 //outputStream流的使用
	 
	 File f=new File("f:\\ff.txt");
	 FileOutputStream fos=null;
	 try {
		fos=new FileOutputStream(f);
		String s="孙helloworld\r\n";  //回车换行
		String s1="sdfsfd";
		//byte []bytes=new byte[1024];
		//如果大的话就用,小的话就不用了。
		fos.write(s.getBytes());
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	

	 //拷贝图片
	 //必须用字节流,用字符流会出错。
	 File f=new File("f:\\1.png");
	 FileInputStream fis=null;
	 File f1=new File("f:\\ff\\2.png");
	 FileOutputStream fos=null;
	 try {
		fos=new FileOutputStream(f1);
		 fis=new FileInputStream(f);
		//定义一个字节数组,相当于缓存
		byte []bytes=new byte[1024];
		int n=0;//实际读取到的字节数
		//循环读取
		
			while((n=fis.read(bytes))!=-1){
				//把字节转成String
				//String s=new String(bytes,0,n);
				fos.write(bytes);
			}
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		//关闭文件流,必须放这
		try {
			fis.close();
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 
	 
	 
	 
	 	
	 //把文件拷贝到另一个文件
	 FileReader fr=null;
	 FileWriter fw=null;
	 
	 //创建fr对象
	 try {
		fr=new FileReader("f:\\ff.txt");
		fw=new FileWriter("f:\\ff\\ff.txt");
		char c[]=new char[1024];
		int i=0;
		while((i=fr.read(c))!=-1){
			//方法一
			String s=new String(c,0,i);
			char c1[]=new char[i];
			//System.out.println(s);
			s.getChars(0, i, c1, 0);
			fw.write(c1);
			
			//方法二
			fw.write(c,0,i);
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			fr.close();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	 
	 
	 
	 
	 //为了提高效率引入了缓冲字符流
	 BufferedReader br=null;
	 BufferedWriter bw=null;
	 try {
		 //先创建FileReader对象,再newBufferedReader
		FileReader fr=new FileReader("f:\\ff.txt");
		br=new BufferedReader(fr);
	
		//创建FileWriter对象
		FileWriter fw=new FileWriter("f:\\ff\\aa.txt");
		bw=new BufferedWriter(fw);
		String s="";
		while((s=br.readLine())!=null){
				System.out.println(s+"11");
			    bw.write(s+"\t\n");
		}
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		try {
			br.close();
			bw.close();
		} catch (IOException 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、付费专栏及课程。

余额充值