java中新建文件的语句_如何用Java创建文件

本文概述

在Java中, 使用预定义的类和包可以很容易地创建文件。有三种创建文件的方法。

使用File.createNewFile()方法

使用FileOutputStream类

使用File.createFile()方法

Java File.createNewFile()方法

File.createNewFile()是File类的一种方法, 属于java.io包。它不接受任何参数。该方法会自动创建一个新的空文件。该方法返回一个布尔值:

如果文件创建成功, 则为true。

如果文件已经存在, 则返回false。

初始化File类对象时, 我们提供文件名, 然后可以调用File类的createNewFile()方法在Java中创建一个新文件。

如果发生I / O错误, 则File.createNewFile()方法将引发java.io.IOException。如果安全管理器存在并且其SecurityManager.checkWriter(java.lang.String)方法拒绝对该文件的写访问权, 则它还会引发SecurityException。该方法的签名是:

public boolean createNewFile() throws IOException

我们可以在File类对象中传递文件名或绝对路径或相对路径作为参数。对于非绝对路径, File对象将尝试在当前目录中找到文件。

下面的示例创建一个新的空文本文件。第一次运行成功创建music.txt, 而第二次运行失败。我们只能通过更改文件扩展名来创建任何类型的文件。

import java.io.File;

import java.io.IOException;

public class CreateFileExample1

{

public static void main(String[] args)

{

File file = new File("C:\demo\music.txt"); //initialize File object and passing path as argument

boolean result;

try

{

result = file.createNewFile();//creates a new file

if(result)// test if successfully created a new file

{

System.out.println("file created "+file.getCanonicalPath()); //returns the path string

}

else

{

System.out.println("File already exist at location: "+file.getCanonicalPath());

}

}

catch (IOException e)

{

e.printStackTrace();//prints exception if any

}

}

}

输出量

文件不存在时。

how-to-create-a-file-in-java.png

文件已经存在时。

b49674c18b29ccef90525468f79e943b.png

Java FileOutputStream

文件输出流将数据写入文件。 Java FileOutputStream类还提供对文件的支持。它属于java.io包。它将数据存储为字节。当需要向创建的文件中写入一些数据时, 可以使用FileOutputStream类。 FileOutputStream类提供了用于创建文件的构造函数。构造函数的签名是:

public FileOutputStream(String name, boolean append) throws FileNotFoundException

参量

名称:是文件名

append:如果为true, 则字节将被写入文件的末尾, 而不是开始。

在下面的示例中, 我们使用FileOutputStream创建了一个文件。

import java.io.FileOutputStream;

import java.util.Scanner;

public class CreateFileExample

{

public static void main(String args[])

{

try

{

Scanner sc=new Scanner(System.in); //object of Scanner class

System.out.print("Enter the file name: ");

String name=sc.nextLine();//variable name to store the file name

FileOutputStream fos=new FileOutputStream(name, true); // true for append mode

System.out.print("Enter file content: ");

String str=sc.nextLine()+"\n";//str stores the string which we have entered

byte[] b= str.getBytes();//converts string into bytes

fos.write(b);//writes bytes into file

fos.close();//close the file

System.out.println("file saved.");

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

输出量

859ac4cfae9f1090f49fbf17f2888724.png

Java File.createFile()方法

File.createFile()是File类的一种方法, 属于java.nio.file包。它还提供对文件的支持。 nio包是面向缓冲区的。 createFile()方法还用于创建一个新的空文件。使用此方法时, 我们不需要关闭资源。这是一个优势。该方法的签名是:

public static Path createFile(Path, Attribute) throws IOException

路径:文件的路径。

属性:文件属性的可选列表。

该方法返回文件。

以下示例还创建一个新的空文件。我们在名为Paths.get()的Paths类(java.nio.file.Paths)中使用静态方法创建Path实例。请注意以下语句:

路径path = Paths.get(“ C:\\ demo \\ javaprogram.txt”);

在上面的行中, Path是一个接口, Paths是一个类。两者都属于同一包。 Paths.get()方法创建路径实例。

import java.io.IOException;

import java.nio.file.*;

public class CreateFileExample3

{

public static void main(String[] args)

{

Path path = Paths.get("C:\demo\javaprogram.txt"); //creates Path instance

try

{

Path p= Files.createFile(path);//creates file at specified location

System.out.println("File Created at Path: "+p);

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

输出量

e3d7d84b73a25f4dde3087bde816ffec.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值