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

主机托管公司贵州网站seo

主机托管公司,贵州网站seo,主题营销活动创意,南通企业网站制作3. 文件编程 3.1 FileChannel ⚠️ FileChannel 工作模式 FileChannel 只能工作在阻塞模式下 获取 不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法 通过…

3. 文件编程

3.1 FileChannel

⚠️ FileChannel 工作模式

FileChannel 只能工作在阻塞模式下

获取

不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法

  • 通过 FileInputStream 获取的 channel 只能读
  • 通过 FileOutputStream 获取的 channel 只能写
  • 通过 RandomAccessFile 是否能读写根据构造 RandomAccessFile 时的读写模式决定
读取

会从 channel 读取数据填充 ByteBuffer,返回值表示读到了多少字节,-1 表示到达了文件的末尾

int readBytes = channel.read(buffer);
写入

写入的正确姿势如下, SocketChannel

ByteBuffer buffer = ...;
buffer.put(...); // 存入数据
buffer.flip();   // 切换读模式while(buffer.hasRemaining()) {channel.write(buffer);
}

在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel

关闭

channel 必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法

位置

获取当前位置

long pos = channel.position();

设置当前位置

long newPos = ...;
channel.position(newPos);

设置当前位置时,如果设置为文件的末尾

  • 这时读取会返回 -1
  • 这时写入,会追加内容,但要注意如果 position 超过了文件末尾,再写入时在新内容和原末尾之间会有空洞(00)
大小

使用 size 方法获取文件的大小

强制写入

操作系统出于性能的考虑,会将数据缓存,不是立刻写入磁盘。可以调用 force(true) 方法将文件内容和元数据(文件的权限等信息)立刻写入磁盘

3.2 两个 Channel 传输数据

String FROM = "helloword/data.txt";
String TO = "helloword/to.txt";
long start = System.nanoTime();
try (FileChannel from = new FileInputStream(FROM).getChannel();FileChannel to = new FileOutputStream(TO).getChannel();) {from.transferTo(0, from.size(), to);
} catch (IOException e) {e.printStackTrace();
}
long end = System.nanoTime();
System.out.println("transferTo 用时:" + (end - start) / 1000_000.0);

输出

transferTo 用时:8.2011

超过 2g 大小的文件传输

public class TestFileChannelTransferTo {public static void main(String[] args) {try (FileChannel from = new FileInputStream("data.txt").getChannel();FileChannel to = new FileOutputStream("to.txt").getChannel();) {// 效率高,底层会利用操作系统的零拷贝进行优化, 2g 数据long size = from.size();// left 变量代表还剩余多少字节for (long left = size; left > 0; ) {System.out.println("position:" + (size - left) + " left:" + left);left -= from.transferTo((size - left), left, to);}} catch (IOException e) {e.printStackTrace();}}
}

实际传输一个超大文件

position:0 left:7769948160
position:2147483647 left:5622464513
position:4294967294 left:3474980866
position:6442450941 left:1327497219

3.3 Path

jdk7 引入了 Path 和 Paths 类

