Java. Creation of empty .txt on the working table - Compilation error



  • I'm studying Java from scratch, there's a question.

    Why is this code, which is supposed to create an empty text in the working table, trying to compromise, making a mistake?

    package filecreator;
    

    import java.io.File;
    import java.io.IOException;

    public class FileCreator {

    public static void main(String[] args) {
    
        String fileName = "My File.txt";
        String filePath = "/Users/Donrumata/Desktop/";
    
        File myFile = new File(filePath + "/" + fileName);
    
        try {
            myFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    



  • If you have brought the whole code in the class, there is a gap at the end with a comma point and a closing brackets after it.


    While writing the answer, I saw the answer about the extra slash. Experience has shown that their number on the way is not a role. The next code will create a file in the same directory as the code from the question:

    String fileName = "My File.txt";
    String filePath = "/Users/Documents/Desktop/";
    File myFile = new File(filePath + "//////" + fileName);
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2