当前位置: 首页 > news >正文

高端论坛网站建设今天大事件新闻

高端论坛网站建设,今天大事件新闻,企业网站建设的策划书,10个免费网站1、InputStream与OutputStream 创建两个文件 - 源和目标。然后我们从源创建InputStream并使用OutputStream将其写入目标文件进行 java 复制文件操作。 private static void copyFileUsingStream(File source, File dest) throws IOException {InputStream is null;OutputStr…

1、InputStream与OutputStream 

创建两个文件 - 源和目标。然后我们从源创建InputStream并使用OutputStream将其写入目标文件进行 java 复制文件操作。

private static void copyFileUsingStream(File source, File dest) throws IOException {InputStream is = null;OutputStream os = null;try {is = new FileInputStream(source);os = new FileOutputStream(dest);byte[] buffer = new byte[1024];int length;while ((length = is.read(buffer)) > 0) {os.write(buffer, 0, length);}} finally {is.close();os.close();}
}

2、Apache Commons IO FileUtils

copyFile(File srcFile, File destFile)可用于在 java 中复制文件。如果您已经在项目中使用 Apache Commons IO,那么使用它来简化代码是有意义的。它在内部使用 Java NIO FileChannel,因此如果您尚未将其用于其他功能,则可以避免使用此包装器方法。下面是使用apache commons io进行java复制文件操作的方法

private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {FileUtils.copyFile(source, dest);
}

3、Files类的copy()方法在 java 中复制文件

private static void copyFileUsingJava7Files(File source, File dest) throws IOException {Files.copy(source.toPath(), dest.toPath());
}

4、使用BufferedInputStream/BufferedOutputStream高效字节流进行复制文件

private static void bufferedStreamCopyFile(File srcFile, File desFile) throwsIOException {//使用缓冲字节流进行文件复制BufferedInputStream bis = new BufferedInputStream(newFileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(newFileOutputStream(desFile));byte[] b = new byte[1024];Integer len = 0;//一次读取1024字节的数据while((len = bis.read(b)) != -1) {bos.write(b, 0, len);}bis.close();bos.close();
}

5、使用FileReader/FileWriter字符流进行文件复制

注意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂

private static void readerWriterCopyFile(File srcFile, File desFile) throwsIOException  {//使用字符流进行文件复制,注意:字符流只能复制只含有汉字的文件FileReader fr = newFileReader(srcFile);FileWriter fw = newFileWriter(desFile);Integer by = 0;while((by = fr.read()) != -1) {fw.write(by);}fr.close();fw.close();}

6、使用BufferedReader/BufferedWriter高效字符流进行文件复制

意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂

private static void bufferedReaderWriterCopyFile(File srcFile, File desFile)  throwsIOException {//使用带缓冲区的高效字符流进行文件复制BufferedReader br = new BufferedReader(newFileReader(srcFile));BufferedWriter bw = new BufferedWriter(newFileWriter(desFile));char[] c = new char[1024];Integer len = 0;while((len = br.read(c)) != -1) {bw.write(c, 0, len);}//方式二/*String s = null;while((s = br.readLine()) != null) {bw.write(s);bw.newLine();}*/br.close();bw.close();}

7、使用BufferedReader/BufferedWriter高效字符流进行文件复制

注意这种方式只能复制只包含字符的文件,也就意味着你用记事本打开该文件你能够读懂

private static void bufferedReaderWriterCopyFile(File srcFile, File desFile)  throwsIOException {//使用带缓冲区的高效字符流进行文件复制BufferedReader br = new BufferedReader(newFileReader(srcFile));BufferedWriter bw = new BufferedWriter(newFileWriter(desFile));char[] c = new char[1024];Integer len = 0;while((len = br.read(c)) != -1) {bw.write(c, 0, len);}//方式二/*String s = null;while((s = br.readLine()) != null) {bw.write(s);bw.newLine();}*/br.close();bw.close();
}

8、使用FileChannel复制

Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快

private static void copyFileUsingFileChannels(File source, File dest) throws IOException {   FileChannel inputChannel = null;   FileChannel outputChannel = null;   try { inputChannel = new FileInputStream(source).getChannel(); outputChannel = new FileOutputStream(dest).getChannel(); outputChannel.transferFrom(inputChannel, 0, inputChannel.size()); } finally { inputChannel.close(); outputChannel.close(); } 
} 

http://www.tj-hxxt.cn/news/92774.html

相关文章:

  • 企业百度网站怎么做游戏推广代理
  • 视频网站 如何做seo搜客
  • h网站建设宁波网络推广联系方式
  • 沈阳外贸网站制作公司国内搜索引擎排名第一的是
  • 淘宝上做网站排名的是真的吗友情链接检索
  • 重庆大良网站建设北京做网站的公司有哪些
  • wordpress淘宝客主题制作视频教程整站多关键词优化
  • wordpress 备案广州市口碑seo推广外包
  • 虚拟网站建设宁波专业seo服务
  • 石家庄核酸检测武汉seo首页优化公司
  • 怎么在b2b网站上发信息让百度收录怎么做世界羽联最新排名
  • 长治网站页设计制作信息如何优化上百度首页
  • 远离有害不良网站应该怎么做今天重大新闻事件
  • 网站公司怎么做运营商班级优化大师
  • 备案网站名怎么填写免费观看短视频的app软件推荐
  • 网站首页没被收录做网络推广怎么收费
  • 装饰设计网站模板廊坊关键词优化报价
  • 设计师图库网站百度优化公司
  • 网页制作与网站建设实战大全 pdf下载浙江seo公司
  • asp网站栏目修改系统优化软件有哪些
  • 北京门户网站建设公司企业如何做网络推广
  • 江西省城乡建设网站网站排名优化培训课程
  • 做图片赚钱的网站商家推广平台有哪些
  • 做个门户网站多少钱广州发布紧急通知
  • 米拓网站建设教程私域营销
  • 网站续费一般多少钱手机网站建设公司
  • 郑州制作网站廊坊网站
  • 怎么用we做网站百度官网客服
  • 网站哪些功能是PHP做的英文关键词seo
  • 做网站建设的技巧推广普通话宣传语100字