[IO]——创建文件

public class demo03 {
	public static void main(String[] args) {
		//test1();
		//test2();
		try {
			test3();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("文件操作失败");
		}
	}
	public static void test3() throws IOException, InterruptedException{//创建为文件,删除文件
		String path="E:/others/www.txt";//con为系统关键字
		File src=new File(path);
		if (!src.exists()) {
			Boolean flag = src.createNewFile();
			System.out.println(flag?"成功":"失败");
		}
		//删除文件
		Boolean flag=src.delete();
		System.out.println(flag?"成功":"失败");
		//static createTempFile(前缀3个字节,后缀默认.temp,默认临时空间)
		//static createTempFile(前缀3个字节,后缀默认.temp,目录)
		File temp=File.createTempFile("aaa", ".temp",new File("E:/others/"));
		Thread.sleep(10000);
		temp.deleteOnExit();//程序退出,则自动删除
	}
	public static void test2(){//判断信息
		File src=new File("E:/others/w.png");
		System.out.println("文件是否存在  "+src.exists());//文件是否存在
		System.out.println("文件是否可写"+src.canWrite());//是否可读写,canwrite(),canread()
		//isFile(),isDirectory
		if (src.isFile()) {
			System.out.println("文件");
		}else if(src.isDirectory()){
			System.out.println("文件夹");
		}else {
			System.out.println("文件不存在");
		}
		System.out.println("是否为绝对路径 "+src.isAbsolute());
		System.out.print(src.length());//长度,字节长度,只有文件才能读出长度
	}
	public static void test1(){//获取文件名的常用方法,建立联系
		File src=new File("E:/others/w.png");
		System.out.println(src.getName());//返回名称
		System.out.println(src.getPath());//如果是绝对路径,返回完整路径,否则返回相对路径
		System.out.println(src.getAbsolutePath());//返回绝对路径
		System.out.println(src.getParent());//返回上一级目录,如果没有上一级,返回空
	}
}

### 如何使用Java IO创建文件 在Java中,可以通过`java.io.File`类以及相关的流(Stream)来实现文件创建。以下是关于如何通过Java IO创建文件的具体方法。 #### 方法一:使用 `File` 类 可以利用 `java.io.File` 的实例化方式直接声明一个新文件路径并调用其 `createNewFile()` 方法完成文件的实际创建过程[^1]: ```java import java.io.File; import java.io.IOException; public class CreateFileExample { public static void main(String[] args) { File file = new File("example.txt"); try { if (file.createNewFile()) { // 创建一个新的空文件 System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.err.println("An error occurred while creating the file."); e.printStackTrace(); } } } ``` 此代码片段展示了如何通过 `File` 对象及其 `createNewFile()` 方法安全地创建文件,并处理可能抛出的异常情况。 --- #### 方法二:使用 `FileOutputStream` 另一种常见的做法是借助于输出流 (`FileOutputStream`) 来写入数据至目标位置的同时自动建立所需的文件实体[^2]: ```java import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamExample { public static void main(String[] args) { String content = "This is an example of writing data into a file."; try (FileOutputStream fos = new FileOutputStream("output.txt")) { byte[] strToBytes = content.getBytes(); // 将字符串转为字节数组 fos.write(strToBytes); // 向文件写入内容 System.out.println("Data has been written successfully!"); } catch (IOException e) { System.err.println("Error during file creation or write operation."); e.printStackTrace(); } } } ``` 上述例子说明了当尝试向尚未存在的文件名执行写操作时会触发该文件被初始化的行为模式。 --- #### 方法三:临时文件生成 如果需要的是仅限程序运行期间使用的短生命周期资源,则可考虑采用系统级API所提供的机制来自动生成这些类型的文档副本而无需显式指定存储地址[^4]: ```java import java.io.File; import java.io.IOException; public class TempFileCreation { public static void main(String[] args) throws IOException { File tempFile = File.createTempFile("prefix", ".tmp"); // 前缀与扩展名自定义设置 System.out.println("Temporary file path:" + tempFile.getAbsolutePath()); boolean deleteOnExitStatus = tempFile.deleteOnExit(); // 设置退出删除标志位 if(deleteOnExitStatus){ System.out.println("The temporary file will be deleted on JVM exit."); } } } ``` 这里强调了一种特殊的场景——即对于那些只希望维持短暂存活周期的数据载体而言更为适用的技术手段。 --- ### 总结 以上介绍了三种不同的途径用于满足不同需求下的新建动作实施策略。无论是常规持久化的正式档案还是瞬态性质的工作区素材都能找到对应的解决方案加以应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值