wood burning stoves 2.0*
The moose likes Java in General and the fly likes File path issues while reading an xsl file in java   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "File path issues while reading an xsl file in java   " Watch "File path issues while reading an xsl file in java   " New topic
Author

File path issues while reading an xsl file in java

chandana lakku
Greenhorn

Joined: Nov 24, 2009
Posts: 1
Hi,

I want to read an xsl file from a java class(which is located in a package com.a.b.c)

xsl file and the java class resides in the same package.(com.a.b.c)

The possible ways that i tried:

String xsltFile = "test.xsl";
String xsltFile = new File("test.xsl").toURL().toExternalForm( );
String xsltFile = new File("test.xsl").getAbsolutePath();
String xsltFile = new File("test.xsl").getPath( );
String xsltFile = new File("test.xsl").getPath( );
String xsltFile = "com/a/b/.../test.xsl"

All the above methods returns file path (where the application is deployed) but the actual xsl file doesn't exist there.

How should I read this "xsl file" from my java class which is located in the same path?

Could some one help me out on it?

Thanks & Regards,
Chandana.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Stop using File and String, and start using Class.getResource and Class.getResourceAsStream instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Hi chandana Try out with this..


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class FilePath {
public static void main(String[] args) {
try{
FileInputStream fs=new FileInputStream("Path to your Xsl file");
DataInputStream in=new DataInputStream(fs);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String strLine;
while((strLine=br.readLine())!=null){
System.out.println(strLine);
}
in.close();
}catch(Exception e){
System.out.println("Error:"+e.getMessage());
}
}
}

I am sure it will work as it worked for me.. Enjoyyyyyyyy..........
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Please both use code tags.

Shirish, that will only work when the file is always located in the same folder. You can't assume that the application has been started in its root folder, or the folder where the JAR file is located. Resources are always relative to a class or the entire class path, so those seem more appropriate.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38016
    
  22
And welcome to the Ranch

shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Shirish, that will only work when the file is always located in the same folder. You can't assume that the application has been started in its root folder, or the folder where the JAR file is located. Resources are always relative to a class or the entire class path, so those seem more appropriate.


Hi Rob,

Yes if the location of file changes, then we do have properties file were we can change the path of the file to be read..

If you do have any proper solution than this then please share it..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

I already mentioned Class.getResourceAsStream. So instead of you write And if you call this in an instance method you can use getClass().getResourceAsStream(...).
shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Thanks Rob, i Got it..
 
Consider Paul's rocket mass heater.
 
subject: File path issues while reading an xsl file in java
 
Similar Threads
java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DocumentImpl
Problem with class.getResourceAsStream
Urgent: simple transformer failing with strange file exception
File Class
Versioning using manifest for multiple packages in a single jar