0

I am looking for a way to make a bunch of empty txt files that would be named after elements of an ArrayList in Java.

Assuming that fList has "Apple", "Banana" and "Cherry", this piece of code should create Apple.txt, Banana.txt and Cherry.txt in the project directory.

Unfortunately, it does not, and I do not understand why. I assume it's a logic or syntax error.

public void ViewList() {
        for (String fruits : fList) {
            String fileName = fruits;
            File f = new File(appDir + fileName + ".txt");
            if (f.exists() && f.isFile()) {
                System.out.println("Success!");
            }
        }

Can you help me understand what's wrong?

5
  • you need to actually create the file using f.createNewFile() Commented Mar 25, 2018 at 8:35
  • see this example: mkyong.com/java/how-to-create-a-file-in-java Commented Mar 25, 2018 at 8:41
  • Path elements are separated by / or \ depending on OS. Your code appDir + fileName + ".txt" will result in my/project/directoryapple.txt (notice lack of / before apple.txt). To solve it use new File(appDir, fileName + ".txt");. Commented Mar 25, 2018 at 8:43
  • I honestly do not see what was duplicated, but hey, what do I know. The explanation of pathing from Pshemo and confirmation that the logic is not at fault from Sathishkumar Manogaran were exactly what I needed. Thanks! Commented Mar 25, 2018 at 9:14
  • 1
    "I honestly do not see what was duplicated" your code doesn't contain part responsible for creating file on file system/disk. new File(...) only creates file object which is not the same (object holds information about potential file, like path, but is not file itself). Answer in duplicate question points that out and explains that to create actual file on disc you need to invoke f.createFile(). Commented Mar 25, 2018 at 10:12

1 Answer 1

1

Everything is correct in your code except few lines.

for (String fruit : fList) {
    //String fileName = fruits;
    File file = new File(appDir + fruit + ".txt");

    //OR if appDir doesn't end with `/` or `\` use
    //File file = new File(appDir, fruit + ".txt"); 

    // Create the file
    if (file.createNewFile()) {
        System.out.println("File is created!");
    } else {
        System.out.println("File already exists.");
    }
}

Also You can refer this link for more info:

https://howtodoinjava.com/core-java/io/how-to-create-a-new-file-in-java/

Note: Please note it down, file path strategy will vary between windows and unix system. So create filepath according to that.

Sign up to request clarification or add additional context in comments.

4 Comments

I wouldn't say it is useless if answer is pointing to right article. But such answer is considered lower quality because of risk of becoming useless when linked resource will disappear or change its location. So while links are nice as additional resource it can't be only source of information.
I got you point dude. Let me remove link and give correct answer :) Hodor :P
My previous comment was replay to other comment which apparently is now deleted. Anyway it is not necessary to remove link. It is fine as additional resource, or if answer is/contains a quote as info about its source.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.