Java利用File类创建文件的示例代码
作者:iteye_6153 时间:2022-02-09 20:53:54
只需要调用该类的一个方法createNewFile(),但是在实际操作中需要注意一些事项,如判断文件是否存在,以及如何向新建文件中写入数据等。
import java.io.*;
public class CreateNewFile{
//该方法用于创建文件,参数分别是文件路径和文件名、文件内容,如:myfile.doc HelloJava!
public void createNewFile(String fileDirectoryAndName,String fileContent){
try{
String fileName = fileDirectoryAndName
File myFile = new File(fileName);//创建File对象,参数为String类型,表示目录名
//判断文件是否存在,如不存在则调用createNewFile()创建新目录,否则跳至异常处理代码
if(!myFile.exists())
myFile.createNewFile();
else //如果不存在则扔出异常
throw new Exception("The new file already exists!");
//下面把数据写入创建的文件,首先新建文件名为参数创建FileWriter对象
FileWriter resultFile = new FileWriter(myFile);
//把该对象包装进PrinterWriter对象
PrintWriter myNewFile = new PrintWriter(resultFile);
//再通过PrinterWriter对象的println()方法把字符串数据写入新建文件
myNewFile.println(fileContent);
resultFile.close(); //关闭文件写入流
}catch(Exception ex){
System.out.println("无法创建新文件!");
ex.printStackTrace();
}
}
public static void main(String[] args){
//创建类的对象并调用该对象的createNewFile()方法,创建新文件并写入数据
CreateNewFile createFile = new CreateNewFile();
createFile.createNewFile(args[0],args[1]);
}
}
执行该程序,在执行代码后直接输入两个参数,第一个参数是文件名,此时需要注明文件类型,这里创建的word文档;第二个参数是文件的内容,该参数是一个字符串数据。
如:myfile.doc HelloJava!
注意:在通过文件路径和文件创建File时的分隔符可以为“//”或者File.separator
public class FileDemo {
public static void main(String[] args){
//构造函数File(String pathname)
File f1 =new File("c:\\abc\\1.txt");
//File(String parent,String child)
File f2 =new File("c:\\abc","2.txt");
//File(File parent,String child)
File f3 =new File("c:"+File.separator+"abc");//separator 跨平台分隔符
File f4 =new File(f3,"3.txt");
System.out.println(f1);//c:\abc\1.txt
}
}
以下代码包括了File的创建以及读写。
public class Test {
public static void main(String[] args) {
String lujing = "d:\\test\\ss\\ss.txt";
File file = new File(lujing);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("kingid");
bw.flush();
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader fr = new FileReader(file);
BufferedReader bReader = new BufferedReader(fr);
String string = bReader.readLine();
System.out.println(string);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
来源:https://blog.csdn.net/iteye_6153/article/details/82648854
标签:Java,File类,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IDEA不编译除了.java之外的文件的解决办法(推荐)
2023-09-16 15:50:44
![](https://img.aspxhome.com/file/2023/0/58570_0s.png)
java读取xml配置参数代码实例
2023-11-25 03:03:17
解决mybatis plus 驼峰式命名规则问题
2022-09-29 11:24:34
![](https://img.aspxhome.com/file/2023/2/62712_0s.png)
elasticsearch集群cluster示例详解
2023-12-11 16:49:27
![](https://img.aspxhome.com/file/2023/3/67003_0s.png)
Java实现五子棋AI算法
2022-02-23 05:32:57
javaWeb使用servlet搭建服务器入门
2023-11-21 04:47:45
springmvc无法访问/WEB-INF/views下的jsp的解决方法
2023-11-23 15:15:38
My eclipse 端口占用(9360)问题解决办法
2023-05-19 02:34:32
![](https://img.aspxhome.com/file/2023/2/68542_0s.png)
详解Java实现设计模式之责任链模式
2023-11-08 10:32:07
![](https://img.aspxhome.com/file/2023/7/72557_0s.jpg)
Java实现考试系统
2023-11-18 04:15:03
![](https://img.aspxhome.com/file/2023/2/59602_0s.jpg)
一文让你搞懂如何手写一个redis分布式锁
2023-11-29 02:46:30
Java web spring异步方法实现步骤解析
2023-12-19 03:14:58
flutter实现底部导航栏
2023-08-23 01:06:13
![](https://img.aspxhome.com/file/2023/0/109150_0s.jpg)
Spring3 整合MyBatis3 配置多数据源动态选择SqlSessionFactory详细教程
2023-03-21 21:22:27
![](https://img.aspxhome.com/file/2023/6/61816_0s.png)
Unity中的PostProcessScene实用案例深入解析
2021-06-09 04:57:28
基于<aop:aspect>与<aop:advisor>的区别
2023-01-22 21:53:35
详解SpringMVC @RequestBody接收Json对象字符串
2022-03-20 05:00:10
SpringBoot实现PPT格式文件上传并在线预览功能
2023-07-22 14:44:56
![](https://img.aspxhome.com/file/2023/3/57403_0s.png)
Java实例讲解注解的应用
2021-11-02 01:43:14
IntelliJ IDEA 安装教程2019.09.23(最新版)
2023-08-24 23:01:44
![](https://img.aspxhome.com/file/2023/5/58175_0s.jpg)