构造器
方法
演示创建文件
方式1:new File(String pathname)
- public class FileCreate {
- public static void main(String[] args) {
-
- }
- @Test
- public void create01(){
- String filePath = "d:\\news1.txt";
- File file = new File(filePath);
-
- try {
- file.createNewFile();
- System.out.println("文件创建成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
- }
方式2:new File(String parent,String child)
- public class FileCreate {
- public static void main(String[] args) {
-
- }
- @Test
- public void create02(){
- String parentPath = "d:\\";
- String filePath = "news2.txt";
- File file = new File(parentPath,filePath);
-
- try {
- file.createNewFile();
- System.out.println("文件创建成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
方式3:new File(File parent,String child)
- public class FileCreate {
- public static void main(String[] args) {
-
- }
- @Test
- public void create02(){
- File parentFile = new File("d:\\");
- String filePath = "news2.txt";
- File file = new File(parentFile,filePath);
-
- try {
- file.createNewFile();
- System.out.println("文件创建成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }