Solved

input file in the classpath

Posted on 2013-12-20
8
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

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 16

Expert Comment

by:krakatoa
ID: 39731488
String classpath = System.getProperty("java.class.path");
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 35

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 105
jsp login check 12 67
Crystal Reports Licensing Questions 4 87
netbeans can not run self signed app 8 38
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 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