Create a file
boolean createNewFile()
- Creates a new file.
static File createTempFile(String prefix, String suffix)
- Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
static File createTempFile(String prefix, String suffix, File directory)
- Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
File aFile = new File("c:/aFolder");
try {
System.out.println(aFile.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}
The following code creates a temporary file under user's home directory.
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
File tempFile = File.createTempFile("abc","txt");
System.out.println(tempFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code below creates a temporary file with the directory you passed in:
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
File tempFile = File.createTempFile("abc","txt",new File("c:\\"));
System.out.println(tempFile);//c:\abc8669897675133345122txt
} catch (IOException e) {
e.printStackTrace();
}
}
}
Home
Java Book
File Stream
Java Book
File Stream
File:
- File
- Constants from File class
- Create file object
- Is it executable, readable or writable
- Compare two file path
- Create a file
- Delete a file
- Is it a file or a directory
- Whether the file or directory denoted by this abstract pathname exists
- Whether this abstract pathname is absolute
- Is this file hidden
- Get the file last modified time
- Get the file size
- Get file path and name
- Get free space, total space, usable space
- Get parent file
- Return file in a directory
- The file system roots
- Create new directories
- Rename file
- Change to executable, readable, writable
- Change last-modified time
- Get string representation of a file location
- Convert file location to URI and URL