1

When creating a PrinterWriter object:

PrintWriter outputFile = new PrintWriter(*FileName*);

Where is the compiler looking when it goes to find FileName? For example, in Eclipse I am working in Arrays/src/ArraysAndFiles.java. In this example I am trying to open Values.txt. I have created this file in the src directory since that is where ArraysAndFiles.java is stored. When I attempt to open the file in the following code I get a FileNotFoundException:

import java.io.PrintWriter;

public class ArraysAndFiles {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        PrintWriter outputFile = new PrintWriter("Values.txt");

    }
}

What is the proper path to Values.txt?

justin
  • 553
  • 1
  • 7
  • 18
  • When you pass a path to `PrintWritier` it is looking in the file system of the OS, you can right-click your file to see its full path and pass that to the `PrintWriter`. **However**, when you JAR your code you wont exactly be able to write to files inside that running JAR. This is an [XY problem](https://meta.stackexchange.com/a/66378), what is your actual goal so someone can help you achieve it? – xtratic Mar 17 '19 at 22:20
  • The other way to open a file is to use a `JFileChooser` and let the user pick out the file for you. https://docs.oracle.com/javase/10/docs/api/javax/swing/JFileChooser.html – markspace Mar 17 '19 at 22:22
  • Place the file into the project directory `/Arrays`. – DevilsHnd - 退職した Mar 17 '19 at 22:29
  • @xtratic I am reading *Starting Out with Java* and was only trying to follow along with a "Working with Arrays and Files" example. – justin Mar 17 '19 at 22:32
  • I'm not sure what that example entails, what is your goal? *If you don't care about your code working when it is Jarred, and this is just an experiment,* then simply correct the path to be the full path according to the OS file system, eg. `C:\...\...\...\Arrays\src\Values.txt`. – xtratic Mar 17 '19 at 22:36
  • @Thank you this worked. But the task was open a file with `Print Writer` and then write the contents of an array to it using using a loop and .`println()` – justin Mar 17 '19 at 22:56

3 Answers3

0

Solution #1 (recommended for small files but you have the benefit that file will be found in other computers as well): How do I load a file from resource folder?

Solution #2: Build the path step by step by using File(String parent, String child) constructor. Example:

File desktop = new File(System.getProperty("user.home"),"Desktop");
File textsFolder = new File(desktop,"texts");
File testsFolder = new File(textsFolder,"tests");
File peopleTxt = new File(testsFolder,"people,txt");

Which is equals to: C://Users//George//Desktop//texts//tests//people.txt (Windows OS).

George Z.
  • 6,643
  • 4
  • 27
  • 47
-1

In your example "Values.txt" is a relative path. It's relative to your working directory.
Usually it's the same directory where your JAR file resides.

In Eclipse an application is built in the 'bin' folder. In your case it's Arrays\bin\. So this is the working directory for the application and your file has to be there.

If you want Eclipse to export this file during the Build process, do the following:

Right click on the file -> Build Path -> Add to Build Path

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
-1

As per code,

PrintWriter outputFile = new PrintWriter("Values.txt");

if you place your Values.txt in current/project directory i.e. in Arrays folder it should work but there are limitation as mentioned in above comments like writing to the file which is a part of JarFile.

Depending upon your purpose, you should take the action.

Rmahajan
  • 1,311
  • 1
  • 14
  • 23