• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File path issues while reading an xsl file in java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stop using File and String, and start using Class.getResource and Class.getResourceAsStream instead.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch

 
shirish katti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob, i Got it..
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic