(copyfile)复制粘贴文件的三种方式

1 篇文章 0 订阅
1 篇文章 0 订阅

一:复制粘贴(从一个文本文件或其他,粘贴到另一个文件上)

二:file的操作 :File file=new File(“文件的位置”);

line :bufferreader
char: filereader
byte: fileinputstream
1:byte

/*
这是一个可以传所有类型的的复制粘贴的方式,通过字节流
 */
public class CopyFileStream {
public static void main(String[] args) {
	FileInputStream fis = null;
	FileOutputStream fos = null;
	String filename = "D:\\练习文件\\abc\\";
  //首先你必须有我这几个文件夹,和文件hello.java
	try {
		fis = new FileInputStream(filename + "hello.java");
		fos = new FileOutputStream(filename + "newhello.txt",true);
      //这儿是true,不覆盖的意思(原来的内容会保存)
      //如果是false,就是覆盖原来的文本
		byte[] buf = new byte[1024];// 缓冲数据	
      //通过1024这么长的字节来读写
		while (fis.read(buf) != -1) {
			fos.write(buf);
		}
		fos.flush();
      //		这里还是冲刷一下吧,万一没进入文本就不好了
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
      //      必须关一下资源,不然你的电脑可能会崩溃
		try {
			if (fis != null) {
				fis.close();
			}
			if (fos != null) {
				fos.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	}}}

提取主要代码:

byte[] buf = new byte[1024];// 缓冲数据
			
			while (fis.read(buf) != -1) {
				fos.write(buf);
			}
fos.flush();

2:char

public class CopyFileChar {
public static void main(String[] args) {

FileReader fReader=null;
FileWriter fWriter=null;
String file="D:\练习文件\abc\";
char[] ch=new char[1024];
try {
    char[] ch=new char[1024];
	fReader=new FileReader(file+"hello.java");
	fWriter=new FileWriter(file+"otherhello.txt",false);
  //false是覆盖的意思,
  //如果这儿是true,不覆盖的意思(原来的内容会保存)
	
  //通过字符,读写
  while (fReader.read(ch)!=-1) {
		fWriter.write(ch);	 
	}  
	fWriter.flush();
} catch (IOException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}finally {
//		必须关一下资源,不然你的电脑可能会崩溃
	try {
		if (fReader!=null) {
			fReader.close();
		}
		if (fWriter!=null) {
			fWriter.close();
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}}}}

提取主要代码:

 char[] ch=new char[1024];
 fReader=new FileReader(file+"hello.java");
 fWriter=new FileWriter(file+"otherhello.txt",false);
      while (fReader.read(ch)!=-1) {
           fWriter.write(ch); 
      }
      fWriter.flush();

3:line

读取一行很常见,通过转型从fileinputstream转型到bufferedreader
BufferedReader bfrReader=new BufferedReader(
                    new InputStreamReader(new FileInputStream(new File(“d:\text”))));

public class CopyFileBuffer {
public static void main(String[] args) {
	BufferedReader bReader = null;
	BufferedWriter bWriter = null;
	String file = "D:\\练习文件\\abc\\";
	String line = null;
	try {
		bReader = new BufferedReader(new FileReader(file + "hello.txt"));
		bWriter = new BufferedWriter(new FileWriter(file + "atherhello.txt"));
		int linnNum=1;
      //通过一行来读取文件,
		while ((line = bReader.readLine()) != null) {
			bWriter.write(linnNum+":\t"+line);
			linnNum++;
			bWriter.newLine();		}
		bWriter.flush();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
		try {
			if (bWriter != null) {
				bWriter.close();
			}
			if (bReader != null) {
				bReader.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	}}}

操作file文件的方法:常用的方法

  • File: 表示文件或者目录
    • delete()
    • exist()
    • isFile()
    • isDirctory
    • createNewFile
    • length
    • mkdir / mkdirs
    • renameTo
    • listFiles
  • IO
    • 概念
    • 分类
      • 输入(InputStream,Reader) 输出(OutputStream,Writer)
      • 字节流(Stream) 字符流(Reader/Writer)
      • 节点流 、 处理流(BufferedXXX)
      • 转换流 InputStreamReader OutputStreamWriter

将数据写入.txt中,或读取 .txt中:
https://blog.csdn.net/nickwong_/article/details/51502969
文件是否存在:
https://blog.csdn.net/D578332749/article/details/81676819

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值