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
Solved

input file in the classpath

Posted on 2013-12-20
8
352 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
servlet example 11 49
Tomcat: Unable to run tomcat service. 2 23
java mysql insert application 14 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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