Eliminate Pathing Implementation of .csv Data Source
Hi All,
I've got a Java functionality/capability question. I've got an application that currently uses a
Code:
Scanner s = new Scanner(new File("C:\\PCQ\\A_R.csv"));
implementation to gather data from a .csv file. At a recent code review meeting, my supervisor stated that any 'pathing' specified in application code is illegal for security reasons in our enterprise. Which leads me to my question: Is there a way to implement and distribute a compiled .jar that contains a data source such as above-mentioned without including path coding within the application code? A fellow programmer here threw out the words "Relative/Absolute Pathing", but I'm not seeing many solutions in my research thus far.
Any input is appreciated. Cheers.
Re: Eliminate Pathing Implementation of .csv Data Source
Yes you can use a configuration file for that purpose... then it is not in your code. ^^
You maybe better also include the csv file in the jar file by including it in the final export of your file.
The last part of your statement makes me think that you maybe just want to have no "absolute" pathing information in your code - then you may specify the path relative to your program folder e.g.:
"../Configuration/blabla.csv"
Re: Eliminate Pathing Implementation of .csv Data Source
1. Where is this file going to be when distributed?
2. Is it read only?
Re: Eliminate Pathing Implementation of .csv Data Source
I've been trying and trying and I can't seem to get to a solution. I'll illustrate with an example. I want to distribute a .jar file that contains the application code. The source file "foo.csv" will be located in the same root folder, e.g.:
C:\PATH\application.jar
C:\PATH\foo.csv
I need to specify the pathing for foo.csv within the application .jar file so that whatever the root folder is, the .jar will be able to 'see' foo.csv.
Below is an example of a hard-coded pathing as I understand it. How can I modify per above specifictions?
Code:
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class path {
public static void main(String[] args) throws IOException {
JPanel panel = new JPanel();
File f = new File("C:\\PATH\\test.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine()){
String l = s.nextLine();
JLabel label = new JLabel(l);
panel.add(label);
}
JFrame frame = new JFrame("Path Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Re: Eliminate Pathing Implementation of .csv Data Source
Hmm, perhaps:
Code:
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class path {
public static void main(String[] args) throws IOException {
JPanel panel = new JPanel();
//File f = new File("C:\\PATH\\test.txt");
File f = new File(".\\test.txt");
Scanner s = new Scanner(f.getCanonicalFile());
while(s.hasNextLine()){
String l = s.nextLine();
JLabel label = new JLabel(l);
panel.add(label);
}
JFrame frame = new JFrame("Path Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.add(panel);
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
with C:\Program Files\eclipse\workspace\path\test.txt
Re: Eliminate Pathing Implementation of .csv Data Source
Ensure the classpath in the manifest includes . and that should work.