0

I'm looking for a way to create a file folder with Eclipse Kepler. I'm just starting to learn this, so please excuse me for any ignorance I may be showing; I don't often apply Java to programs other than plugins for a game called Minecraft. So far, I know that files are creatable (with the code I have) through a file folder IF the folder exists, but the program won't create the folder automatically, like it does with the file that's being generated when the specified folder does exist. Here's the code I have so far:

package me.pookeythekid.filetests;
import java.io.BufferedWriter;
import java.io.FileWriter;

public class Main {
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter("C:\\Users\\Luke\\Desktop\\Folder\\Test.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("Hello World!");
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 Answers1

1

File Example:

        File file = new File("C:/file.txt");
        String content = "hello world";

        try (FileOutputStream fop = new FileOutputStream(file)) {


            if (!file.exists()) {
                file.createNewFile();
            }


            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

Folder Example:

File file = new File("C:\\Folder");
if (!file.exists()) {
    file.mkdir();           
}
fida1989
  • 3,234
  • 1
  • 27
  • 30