首页 > 其他分享 >课上代码

课上代码

时间:2022-11-05 23:33:18浏览次数:28  
标签:File 代码 createNewFile 课上 file new public String

1、创建文件对象相关构造器和方法 new File(String pathname) //根据路径构建一个 File 对象 new File(File parent,String child) //根据父目录文件+子路径构建 new File(String parent,String child) //根据父目录+子路径构建 createNewFile() //创建文件 import org.testng.annotations.Test; import java.io.File; import java.io.IOException; public class FileCreate {public static void main(String[] args) { } //方式 1 @Test public void create1(){ String filePath = "D:\\file1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("创建文件 1 成功"); } catch (IOException e) { e.printStackTrace(); } } //方式 2 @Test public void create2(){ File parentFile = new File("D:\\"); String fileNane = "file2.txt";File file = new File(parentFile, fileNane); try { file.createNewFile(); System.out.println("文件 2 创建成功"); } catch (IOException e) { throw new RuntimeException(e); } } //方式三 @Test public void create3(){ String parentPath = "d:\\"; String filePath = "file3.txt"; File file = new File(parentPath, filePath); try { file.createNewFile(); System.out.println("文件 3 创建成功");} catch (IOException e) { throw new RuntimeException(e); } } }

 

 

标签:File,代码,createNewFile,课上,file,new,public,String
From: https://www.cnblogs.com/tscky/p/16861691.html

相关文章

  • Codelf 搜索开源代码帮程序员命名
    "计算机科学里两件最难的事:缓存失效和命名。"Codelf通过搜索在线开源平台Github,Bitbucket,GoogleCode,Codeplex,Sourceforge,FedoraProject的项目源码,帮开发者从......
  • idea提交代码时.gitignore过滤不生效问题
    1、进入项目目录,执行以下代码:gitrm-r--cached.gitadd.gitcommit-m'update.gitignore' 如效果图:  2、然后把idea关闭,重新打开,再提交代码.git......
  • 前端vue2+vue-router3+ElementUI+axios综合使用代码教程登录页案例
    为了测试vue2+vue-router3路由+ElementUI界面+axios网络HTTP请求的基本使用情况专门编写一个登录页面进行demo验证  依赖情况package.json{"name":"default",......
  • 2.第一个宏代码
    #//在本工作簿路径下新增工作簿并输入数据//在本工作簿路径下新增工作簿并输入数据functiontest()//声明函数,函数名字为text{......
  • 关机代码
    #include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){ charinput[20]={0}; system("shutdown-s-t60"); again: printf("请注意,你的电脑还有一分......
  • 关机代码
    #include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){ charinput[20]={0}; system("shutdown-s-t60"); again: printf("请注意,你的电脑还有一分......
  • 关机代码
    #include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){ charinput[20]={0}; system("shutdown-s-t60"); again: printf("请注意,你的电脑还有一分......
  • 关机代码
    #include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){ charinput[20]={0}; system("shutdown-s-t60"); again: printf("请注意,你的电脑还有一分......
  • 一个瞬间让你的代码量暴增的脚本
    1功能概述在某些特殊情况下,需要凑齐一定的代码量,或者一定的提交次数,为了应急不得不采用一些非常规的手段来保证达标。本文分享的是一段自动提交代码的脚本,用于凑齐coderev......
  • 代码随想录Day17
    LeetCode199.二叉树右视图给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。   层序遍历一个思路,只需要判断当前元素是否......