I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".
Related
This question already has answers here:
Reading a .txt file using Scanner class in Java
(11 answers)
Closed 3 years ago.
I have an assignment from my teacher i have make a serial program into a parallel with OMP.It's the Barnes-Hut one and its the first time i'm using netbeans.
I have a text file with someone numbers and i gotta import it into the project so it can use the values the text file has.How can i import the example.txt into netbeans?
I've tried this but it doesn't work
File myFile = new File("example.txt");
It also contains a scanner
public static void main(String[] args) {
// for reading from stdin
Scanner console = new Scanner(System.in);
Make sure your text file is in the classpath
You have a number of choices.
1. Relative Path
You can use a relative path. This means you take your current "working" location and look for the file from there, this is basically what you're doing now.
The problem with this is, the "working" location changes, and is based on the location from which the program is executed. Remember, the location of the class is not the same as the "working" location.
Netbeans "default" "working" location is usually the root directory of the project. There are ways you can configure Netbeans to use a specific "working" location, but you need to consider what happens when you're no longer running in Netbeans.
So a "simple" solution would be too drop the file into the Netbeans project directory. A slightly more advance solution would be to configure the project's "working" directory to a location containing the file
2. Absolute Path
This is when you use a fully qualified path from the a root location to the file (ie C:\some\place).
The problem with this is, rarely are this paths the same from one computer system to another and you need to remember to place the file into this location each time.
2.1 A "well known" location
A form of the absolute path solution is to place the file into a "well known" location, typically based on the OS.
For example, on Windows, you would typically use something like {user.home}\AppData\Remote\{name of your application}.
This would allow you to place the file in, what is essentially a "static" location and load it when ever you needed to.
But, this is probably over kill for your needs
3 Embedded resource
A more common solution is to "embed" the resource with your application. Essentially, this is placing the file within the applications class path, which allows the Java runtime to locate it.
In Netbeans, this means putting the file within the src directory, preferably in a subdirectory, like resources for example.
Then, when you want to read the file, you'd need to use Class#getResource(String) or Class#getResourceAsStream(String) to gain access to it.
So, based on the available information, and the example above, you might do something like...
try (InputStream is = YourClass.getResourceAsStream("/resources/example.txt")) {
// Read the file...
} catch (IOException exp) {
// Handle the exception
}
Just remember, this renders the resource "read-only" (at least for context of this simple example)
I am a newbie. A wanna to check for existing a folder or file in directory in past.
For better. Example, i may a directory C:\Users\Admin\AppData\ and i wanna to check of existing a directory Test in that path. That maybe be checket by:
File file = new File(System.getenv("APPDATA") + "\\Test\\");
if(file.isDirectory()){
///...
} else { ////....}
But i wanna to check if that directory is deleted - when. Please help with code examples... be VERY and VERY thanks
Instead of the File class, I recommend looking at the Files class - it is there to help you do many things. For example, Files.createFile(...) will check to see if a file exists before creating. You can then pass a positive result to FileWriter(...) for your work.
You can check for the presence of a file of folder, but not that it was deleted (e.g. checking a log file of past actions). I recommend using the logic of "if not there then it never existed or was deleted". Another option when working with files is to use parameters to always overwrite the file if that is what you want.
You are asking a question about the operating system. What happens after a file or folder is deleted is unique to each operating system. A notional recycle bin's awareness of a file or folder's original location was, and where that content may have been moved to is specific to an operating system (and usually isn't just moved into another folder).
I know there are 2 ways of writing a pathway for a file while reading file in JAVA.
//1st way
scanner = new Scanner(new File("C:\\Users\\User\\IdeaProjects\\EDSS\\src\\file\\JobList.txt"));
//2nd way
scanner = new Scanner(new File("src/file/JobList.txt"));
But when I try to convert my format from .java to .exe(Application) with the 1st way in IntelliJ via something called "Build Artifacts", I can open the application smoothly. However, with 2nd way, I cannot open the Application and I found out that it is because of the pathway. And 1st way only allow me to run the Application in my computer only. If I copy the file to my friend's laptop with different username, for example ("C:\Users\Bernard..."), the application cannot work too and I have to manually modify the pathway in the codes to run the Application.
Is there any way to search for a pathway accurately without manually type the full length out and at the same time, the Application (.exe) can work with it?
You run into the problems with absolute and relative paths.
Your first option is an absolute path. which means you will always grab the file in a particular location on the computer but if that location does not exist you can't find it
Your second option uses relative path which will look for a file based on the starting location of your program so an exe might start from a different location then the command line you build java from.
The solution is to use relative paths and to make sure the Joblist.txt file is always in one location relative to your run location.
If you want to know where the starting path of your program is you can run the following code:
System.out.println(new File(".").getAbsolutePath())
I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".
so I am in the process of making a small application.
Right now, the project works fine. I am running it through an IDE. The problem comes about when trying to run the project as a jar - which is the end result. Right now, it fails to properly load the required files (classes and simple ASCII files).
The method I am using is one based off of:
final Enumeration<URL> paths = CLASS_LOADER.getResources("");
Where CLASS_LOADER is an instance of class.getClassLoader().
This works great when not inside a jar. Inside a jar though, it seems to fail horribly. For example, in the code above, paths would be empty.
I am assuming that the fault is that the files are all within a jar - the same jar to be precise.
The class path for the manifest file is blank at the moment.
If it helps, I have two tasks that require loading files.
I need to create a list of all files that are a subclass of
another class.
I need to load a list of language files (all of
which are in the same directory).
If you need anything else to help debug this problem or provide a solution - let me know. Thanks for reading this!
For ClassLoader.getResources() to work you need to feed a path relative to the jar root. If you want to search the jar then ClassLoader public API won't help you. You have to use custom code based on java.util.jar.JarFile, like the one here.