How to swap file path and queries from Java code - java

I have a "how-should-I-implement- it- on -the- best- way -in -java" question. I have written a program in Java that reads a csv file that is stored on a fixed directory. (BufferedReader & FileReader) The file is then read out and the data is displayed in an XYDataset (XY graph) using jFreeChart.
Question: The program should now run without a development interface, that means I export it as .exe. However, since the directory is now to be "independent", that means it can be changed at any time, I should now save the directory externally and / or stored it should be without the code must be changed. In addition, the data from the directory (csv-files) should also be called regardless of the code. That means there are different .csv files inside the directory & you should sort by name & most recent date. That It should always open the latest .csv file with the "1" in the name and another time the .csv file with the "2" in the name. My question is now: How should I implement this best?
My first idea would be an XML file to access my Java program, but I do not know exactly how I should store the file path there.
maybe like this: ? file: /// H: /Test/Testfile.csv
Does somebody know how to "outsourced" & then accessed a directory or other ideas, how I could implement that outside the "real" code?
Would be very happy about help!

Related

Writing Information (and Image) to a Jar File from another Java Program

Possibly a duplicate, though I doubt so since I have not seen anything so far completely answering my criteria in a way that I can complete my program
Background
What I need is to access another jar, from a seperate jar, read and write files to that jar. So far what I have done is change the jar to a zip and then I can delete files, but the problem I am having is with writing files back in, specifically image files (.txt works perfectly fine)
Question
How do I write image files to a zip (that was originally a jar) from another java program (in the end product another jar)
Note
I have looked around and most sources say this is not possible, but those questions dealt with this during the running of a program, my special case is that the other program is not running, but in file format. All I want to do is write and image in and convert it back to a jar and not have any problems with running that jar in the end.
Thank you!
Use FileSystems to access, write and replace the contents of the jar file:
try (FileSystem fs = FileSystems.newFileSystem(Paths.get("path/file.jar"), null)) {
Files.copy(Paths.get("path/to/image"), // path to an external image
fs.getPath("image.jpg"), // path inside a jar file
StandardCopyOption.REPLACE_EXISTING);
}

Getting a time of deleted folder \ file

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).

Java Program to find image in .java or .jsp file

I have a project with around 50 java classes/files (this 50 files includes both JSP and Java files). In this files I have written code to display abc.jpg image on my JSP pages. Now I want to write a java program which will replace all the abc.jpg images by xyz.jpg image in my project. I have no clue how I can do this. Also suggest if there is any free tool available to do the same.
What you want to do, will be done using following steps. Please note that you can find information about each step in StackOverflow itself in different posts:
1. Create a traversal policy for a directory in which all files are present, to get path of all .java and .jsp files.
2. Read all files one-by-one and write its content to another temporary file line by line.
3. While reading individual line, check whether it contains the name of .jpg file whose name you want to update. If yes the replace the name with new name and write the current updated line in temporary file.
4. Once the file reading is completed, delete the original file and rename the temporary file to original one.
5. Now you will have a same file with updated names of .jpg files.
6. Repeat the steps from 2-5 until all files are read and write.
Hope this will help. And yes you have to search these methods in SO itself. :-)

How can I get Eclipse to find my .txt file in java ON A MAC? [duplicate]

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".

Relative/Absolute Path in Java

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".

Categories

Resources