个性化阅读
专注于IT技术分析

如何用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
}		
}
}

输出量

文件不存在时。

如何用Java创建文件

文件已经存在时。

如何在Java1中创建文件

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();		
}
}
}

输出量

如何在Java2中创建文件

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();
}
}
}

输出量

如何在Java3中创建文件

赞(0)
未经允许不得转载:srcmini » 如何用Java创建文件

评论 抢沙发

评论前必须登录!