If the directory doesn’t exist you need to create it. Java won’t create it by itself since the File
class is just a link to an entity that can also not exist at all.
As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see
FileNotFoundException – If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
You should try creating a path for the folder it contains before:
File file = new File("C:/Users/Me/Desktop/directory/file.txt"); file.getParentFile().mkdirs(); PrintWriter printWriter = new PrintWriter(file);
Related Posts:
- PrintWriter append method not appending
- error: unreported exception FileNotFoundException; must be caught or declared to be thrown
- How to format strings in Java
- What exactly is Apache Camel?
- What is an instance variable in Java?
- Why do we need copy constructor and when should we use copy constructor in java
- What are the -Xms and -Xmx parameters when starting JVM?
- How to match “any character” in regular expression?
- Cannot make a static reference to the non-static method fxn(int) from the type Two
- How to implement a tree data-structure in Java?
- Difference between StringBuilder and StringBuffer
- How can I use pointers in Java?
- JAVA invalid maximum heap size. The specified size exceeds the maximum representable size
- JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState
- A long bigger than Long.MAX_VALUE
- How to remove single character from a String
- Unsupported major.minor version 52.0 [duplicate]
- How do I limit the number of decimals printed for a double?
- Unfortunately MyApp has stopped. How can I solve this?
- exception in thread ‘main’ java.lang.NoClassDefFoundError:
- java.io.FileNotFoundException: the system cannot find the file specified
- How to clear the console?
- How to capitalize the first letter of a String in Java?
- Scanner vs. BufferedReader
- Raw use of parameterized class
- :: (double colon) operator in Java 8
- What does ‘0’ do in Java?
- Service will not start: error 1067: the process terminated unexpectedly
- Explain the use of a bit vector for determining if all characters are unique
- Convert java.util.Date to String
- How do I do a deep copy of a 2d array in Java?
- Returning an empty array
- What’s the advantage of load() vs get() in Hibernate?
- What Java ORM do you prefer, and why?
- Always Round UP a Double
- Using Enum values as String literals
- How to change a package name in Eclipse?
- Are arrays passed by value or passed by reference in Java?
- How do I get “Press any key to continue” to work in my Java code?
- What does “?” mean in Java? [duplicate]
- What is the difference between a local variable, an instance field, an input parameter, and a class field?
- Error: Selection does not contain a main type
- Java, “Variable name” cannot be resolved to a variable
- JRE 1.7 – java version – returns: java/lang/NoClassDefFoundError: java/lang/Object
- Short form for Java if statement
- How to read json file into java with simple JSON library
- How can I convert a long to int in Java?
- org.apache.jasper.JasperException: Unable to compile class for JSP:
- How do I parse command line arguments in Java?
- Missing method body, or declare abstract in Java
- java code is showing error. ( ‘;’,expected)
- Efficient swapping of elements of an array in Java
- local variables referenced from an inner class must be final or effectively final
- java.lang.NumberFormatException: empty String
- Java and Windows – error: illegal escape character
- How should I copy Strings in Java?
- Bank Account Java Program
- List of all special characters that need to be escaped in a regex
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
- java.lang.ArithmeticException: / by zero
- Eclipse error, “The selection cannot be launched, and there are no recent launches”
- STS launch error – Java was started but returned exit code=13
- RecyclerView onClick
- Java, Shifting Elements in an Array
- error: bad operand types for binary operator ‘&&’
- ReferenceError: “alert” is not defined
- How to write an ArrayList of Strings into a text file?
- how to fix Cannot call sendRedirect() after the response has been committed?
- Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project
- difference between equals() and hashCode()
- Adding days to a date in Java
- The C# Shorthand getters and setters
- What’s the reason I can’t create generic array types in Java?
- How to pass an object from one activity to another on Android
- Finding the size of a char array in Java
- What is ‘PermSize’ in Java?
- Jackson with JSON: Unrecognized field, not marked as ignorable
- Create a two dimensional string array anArray[2][2]
- Convert double to Int, rounded down
- A beginner’s error (args[0]) [duplicate]
- Converting from byte to int in Java
- When/why to call System.out.flush() in Java
- Throwing multiple exceptions in Java
- fixing errors on a program to call methods in java
- How to Set the Background Color of a JButton on the Mac OS
- How to easily convert a BufferedReader to a String?
- How can I calculate a time difference in Java?
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- convert string to arraylist
in java - how to use drawArc()
- How to add RGB values into setColor() in Java?
- Can you write virtual functions / methods in Java?
- Creating video player using Java
- Can I override and overload static methods in Java?
- How can i fix this equals on primitive type(int)
- Split string into array of character strings
- intellij idea – Error: java: invalid source release 1.9
- How fix Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
- Is it okay to throw NullPointerException programmatically?
- Android: how to create Switch case from this?