Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

input file in the classpath

Posted on 2013-12-20
8
Medium Priority
?
360 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
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!

 
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

Technology Partners: 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

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

721 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