java createNewFile fails to create a file, the system cannot find the specified path

1. Code

package com.task.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class T5 {

    public static void main(String[] args) throws IOException {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        T5.generateText2("E:/1/2/3/4/5/file2.txt",list);
        T5.generateText1("E:/1/2/3/4/5/file1.txt",list);

    }

    public static void generateText1(String fileName,List<String> list) throws IOException{

        /* 写入Txt文件 */
        File writename = new File(fileName);
        if(!writename.isFile() && !writename.exists()){ //判断文件是否存在){}
            String  path = fileName.substring(0,fileName.lastIndexOf("/"));
            new File(path).mkdirs();
            String name = fileName.substring(fileName.lastIndexOf("/"));
            new File(name).createNewFile();
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(writename,true));
        if (list.size()>0) {
            for (int i = 0; i <list.size(); i++) {
                out.write(list.get(i));
            }
        }
        out.flush(); // 把缓存区内容压入文件
        out.close(); // 最后记得关闭文件
        System.out.println("1文件创建成功 = " + fileName);
    }

    /* 写入Txt文件 */
    public static void generateText2(String fileName,List<String> list) throws IOException{
        File writename = new File(fileName);
        //判断文件是否存在
        if(!writename.isFile() && !writename.exists()){
            // 创建新文件
            writename.createNewFile();
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(writename,true));
        if (list.size()>0) {
            for (int i = 0; i <list.size(); i++) {
                out.write(list.get(i));
            }
        }
        out.flush(); // 把缓存区内容压入文件
        out.close(); // 最后记得关闭文件
        System.out.println("2文件创建成功 = " + fileName);
    }
}

 2. Comparison of test results

When method 1 is executed first, and then method 2 is executed, there is no problem 

 Create the file as shown

 

 Execute method 2 first, and then execute method 1, and the path cannot be found

 3. Code Analysis

        In the two methods, the second half of "filling data into the file" is the same, the difference is that the former creates the file directory new File(path).mkdirs(); and then creates the file new File( name).createNewFile(); while the latter creates the file directly.

        View the source code of createNewFile:

        Atomically creates a new empty file named by this abstract pathname if and only if a file with that name does not already exist. As with all other filesystem activities that may affect a file, checking for the existence of the file and creating the file if it does not exist is an atomic operation.
        Note: This method should not be used for file locking, as the resulting protocol cannot be made to work reliably. The FileLock feature should be used instead.
        Returns: true if the named file does not exist and was successfully created, false if the named file already exists 

 

        A FileSystem object representing the platform's native file system.

        The canonicalized pathname string for this abstract pathname. Canonicalized pathname strings use the default name separators and do not contain any repeated or redundant separators. 

 

 

Guess you like

Origin blog.csdn.net/weixin_52255395/article/details/130933375