0

I have tried creating a file, using the code below:

import java.io.File;

public class DeleteEvidence {

public static void main(String[] args) {
    File evidence = new File("cookedBooks.txt");

However, the file cookedBooks.txt does not exist anywhere on my computer. I'm pretty new to this, so I'm having problems understanding other threads about similar problems.

Milen
  • 8,697
  • 7
  • 43
  • 57
  • You question has already been answered: http://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java – ShaMan-H_Fel Jun 21 '13 at 06:07
  • Possible duplicate of [How to create a file in a directory in java?](https://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java) – Bernhard Barker Mar 25 '18 at 08:45

6 Answers6

5

You have successfully created an instance of the class File, which is very different from creating actual files in your hard drive.

Instances of the File class are used to refer to files on the disk. You can use them to many things, for instance:

  • check if files or directories exist;
  • create/delete/rename files or directories; and
  • open "streams" to write data into the files.

To create a file in your hard disk and write some data to it, you could use, for instance, FileOutputStream.

public class AnExample {
  public static void main(String... args) throws Throwable {
    final File file = new File("file.dat");
    try (FileOutputStream fos = new FileOutputStream(file);
         DataOutputStream out = new DataOutputStream(fos)) {
      out.writeInt(42);
    }
  }
}

Here, fos in an instance of FileOutputStream, which is an OutputStream that writes all bytes written to it to an underlying file on disk.

Then, I create an instance of DataOutputStream around that FileOutputStream: this way, we can write more complex data types than bytes and byte arrays (which is your only possibility using the FileOutputStream directly).

Finally, four bytes of data are written to the file: the four bytes representing the integer 42. Note that, if you open this file on a text editor, you will see garbage, since the code above did not write the characters '4' and '2'.

Another possibility would have been to use an OutputStreamWriter, which would give you an instance of Writer that can be used to write text (non-binary) files:

public class AnExample {
  public static void main(String... args) throws Throwable {
    final File file = new File("file.txt");
    try (FileOutputStream fos = new FileOutputStream(file);
         OutputStreamWriter out = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
      out.write("You can read this with a text editor.");
    }
  }
}

Here, you can open the file file.txt on a text editor and read the message written to it.

Bruno Reis
  • 37,201
  • 11
  • 119
  • 156
  • 1
    Or `File#createNewFile` ;) – MadProgrammer Jun 21 '13 at 06:09
  • Okay, thank you. I think I understand what you're saying. I'm just taking myself through Java For Dummies right now so I'm pretty bad at all of this. – RUNN1NG EMOT10N Jun 21 '13 at 06:20
  • :) keep going, things will become more and more clear the more you read and practice! Be sure to check the JavaDocs all the time. I'd suggest you to open it right now and read the description of the classes `FileOutputStream` and `File`. Maybe also you could check `DataOutputStream`, `Writer` and `OutputStreamWriter`! http://docs.oracle.com/javase/7/docs/api/ – Bruno Reis Jun 21 '13 at 06:22
2
File evidence = new File(path);
evidence.mkdirs(); 
evidence.createNewFile();
stinepike
  • 54,068
  • 14
  • 92
  • 112
0

File is an abstract concept of a file which does not have to exist. Simply creating a File object does not actually create a physical object.

You can do this in (at least) two ways.

  1. Write something to the file (reference by the abstract File object)
  2. Calling File#createNewFile

You can also create temporary files using File#createTempFile but I don't think this is what you are trying to achieve.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

You have only created an object which can represent a file. This is just in memory though. If you want to access the file you must us ea FileInputStream or a FileOutputStream. Then it will also be created on the drive (in case of the outputstream).

 FileOutputStream fo = new FileOutputStream(new File(oFileName));
 fo.write("test".getBytes());
 fo.close();
Devolus
  • 21,661
  • 13
  • 66
  • 113
0

This is just ur creating file object by using this object u need to call one method i.e createFile() method..

So use evidence.createNewFile(); if you are creating just file. else if u want to create file in any specific location then specify your file name i.e File evidence=new File("path"); In this case if ur specifying any directoty

Jayant Jadhav
  • 318
  • 4
  • 12
0
String path="abc.txt";
File file = new File(path);
if (file.createNewFile()) {
    System.out.println("File is created");
}
else {
    System.out.println("File is  already   created");
}
FileWriter fw = new FileWriter(file, true);
string ab="Hello";
fw.write(ab);
fw.write(summary);
fw.close();
Matthias
  • 3,582
  • 2
  • 30
  • 41
Ganesh Joshi
  • 155
  • 1
  • 7