首页 > 其他分享 >IO流中常用的创建文件操作

IO流中常用的创建文件操作

时间:2022-11-06 10:00:10浏览次数:57  
标签:file IO 创建 流中 File new txt public String

1.new File(String pathname)//根据路径构建一个file对象

 

2.new File(File parent,String child) //根据父目录文件+子路径构建

 

3. new File(String parent,String child) //根据父目录+子路径构建

 

最终在D盘下面创建了三个txt文件(命名为名字缩写)

 

以下为三种方式的源代码

package com.hspedu.file;

import org.junit.jupiter.api.Test;

import java.io.*;

public class FileCreate {
public static void main(String[] args) {

}

//方式1 new File(String pathname)
@Test
public void create01() {
String filePath = "e:\\news1.txt";
File file = new File(filePath);

try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}

}
//方式2 new File(File parent,String child) //根据父目录文件+子路径构建
//e:\\news2.txt
@Test
public void create02() {
File parentFile = new File("e:\\");
String fileName = "news2.txt";
//这里的file对象,在java程序中,只是一个对象
//只有执行了createNewFile 方法,才会真正的,在磁盘创建该文件
File file = new File(parentFile, fileName);

try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}

//方式3 new File(String parent,String child) //根据父目录+子路径构建
@Test
public void create03() {
//String parentPath = "e:\\";
String parentPath = "e:\\";
String fileName = "news4.txt";
File file = new File(parentPath, fileName);

try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}

}

标签:file,IO,创建,流中,File,new,txt,public,String
From: https://www.cnblogs.com/1713564464qq/p/16861949.html

相关文章

  • ASP.NET Core教程-Configuration(配置)-返回XML
    更新记录转载请注明出处:2022年11月6日发布。2022年11月5日从笔记迁移到博客。ASP.NETCoreWebAPI配置支持XML说明默认情况下,ASP.NETCoreWebAPIController......
  • AfterEffects Anchor Point vs. Position
    AnchorPointvs.PositionHiEveryone,I’veseenafewrecenttutorialsthatsuggestusinganchorpointinsteadofpositionkeyframestomovevideoandstill......
  • Complexification
    目录ComplexificationRealificationFurtherRemarksComplexificationLet\(V\)bearealvectorspaceand\(T\)alinearoperatoron\(V\).Definethecomplexific......
  • cookie和session的区别
    1.什么是cookie?Http协议本身是无状态的,即服务器无法判断用户身份。Cookie实际上是一小段文本信息,客户端向服务器发起请求,如果服务器需要记录该用户状态,就使用response......
  • javaIO流
    文件的创建:packageio.stream.file;importjava.io.File;importjava.io.IOException;publicclass文件创建{publicstaticvoidmain(String[]args){......
  • bean的创建生命周期
    spring创建对象UserService.class->无参构造方法->得到对象->依赖注入(属性赋值)->初始化前方法(@PostConstruct)->初始化方法(InitializingBean)->初始化后(AOP)->bean......
  • 【IoT】产品认证:国密认证中的委托人、生产者、生产企业是什么意思?
    委托人:就是证书的持有人。生产者:原来叫制造商,对产品质量进行控制,并对产品销售负责。生产企业:产品的加工工厂。举例:A公司开发了一款产品,这款产品由B公司代工生产,C公司拿到这......
  • Vue Need to install with `app.use` function
    UncaughtSyntaxError:Needtoinstallwith`app.use`function(atmessage-compiler.esm-bundler.js:54:19)atcreateCompileError(message-compiler.esm-bundler.j......
  • MySQL表加字段可为null导致ArrayIndexOutOfBoundsException报错问题记录
    问题爆出企微群告警爆了,立即去看ELK报错日志,报错日志非常莫名其妙:​​java.lang.ArrayIndexOutOfBoundsException:16​​原因分析事后发现共有18257次报错日志,时间跨度19:......
  • Educational Codeforces Round 138 E,F
    E一道比较基础的题。首先就是纵向,走无障碍的格子,无法四联通和横向,走有障碍的格子,八联通是等价的。也就是,如果我们要让其不存在非法路径,那么应该存在一个从第一列出发,一......