Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

input file in the classpath

Posted on 2013-12-20
8
Medium Priority
?
361 Views
Last Modified: 2013-12-28
Hi,

How to set the input file from the classpath instead of hardcording...

public static final String path =
                  "D:\\MyDev\\Consum\\-identy\\MID.csv";      
      

Thanks
0
Comment
Question by:roy_sanu
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 39731464
Look at Class class in Java.
0
 

Author Comment

by:roy_sanu
ID: 39731475
is it this
InputStream in = this.getClass().getResourceAsStream("/MID.csv");
      InputStreamReader inputReader = new InputStreamReader(in);

Now how can i convert a InputStreamReader  to a string as
I need to pass it inputReader  as a string
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 39731488
String classpath = System.getProperty("java.class.path");
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 36

Expert Comment

by:mccarl
ID: 39731566
is it this
Yes, getResourceAsStream() would be the preferred way to go, to access a file that's on the classpath. Whether this .csv file *should* be on the classpath is another question...

Now how can i convert a InputStreamReader  to a string as
I need to pass it inputReader  as a string
Are you sure that you want the whole file as a String? I would guess that the code after that would need to split that string based on newline character anyway so that you can process each individual line. It sounds like it would be wasted effort to concatenate the whole file into one string only to have it split up again.

Can you refactor the code to accept a Reader object? Or at least a List/array of Strings that are each line in your file? If you can post more of your code that uses the content of this file, we can help further.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39731754
Loading files as resources is preferable to using relative or even absolute file paths. In order to do that, they have to be on the classpath

http://technojeeves.com/joomla/index.php/free/126-loading-files-as-resources-in-java-with-netbeans
http://technojeeves.com/joomla/index.php/free/120-loading-files-as-resources-in-java-with-eclipse

Loading a file directly into a String is something i often do, for various reasons

http://technojeeves.com/joomla/index.php/free/93-file-to-string-in-java
0
 

Author Comment

by:roy_sanu
ID: 39732271
I want my csv file need to be in classpath,i have added the csv file in the classpath(i.e my resource) package in eclipse...
 
 Here is my piece of code where i was trying to comment  the code and add
String classpath = System.getProperty("\\MID.csv");
is it not the correct way to set the correct path......

and i need to pass the String classpath in the  jobParameterMap
jobParameterMap.put("inputFilePath", new JobParameter(classpath ));


public class AppTest {

	
//	public static final String path = 
  //                "D:\\MyDev\\Consum\\identityy\\MID.csv";    
	
	String classpath = System.getProperty("\\MID.csv"); 
	
	
	@Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;
 
    @Test
    public void launchJob() throws Exception {
		
	Map<String, JobParameter> jobParameterMap = new HashMap<String, JobParameter>();
	jobParameterMap.put("inputFilePath", new JobParameter(path));
	JobParameters jobParameters = new JobParameters(jobParameterMap);
		
}
 

Open in new window

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 39732308
is it not the correct way to set the correct path......
You don't need to set any path. All that's required is to do what is specified in my link about Eclipse
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39743688
:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question