Create file (absolute path)
You can create a file object by passing the absolute path as an argument to File()
.
A file object is just a file pointing to that path, not actually creating a file.
If you pass a file object as an argument to Files.touch()
, an actual file is created.
You can create a file in a specific path like this:
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File file = new File(tempDir + "/text.txt");
System.out.println(file.toPath());
System.out.println(file.exists());
Files.touch(file);
System.out.println(file.exists());
Output:
/tmp/text.txt
false
true
You could directly enter the file path as an absolute path, but since the path may be different for each platform and user,
I got the temp directory path temporarily available on the system with System.getProperty("java.io.tmpdir")
.
File.separator
Different OSes can use different delimiters such as /
and \
to separate directories.
File.separator
returns the separator used by the platform.
If the code runs on various platforms such as Windows and Linux, you should use File.separator
.
You can use File.separator
to implement something like this:
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File file = new File(tempDir + File.separator + "text.txt");
System.out.println(file.toPath());
System.out.println(file.exists());
Files.touch(file);
System.out.println(file.exists());
Output:
/tmp/text.txt
false
true
Create file (relative path)
If you create a file like File(tempDir, "text.txt")
, text.txt
file is created in the path of tempDir.
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File file = new File(tempDir, "text.txt");
System.out.println(file.toPath());
System.out.println(file.exists());
Files.touch(file);
System.out.println(file.exists());
Output:
/tmp/text.txt
false
true
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)