Java 文件操作

 java io 操作是基础中的基础,下面列出常见的IO 操作

1,读取本地Txt 文档

File file = new File("e://****.txt");
		FileReader fr = null;
		BufferedReader br = null;
		try {
			 fr = new FileReader(file);
			 br = new BufferedReader(fr);
			String line = "";
			StringBuffer sb = new StringBuffer();
			
			while((line = br.readLine())!= null)
				{
				sb.append(line+"\n");
				System.out.println(line);
				}
			//System.out.println(sb.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
				fr.close();
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

2,写字符到本地文件

private static final String SEPARATOR = System.getProperty("file.separator");
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			File file = new File("f:"+SEPARATOR+"File_Test"+SEPARATOR+"test.txt");
			if(!file.exists())
			{
				file.createNewFile();
			}
			FileWriter fw = new FileWriter(file);
			fw.append("charlie");
			fw.append("\n");
			fw.append("only");
			fw.append("have fun");
			fw.flush();
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

3,读写文件的五种写法

/**
	 * 读写一个文件测试
	 * 
	 * 输入输出 都用 Buffer
	 */
	
	public static void copyFileByBuffer()
	{
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			 fis = new FileInputStream("e://Git权威指南.pdf");
			 bis = new BufferedInputStream(fis);
			 fos = new FileOutputStream("D://git.pdf");
			 bos = new BufferedOutputStream(fos);
			 int length = 0;
			 byte[] bytes = new byte[1024];
			 long start = System.currentTimeMillis();
			 while((length = bis.read(bytes))!= -1)
			 {
				 bos.write(bytes, 0, length);
			 }
			 System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
			 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
			
				fis.close();
				bis.close();
				bos.close();
				fos.close();
				
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * 输出 buffer
	 */
	public static void copyFileByBufferOutput()
	{
		FileInputStream fis = null;
		//BufferedInputStream bis = null;
		FileOutputStream fos = null;
		BufferedOutputStream bos = null;
		try {
			 fis = new FileInputStream("e://Git权威指南.pdf");
			// bis = new BufferedInputStream(fis);
			 fos = new FileOutputStream("D://git.pdf");
			 bos = new BufferedOutputStream(fos);
			 int length = 0;
			 byte[] bytes = new byte[1024];
			 long start = System.currentTimeMillis();
			 while((length = fis.read(bytes))!= -1)
			 {
				 fos.write(bytes, 0, length);
			 }
			 System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
			 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
			
				fis.close();
				bos.close();
				fos.close();
				
			
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * 输出 buffer
	 */
	public static void copyFileByBufferedInputStream()
	{
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		FileOutputStream fos = null;

		try {
			 fis = new FileInputStream("e://Git权威指南.pdf");
			 bis = new BufferedInputStream(fis);
			 fos = new FileOutputStream("D://git.pdf");
			 int length = 0;
			 byte[] bytes = new byte[1024];
			 long start = System.currentTimeMillis();
			 while((length = bis.read(bytes))!= -1)
			 {
				 fos.write(bytes, 0, length);
			 }
			 System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
			 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
			
				fis.close();
				fos.close();
				
		
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * NoBuffer
	 */
	public static void copyFileByNoBuffer()
	{
		FileInputStream fis = null;
		FileOutputStream fos = null;

		try {
			 fis = new FileInputStream("e://Git权威指南.pdf");
			 fos = new FileOutputStream("D://git.pdf");
			 int length = 0;
			 byte[] bytes = new byte[1024];
			 long start = System.currentTimeMillis();
			 while((length = fis.read(bytes))!= -1)
			 {
				 fos.write(bytes, 0, length);
			 }
			 System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
			 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
			
				fis.close();
				fos.close();
				
		
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
	
	/**
	 * NoBuffer
	 */
	public static void copyFileByReader()
	{
		FileReader fr = null;
		FileWriter fw = null;

		try {
			fr = new FileReader("e://签名信息.txt");
			fw = new FileWriter("D://git.txt");
			 int length = 0;
			 char str = ' ';
			 char[] chars = new char[1024];
			 byte[] bytes = new byte[1024];
			 long start = System.currentTimeMillis();
			 while((length =  fr.read(chars))!= -1)
			 {
				 fw.write(chars, 0, length);
			 }
			 System.out.println("复制文件所需的时间:" + (System.currentTimeMillis() - start)+"ms"); // 平均时间约 200 多毫秒
			 
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			
			try {
			
				fr.close();
				fw.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、付费专栏及课程。

余额充值