常用的创建文件方法
- 方式一
new File(String pathname)
- 方式二
new File(File parent, String child)
- 方式三
new File(String parent, String child)
package OI_practice1;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class FileCreate {
public static void main(String[] args) {
create01();
create02();
create03();
}
@Test
public static void create01() {
String filePath = "F:\\new1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("File creat ok");
} catch (IOException e) {
System.out.println("File creat error");
e.printStackTrace();
}
}
public static void create02() {
File parentfile = new File("F:\\");
String filePath = "new2.txt";
File file = new File(parentfile, filePath);
try {
file.createNewFile();
System.out.println("File creat ok");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void create03() {
String parentPath = "F:\\";
String filePath = "new3.txt";
File file = new File(parentPath, filePath);
try {
file.createNewFile();
System.out.println("File creat ok");
} catch (IOException e) {
e.printStackTrace();
}
}
}