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

教师做网站赚钱深圳网络营销推广中心

教师做网站赚钱,深圳网络营销推广中心,服装外贸网站设计,网站建设考核标准目录 文件写入读取文件内容删除文件复制文件向文件中追加数据创建临时文件文件/目录最后的修改日期获取文件大小文件重命名设置文件只读检测文件是否存在创建文件文件路径比较递归创建目录删除目录判断目录是否为空判断文件是否隐藏获取目录大小在指定目录中查找文件获取文件的… 目录 文件写入读取文件内容删除文件复制文件向文件中追加数据创建临时文件文件/目录最后的修改日期获取文件大小文件重命名设置文件只读检测文件是否存在创建文件文件路径比较递归创建目录删除目录判断目录是否为空判断文件是否隐藏获取目录大小在指定目录中查找文件获取文件的上级目录遍历目录结构遍历指定目录下的所有目录输出指定目录下的所有文件在指定目录中查找匹配文件查看系统根目录查看当前工作目录 文件写入 package com.example.demo;import org.junit.Test;import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException;public class FileTests {Testpublic void testWriteFile() throws IOException {BufferedWriter writer new BufferedWriter(new FileWriter(demo.txt));writer.write(你好世界);writer.close();} } 查看写入结果 $ cat demo.txt 你好世界读取文件内容 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testReadFile() throws IOException {BufferedReader reader new BufferedReader(new FileReader(demo.txt));String line null;while ((line reader.readLine()) ! null) {System.out.println(line);// 你好世界}} } 删除文件 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testDeleteFile() throws IOException {File file new File(demo.txt);boolean isDeleted file.delete();} } 复制文件 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testCopyFile() throws IOException {InputStream input new FileInputStream(new File(source.txt));OutputStream output new FileOutputStream(new File(target.txt));byte[] buf new byte[1024];int len 0;while ((len input.read(buf)) 0) {output.write(buf, 0, len);}input.close();output.close();} } 向文件中追加数据 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testAppendFile() throws IOException {BufferedWriter writer new BufferedWriter(new FileWriter(demo.txt, true));writer.write(Hello World! System.lineSeparator());writer.close();} } 创建临时文件 方法签名 // prefix 前缀 // suffix 后缀 // directory 目录需要提前创建好public static File createTempFile(String prefix, String suffix, File directory)public static File createTempFile(String prefix, String suffix)package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testTempFile() throws IOException {File temp File.createTempFile(temp_, .txt, new File(temp));File absoluteFile temp.getAbsoluteFile();System.out.println(absoluteFile);BufferedWriter writer new BufferedWriter(new FileWriter(temp));writer.write(Hello World! System.lineSeparator());writer.close();} } $ cat temp/temp_4469794763475912115.txtHello World!文件/目录最后的修改日期 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testLastModifiedFile() throws IOException {File file new File(demo.txt);file.createNewFile();// 获取最后修改时间long lastModified file.lastModified();System.out.println(lastModified);// 1696666928606// 修改文件最后的修改日期file.setLastModified(System.currentTimeMillis());System.out.println(file.lastModified());// 1696667018522} } 获取文件大小 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testGetFileSize() throws IOException {File file new File(demo.txt);long length file.length();System.out.println(length);// 434} } 文件重命名 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testRenameFile() throws IOException {File file new File(demo.txt);File newFile new File(newDemo.txt);boolean ret file.renameTo(newFile);} } 设置文件只读 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testReadOnlyFile() {File file new File(demo.txt);System.out.println(file.setReadOnly()); // trueSystem.out.println(file.canWrite()); // false} } 检测文件是否存在 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testFileExists() {File file new File(demo.txt);System.out.println(file.exists()); // true} } 创建文件 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testCreateFile() throws IOException {File file new File(demo.txt);System.out.println(file.createNewFile()); // true} } 文件路径比较 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testCompareFile() throws IOException {File file1 new File(demo1.txt);File file2 new File(demo2.txt);System.out.println(file1.compareTo(file2)); // -1} } 递归创建目录 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testMKDirs() throws IOException {File file new File(./temp/demo);boolean result file.mkdirs();System.out.println(result); // true} } 删除目录 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {public static boolean deleteDir(File dir) {// 递归删除目录中的文件if (dir.isDirectory()) {for (File file : dir.listFiles()) {boolean success deleteDir(file);if (!success) {return false;}}}return dir.delete();}Testpublic void testDeleteDirectory() {File file new File(./temp);boolean result deleteDir(file);System.out.println(result); // true} } 判断目录是否为空 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {public static boolean isEmptyDirectory(File dir) {if(!dir.isDirectory()){throw new RuntimeException(Not a Directory);}return dir.list().length 0;}Testpublic void testEmptyDirectory() {File file new File(./temp);boolean result isEmptyDirectory(file);System.out.println(result); // true} } 判断文件是否隐藏 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testHiddenFile() {File file new File(./temp);boolean result file.isHidden();System.out.println(result); // false} } 获取目录大小 依赖 !-- https://mvnrepository.com/artifact/commons-io/commons-io -- dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.11.0/version /dependencypackage com.example.demo;import org.apache.commons.io.FileUtils; import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testDirectorySize() {File file new File(./temp);long sizeOfDirectory FileUtils.sizeOfDirectory(file);System.out.println(sizeOfDirectory); // false} } 在指定目录中查找文件 package com.example.demo;import org.junit.Test;import java.io.*;public class FileTests {Testpublic void testFindFileFromDirectory() {File file new File(./temp);String[] list file.list();for (String filename : list) {System.out.println(filename);}} } 获取文件的上级目录 package com.example.demo;import org.junit.Test;import java.io.File;public class FileTests {Testpublic void testParent() {File file new File(/temp/demo.txt);String parent file.getParent();System.out.println(parent);// /temp} } 遍历目录结构 package com.example.demo;import org.junit.Test;import java.io.File;public class FileTests {public static void showTree(int indent, File file) {// 缩进for (int i 0; i indent; i) {System.out.print(-);}System.out.println(file.getName());if(file.isDirectory()){for (File f : file.listFiles()) {showTree(indent 4, f);}}}Testpublic void testShowTree() {showTree(0, new File(./temp));} } 打印结果 temp ----demo --------demo1.txt ----demo.txt遍历指定目录下的所有目录 package com.example.demo;import org.junit.Test;import java.io.File; import java.io.FileFilter;public class FileTests {Testpublic void testFileFilter() {File file new File(./temp);FileFilter fileFilter new FileFilter(){Overridepublic boolean accept(File pathname) {return pathname.isDirectory();}};File[] files file.listFiles(fileFilter);for (File f : files) {System.out.println(f);}} } 输出指定目录下的所有文件 package com.example.demo;import org.junit.Test;import java.io.File; import java.io.FileFilter;public class FileTests {Testpublic void testListFiles() {File file new File(./temp);File[] files file.listFiles();for (File f : files) {System.out.println(f);}} } 在指定目录中查找匹配文件 package com.example.demo;import org.junit.Test;import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter;public class FileTests {Testpublic void testListFiles() {File file new File(./temp);FilenameFilter filter new FilenameFilter(){Overridepublic boolean accept(File dir, String name) {return name.endsWith(.txt);}};File[] files file.listFiles(filter);for (File f : files) {System.out.println(f);}} } 查看系统根目录 package com.example.demo;import org.junit.Test;import java.io.File;public class FileTests {Testpublic void testListFiles() {File[] roots File.listRoots();for (File f : roots) {System.out.println(f);}// /} } 查看当前工作目录 package com.example.demo;import org.junit.Test;public class FileTests {Testpublic void testCurrentDirectory() {String currentDir System.getProperty(user.dir);System.out.println(currentDir);} }参考 https://www.nowcoder.com/tutorial/10001/d7671778e69f4fd2b66c6209b4bce9a4
http://www.zqtcl.cn/news/570249/

相关文章:

  • 网站建设必备网站自助建设
  • 杭州免费自助建站模板辽宁建设工程信息网为什么打不开
  • sdcms网站源码百度怎么免费做网站
  • 图书馆网站参考咨询建设wordpress安装500
  • 详细描述建设网站wordpress 子页面
  • 做公司网站推广如何快速推广
  • 给期货交易类做网站违法吗青海企业网站制作
  • 成都网站模板购买一站式营销型网站建设服务
  • wordpress建站优势做网站认证对网站有什么好处
  • synology做网站专业企业建站价格
  • php开发大型网站开发免费个人微网站
  • 专门做奢侈品的网站怎么建设课题网站
  • 博客推广那个网站列好深圳社保个人网页登录
  • 网站的背景图怎么做最新章节 第一百四十七章 做视频网站
  • 济南网站建设百家号阿里云怎么wordpress
  • 网站分享对联广告北京建设执业网站
  • 一级做爰片免费网站域名流量查询
  • 做网站网站需要注意什么网站建设swot市场分析
  • 大学生兼职网站的融资方案云凡济南网站建设开发
  • 做动态效果的插件网站抚顺清原网站建设招聘
  • 商务网站开发需求分析厦门35网站建设公司
  • wordpress classseo推广服务
  • 石景山网站建设公司网站后台密码如何破解
  • 哪个大学的网站做的最好看南宁网站设计制作公司
  • 北京 集团公司网站建设免费网站建设模版云盘
  • 阿里云建设网站要什么广州网站建设方案案例
  • 德阳吧网站建设线上编程培训机构哪家好
  • 天津电商网站开发备案查询站长之家
  • 网至普的营销型网站布局青岛做网站
  • 网站开发的安全问题wordpress文章列表显示缩略图