Question: Java question, need help to make directories and adding strings to .txt file. import java.io.File; import java.io.IOException; /** * .test1/ -- top level folder *

Java question, need help to make directories and adding strings to .txt file.

import java.io.File; import java.io.IOException; /** * .test1/ -- top level folder * test2/ -- folder inside test1 folder * fairytale -- text file inside test1 containing the current fairytale */ public class Repo { static final File Cur = new File(System.getProperty("user.dir")); static final File a = Utils.join(Cur,".test1"); static final File b = Utils.join(a, "test2"); static final File c = Utils.join(a, "fairytale.txt"); /** * creates any necessary folders or files * .test1/ -- top level folder * - test2/ * - fairytale */ public static void setupPersistence() { File a = new File(".test1"); a.mkdir(); File b = new File("test2"); b.mkdir(); File c = new File("fairytale.txt"); try { c.createNewFile(); } catch (IOException e) { System.err.println("File already exists"); } a.exists(); b.exists(); } /** * Append first non-command argument in args * to a file called `fairytale` in the .test1 directory. */ public static void writeStory(String text) { static String Utils.readContentsAsString(c); Utils.writeContents(CAPERS_FOLDER2, text);// TODO } 

Supposed output:

$ java test1.Main fairytale "The"

The

$ java test1.Main fairytale "Princess"

The

Princess

Im getting: java test1/Main fairytale "The" args: [fairytale, Hello] Exception in thread "main" java.lang.IllegalArgumentException: cannot overwrite directory

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!