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

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

二: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

    原文作者:wangyer_
    原文地址: https://blog.csdn.net/m0_37121708/article/details/88699315
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