Solved

input file in the classpath

Posted on 2013-12-20
8
348 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 16

Expert Comment

by:krakatoa
Comment Utility
Look at Class class in Java.
0
 

Author Comment

by:roy_sanu
Comment Utility
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 16

Expert Comment

by:krakatoa
Comment Utility
String classpath = System.getProperty("java.class.path");
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
:)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now