博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java——IO流01
阅读量:4583 次
发布时间:2019-06-09

本文共 3313 字,大约阅读时间需要 11 分钟。

移动文件有一种简单方法,不需要复制文件再删除文件。

package com.unir.test01;import java.io.File;import java.io.IOException;public class Test02 {     public static void main(String[] args) throws IOException {        //创建文件        File f=new File("d:\\developer\\6.txt");        f.renameTo(new File("E:\\英雄时刻"));//移动到e盘        f.delete();//删除     }}

 

 

 

 

使用FileInputStream类的read(byte[])方法和FileOutputStream类的write(byte[])方法实现文件移动。

       已知文件:d:\\developer\\56.txt

       目标地址:e:\\英雄时刻\\56.txt

package com.unir.test01;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class Test01 {	/*	已知文件:d:/developer/12345.txt		目标地址:e:/英雄时刻/12345.txt	*/     public static void main(String[] args) throws IOException {    	 //创建文件    	 File f=new File("d:\\developer\\12345.txt");    	 //创建字符    	 String txt="nice.";    	 byte[] b=txt.getBytes();    	 FileOutputStream fos=new FileOutputStream(f);    	 fos.write(b);    	     	     	//创建输入流        FileInputStream input = new FileInputStream("d:/developer/12345.txt");        //创建输出流        FileOutputStream output = new FileOutputStream("e:/英雄时刻/12345.txt");        int len = 0;        byte[] buf = new byte[1024];        if((len = input.read(buf)) > -1){            output.write(buf, 0 , len);        }                              fos.close();        input.close();        output.close();        f.delete();             }     }

  

使用BufferedInputStream类的read方法和BufferedOutputStream类的write方法实现文件移动。

 

   已知文件:d:\\developer\\56.txt

      目标地址:e:\\英雄时刻\\56.txt

package com.unir.test01;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;public class Test04 {	   public static void main(String[] args) throws IOException {	    	FileInputStream f=new FileInputStream("D:\\developer\\9.txt");	    	InputStreamReader i=new InputStreamReader(f,"utf-8");	    	FileOutputStream f1=new FileOutputStream("e:\\英雄时刻\\9.txt",true);	    	BufferedOutputStream b=new BufferedOutputStream(f1);	    				        int len = 0;	        byte[] buf = new byte[1024];	        if((len = i.read()) != -1){	            b.write(buf, 0 , len);	        }	        b.close();	        i.close();	        File file=new File("D:\\developer\\9.txt");	        file.delete();	     }}

  从键盘读入“Java IO流的分类”,并将这些文字写入文件d:\\developer\\9.txt,然后将该文件复制到e:\\英雄时刻\\9.txt

package com.unir.test01;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;public class Test04 {	   public static void main(String[] args) throws IOException {	    	FileInputStream f=new FileInputStream("D:\\developer\\9.txt");	    	InputStreamReader i=new InputStreamReader(f,"utf-8");	    	FileOutputStream f1=new FileOutputStream("e:\\英雄时刻\\9.txt",true);	    	BufferedOutputStream b=new BufferedOutputStream(f1);	    				        int len = 0;	        byte[] buf = new byte[1024];	        if((len = i.read()) != -1){	            b.write(buf, 0 , len);	        }	        b.close();	        i.close();	        File file=new File("D:\\developer\\9.txt");	        file.delete();	     }}

  

中。

posted on
2018-11-13 20:04 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/money131/p/9954373.html

你可能感兴趣的文章
探讨在线支付平台支付接口的设计
查看>>
【设计模式】常用设计模式总结
查看>>
.NET中的六个重要概念
查看>>
二十九、简谈设计模式
查看>>
js中数组的检测方法
查看>>
[译]GotW #6a: Const-Correctness, Part 1
查看>>
JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)...
查看>>
用Python学分析 - 单因素方差分析
查看>>
2018个人年终总结
查看>>
[编辑排版]小技巧---markdown 转 richText
查看>>
JSON_UNESCAPED_UNICODE
查看>>
bug解决思路
查看>>
Oracle没有WM_CONCAT函数的解决办法
查看>>
消息中间件——RabbitMQ(四)命令行与管控台的基本操作!
查看>>
Eclipse 写代码是自动重启服务
查看>>
3.8 spring - AbstractBeanDefinition 介绍
查看>>
如何在Visual Studio里面查看程序的汇编代码?
查看>>
解决IE11只能用管理员身份运行的问题
查看>>
android学习-LocationManager(一)-
查看>>
Linux安装单机solr
查看>>