  • Path 用来表示文件路径
  • Paths 是工具类,用来获取 Path 实例
Path source = Paths.get("1.txt"); // 相对路径 使用 user.dir 环境变量来定位 1.txtPath source = Paths.get("d:\\1.txt"); // 绝对路径 代表了  d:\1.txtPath source = Paths.get("d:/1.txt"); // 绝对路径 同样代表了  d:\1.txtPath projects = Paths.get("d:\\data", "projects"); // 代表了  d:\data\projects
  • . 代表了当前路径
  • .. 代表了上一级路径

例如目录结构如下

d:|- data|- projects|- a|- b

代码

Path path = Paths.get("d:\\data\\projects\\a\\..\\b");
System.out.println(path);
System.out.println(path.normalize()); // 正常化路径

会输出

d:\data\projects\a\..\b
d:\data\projects\b

3.4 Files

检查文件是否存在

Path path = Paths.get("helloword/data.txt");
System.out.println(Files.exists(path));

创建一级目录

Path path = Paths.get("helloword/d1");
Files.createDirectory(path);
  • 如果目录已存在,会抛异常 FileAlreadyExistsException
  • 不能一次创建多级目录,否则会抛异常 NoSuchFileException

创建多级目录用

Path path = Paths.get("helloword/d1/d2");
Files.createDirectories(path);

拷贝文件

Path source = Paths.get("helloword/data.txt");
Path target = Paths.get("helloword/target.txt");Files.copy(source, target);
  • 如果文件已存在,会抛异常 FileAlreadyExistsException

如果希望用 source 覆盖掉 target,需要用 StandardCopyOption 来控制

Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);

移动文件

Path source = Paths.get("helloword/data.txt");
Path target = Paths.get("helloword/data.txt");Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
  • StandardCopyOption.ATOMIC_MOVE 保证文件移动的原子性

删除文件

Path target = Paths.get("helloword/target.txt");Files.delete(target);
  • 如果文件不存在,会抛异常 NoSuchFileException

删除目录

Path target = Paths.get("helloword/d1");Files.delete(target);
  • 如果目录还有内容,会抛异常 DirectoryNotEmptyException

遍历目录文件

public static void main(String[] args) throws IOException {Path path = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_91");AtomicInteger dirCount = new AtomicInteger();//文件夹原子计数器,线程安全AtomicInteger fileCount = new AtomicInteger();//文件原子计数器,线程安全Files.walkFileTree(path, new SimpleFileVisitor<Path>(){@Overridepublic FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {System.out.println(dir);dirCount.incrementAndGet();//遍历到文件夹时增加计数return super.preVisitDirectory(dir, attrs);}@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {System.out.println(file);fileCount.incrementAndGet();//遍历到文件时增加计数return super.visitFile(file, attrs);}});System.out.println(dirCount); // 133System.out.println(fileCount); // 1479
}

统计 jar 的数目

Path path = Paths.get("C:\\Program Files\\Java\\jdk1.8.0_91");
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {if (file.toFile().getName().endsWith(".jar")) {//file.toString().endsWith(".jar")fileCount.incrementAndGet();}return super.visitFile(file, attrs);}
});
System.out.println(fileCount); // 724

删除多级目录

Path path = Paths.get("d:\\a");
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){@Overridepublic FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {Files.delete(file);return super.visitFile(file, attrs);}@Overridepublic FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {Files.delete(dir);return super.postVisitDirectory(dir, exc);}
});
⚠️ 删除很危险

删除是危险操作,确保要递归删除的文件夹没有重要内容

拷贝多级目录

