Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 22512
122
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: 22512
122
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: 74393
335
  • 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: 22512
122
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..
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic