Home > OS >  How to create a file in a folder in java [duplicate]
How to create a file in a folder in java [duplicate]

Time:09-30

I have created a file named Student, however I would like to create a folder called School and make a file called Student inside that folder how can I do it?

My code below:

public void upload() throws IOException {
        FileWriter writer = new FileWriter("student.txt");
        BufferedWriter buffer = new BufferedWriter(writer);
        buffer.write(generatedText);
        buffer.close();
    }

CodePudding user response:

You can use mkdir() method from File class:

new File("path\\School").mkdir();
FileWriter writer = new FileWriter("path\\School\\student.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Hello");
buffer.close();

Here path need to be replaced with your system path for the .

  • Related