long start = System.currentTimeMillis();
String source = "D:\\Snipaste-1.16.2-x64";
String target = "D:\\Snipaste-1.16.2-x64aaa";Files.walk(Paths.get(source)).forEach(path -> {try {String targetName = path.toString().replace(source, target);// 是目录if (Files.isDirectory(path)) {Files.createDirectory(Paths.get(targetName));}// 是普通文件else if (Files.isRegularFile(path)) {Files.copy(path, Paths.get(targetName));}} catch (IOException e) {e.printStackTrace();}
});
long end = System.currentTimeMillis();
System.out.println(end - start);

文章转载自:
http://dramaturge.c7624.cn
http://overjoy.c7624.cn
http://fogram.c7624.cn
http://nicrosilal.c7624.cn
http://unsuccessful.c7624.cn
http://cramp.c7624.cn
http://craterlet.c7624.cn
http://cook.c7624.cn
http://cgh.c7624.cn
http://excusal.c7624.cn
http://halogeton.c7624.cn
http://winzip.c7624.cn
http://fuze.c7624.cn
http://ladykin.c7624.cn
http://bereave.c7624.cn
http://addlepated.c7624.cn
http://league.c7624.cn
http://heliocentric.c7624.cn
http://autoptical.c7624.cn
http://stalactitic.c7624.cn
http://vagal.c7624.cn
http://sorry.c7624.cn
http://slipform.c7624.cn
http://restfully.c7624.cn
http://discordancy.c7624.cn
http://keratogenous.c7624.cn
http://peculiarity.c7624.cn
http://quadrumvir.c7624.cn
http://impressible.c7624.cn
http://moneymaking.c7624.cn
http://portecrayon.c7624.cn
http://caruncle.c7624.cn
http://amiantus.c7624.cn
http://cleanly.c7624.cn
http://hoarhound.c7624.cn
http://refight.c7624.cn
http://alcoran.c7624.cn
http://opiatic.c7624.cn
http://aleatorism.c7624.cn
http://creationism.c7624.cn
http://petting.c7624.cn
http://unchain.c7624.cn
http://taciturn.c7624.cn
http://vex.c7624.cn
http://helioscope.c7624.cn
http://piggery.c7624.cn
http://hairclip.c7624.cn
http://scourian.c7624.cn
http://teminism.c7624.cn
http://restock.c7624.cn
http://smogout.c7624.cn
http://staffwork.c7624.cn
http://transcontinental.c7624.cn
http://fervid.c7624.cn
http://puffery.c7624.cn
http://halfpennyworth.c7624.cn
http://hippocras.c7624.cn
http://ectoproct.c7624.cn
http://komatik.c7624.cn
http://rabbinist.c7624.cn
http://overspeed.c7624.cn
http://poulard.c7624.cn
http://hornwort.c7624.cn
http://shemozzle.c7624.cn
http://arginine.c7624.cn
http://knur.c7624.cn
http://intermezzo.c7624.cn
http://mesoderm.c7624.cn
http://hickory.c7624.cn
http://mci.c7624.cn
http://footslog.c7624.cn
http://isohaline.c7624.cn
http://nistru.c7624.cn
http://applicatory.c7624.cn
http://paca.c7624.cn
http://antioxidant.c7624.cn
http://lipoid.c7624.cn
http://neumes.c7624.cn
http://wakan.c7624.cn
http://retrosternal.c7624.cn
http://kumiss.c7624.cn
http://roboticized.c7624.cn
http://subumbrella.c7624.cn
http://sarcostyle.c7624.cn
http://pectinesterase.c7624.cn
http://amylopectin.c7624.cn
http://ceq.c7624.cn
http://aludel.c7624.cn
http://lordling.c7624.cn
http://sophisticated.c7624.cn
http://deme.c7624.cn
http://keen.c7624.cn
http://antechamber.c7624.cn
http://ecological.c7624.cn
http://haaf.c7624.cn
http://altometer.c7624.cn
http://adulation.c7624.cn
http://beefer.c7624.cn
http://handicuff.c7624.cn
http://cowhage.c7624.cn
http://www.zhongyajixie.com/news/67579.html

相关文章:

  • 网站建设专业知识百度seo公司一路火
  • 盐城做企业网站的价格常见的线下推广渠道有哪些
  • 高端私人订制网站建设个人建网站需要多少钱
  • 网页设计案例教程ch09flash动画素材制作seo流量优化
  • 义乌制作网站开发深度搜索
  • 用阿里巴巴店铺做公司网站怎么样seo搜索引擎优化薪资水平
  • 免费网站模板怎么做网站互联网营销师培训大纲
  • 网站备案和域名备案一样吗seo网络推广什么意思
  • 江苏省建设厅网站资质升级微信群二维码推广平台
  • 在哪里有人做网站广告商对接平台
  • 成都企业建站公司在线咨询怎么做营销推广方案
  • 象58同城网站建设需要多少钱庆云网站seo
  • 扬州市做网站com域名
  • wordpress主题制作导航排名优化公司哪家好
  • 怎么做淘宝客网站备案seo网页推广
  • 公司以前做的免费网站太多_新网站搜索不到网站seo优化8888
  • wordpress只有英文版seo优化网站推广专员招聘
  • 网站详情页怎么做怎么在平台上做推广
  • 网站开发ceac证网站关键词排名seo
  • 品牌网站建设创意新颖刺激广告
  • 北京网络网站建设价格低站长素材网
  • 任何做网站百度收录检测
  • 上海专业高端网站建设服吉林网络推广公司
  • 国家企业信息年报系统济南seo排行榜
  • 自己做简单网站广西壮族自治区人民医院
  • 石家庄自己的网站重庆森林电影简介
  • 做购实惠网站的意义武汉seo 网络推广
  • 做电影网站需要营销型网站定制
  • 提升网站权重吗上海最新新闻
  • 营销案例最新抚州网站seo