只需要调用该类的一个方法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();
} }
}

引用:

http://blog.sina.com.cn/s/blog_7014ad5c01019ah8.html

http://lisong0624.blog.163.com/blog/static/18871986201041724239325/

对File的api简单介绍,并有部分例子(推荐查看):

http://www.jb51.net/article/36126.htm

Java中File类创建文件的更多相关文章

  1. java中File类的常用方法总结

    java中File类的常用方法 创建: createNewFile()在指定的路径创建一个空文件,成功返回true,如果已经存在就不创建,然后返回false. mkdir() 在指定的位置创建一个此抽 ...

  2. Java 中File类的createNewFile()与createTempFile(), delete和deleteOnExit区别

    1. Java 中File类的createNewFile()与createTempFile()的区别 最近,在看代码时看到了一个方法, File.createTempFile() ,由此联想到File ...

  3. Java 之 File类(文件操作)

    一.概述 java.io.File 类是文件和目录路径名册抽象表示,主要用于文件和目录的创建.查找和删除等操作. File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法. 路径问题: ...

  4. Java中File类的基本用法

    File类的基本用法 java.io.File类:代表文件和目录.在开发中,读取文件.生成文件.删除文件.修改文件的属性时经常会用到此类. File类的常用构造方法:public File(Strin ...

  5. 关于Java里面File类创建txt文件重复???

    private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...

  6. Java中File类的方法详解

    File类也是Java中一个比较重要的类,通过他我们可以实现对文件的一系列操作,其内置了很多方法,下面我将按方法的功能分块,逐一讲解: 快速导航 构造方法 常用方法 创建目录 判断 `is...` t ...

  7. 使用Java中File类批量创建文件和批量修改文件名

    批量创建文件 int cont = 1; String s = "E:\\学习资料\\Java笔记-"; while(cont<100){ File f = new File ...

  8. java中File类应用:遍历文件夹下所有文件

    练习: 要求指定文件夹下的所有文件,包括子文件夹下的文件 代码: package 遍历文件夹所有文件; import java.io.File; public class Test { public ...

  9. java中File类的使用

    public class FileLei {    public static void main(String[] args) throws IOException {        //..表示上 ...

随机推荐

  1. python排序参数key以及lambda函数

    首先,lambda格式 lambda x:x+1, 前面的x相当于传入的形参,后面的相当于返回值, 使用起来很简单,只要明白“:”前后的含义即可正确使用. 再来说一下排序等函数中的key,这里以lis ...

  2. HDU 6464 /// 权值线段树

    题目大意: 共Q次操作 操作有两种 操作一 在序列尾部加入f[i]个s[i] 操作二 查询序列第f[i]小到第s[i]小之间的总和 离线操作 把序列内的值离散化 然后利用离散化后的值 在线段树上对应权 ...

  3. vue - blog开发学7

    将基本的项目部署到linux上(前后台只是实现了基本的功能,本次只是记录一些基本的开发流程,完善,等后续) 1.linux环境准备(我用的是阿里云服务器) ①jre.mysql,Nginx基本上这些就 ...

  4. document.location window.location

    document.location 和 window.location 取url的值的时候可以通用,但是 document是window的属性,所以不能直接用document.location =ur ...

  5. Mac 安装cnpm

    1.先安装node   node的下载地址:http://nodejs.cn/download/   这个没什么好说的,安装完成后测试一下,在终端输入:node -v   这时候就可以看到安装的nod ...

  6. 2018-2-13-win10-UWP-应用设置

    title author date CreateTime categories win10 UWP 应用设置 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  7. 2018-2-13-win10-UWP-单元测试

    title author date CreateTime categories win10 UWP 单元测试 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...

  8. 分析dwebsocket的源码过程

    前言 dwebsocet 是python django的websocket库,github地址:https://github.com/duanhongyi/dwebsocket 本章是对dwebsoc ...

  9. Java多线程状态切换

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11426573.html 线程状态 NEW RUNNABLE BLOCKED WAITING TIMED ...

  10. 【leetcode】980. Unique Paths III

    题目如下: On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  Ther ...