Get resource from another folder in Java - properties

My project has a folder for resource and a folder for src. All my code are in src and all my property files are in resource (it's set as resource root too)
So I want to read xyz.properties in my code as input stream
I tried
InputStream in = getClass().getResourceAsStream("/resources/xyzz.properties");
and
InputStream in = getClass().getResourceAsStream("xyzz.properties");
and neither worked. They all end up being null - only when I move xyzz.properties to the package that has the class that uses the above code does the second code snippet work. What should I be using for the path if I want to keep the current location of the property file?

Like this
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("xyzz.properties");
Or like this
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("xyzz.properties");

Related

Can the Image path for the AddImage method in MigraDoc be used for the Visual Studio project subfolders?

The following example shows a very simple construction for creating a PDF with MigraDoc. The most parts of it are taken from the examples provided in the MigraDoc - Wiki documentation.
I have commented all the most few steps for the job. The path for the "AddImage" points to the subfolder "Images" which is a subfolder of the folder "Resources" created in the root of the console application of a Visual Studio (VS) project.
The most recent MigraDoc library is added to the VS - project via "NuGet" just yesterday.
The application creates the PDF output file unfortunately without the provided image file, if it is used in another machine. Because the execution file has then no access to the subfolders created in VS project.
Is there any solution to this problem?
public class Program
{
static void Main(string[] args)
{
// Create a MigraDoc document
var document = new Document();
// Add a section the document
var section = document.AddSection();
// .....
/*
------------------------------- NOTE -------------------------------
The path "../../Resources/Images/MigraDoc.png" is valid only for the developer machine!
A copy of the content of the "Debug" or "Release" folder any where else does not show the image in the output PDF.
Because such a copy has no access to the subfolder "/Resources/Images" of the Visual Studio project on the developer machine.
*/
section.Headers.Primary.AddImage("../../Resources/Images/MigraDoc.png");
// Create a renderer for PDF that uses Unicode font encoding.
var pdfRenderer = new PdfDocumentRenderer(true);
// Set the MigraDoc document.
pdfRenderer.Document = document;
// Create the PDF document.
pdfRenderer.RenderDocument();
// Save the PDF document...
var filename = "Invoice.pdf";
// Create the output directory
Directory.CreateDirectory("PDF");
// Create the ouptput file path
var savePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\PDF\\" + filename;
// Delete the output file if it already exists
if (File.Exists(savePath))
File.Delete(savePath);
// Save the output file
pdfRenderer.Save(savePath);
// Start a the default PDF viewer from the operation system
Process.Start(savePath);
}
}
A path beginning with ..\..\ relies on the current directory. For a portable solution, include the images you need in your deployment.
Recommended usage: The MigraDoc Document class has an ImagePath property. If your installation folder has an Images folder, just locate your .EXE file and set the ImagePath property accordingly.

Where is the file path of IFormFile?

When I receive a file from C# to iformfile, how do I know the file path of that file?
If I don't know, how do I set the path?
public async Task<JObject> files(IFormFile files){
string filePath = "";
var fileStream = System.IO.File.OpenRead(filePath)
}
In this document, some instructions on IFormFile are introduced:
Files uploaded using the IFormFile technique are buffered in memory or on disk on the server before processing. Inside the action method, the IFormFile contents are accessible as a Stream.
So for IFormFile, you need to save it locally before using the local path.
For example:
// Uses Path.GetTempFileName to return a full path for a file, including the file name.
var filePath = Path.GetTempFileName();
using (var stream = System.IO.File.Create(filePath))
{
// The formFile is the method parameter which type is IFormFile
// Saves the files to the local file system using a file name generated by the app.
await formFile.CopyToAsync(stream);
}
After this, you can get the local file path, namely filePath.

Get module relative path of IFile in Eclipse

I have created an IFile instance as follows:
IPath filePath = new Path(WebContent/folder1/file1.html);
IFile file = project.getFile(filePath);
That file will be deployed on a server and I need to get the module relative path (in this case /folder1/file1.html).
I have tried using the IFlatFile class like this:
IFlatFile flatFile = new FlatFile(file, file.getName(), file.getProjectRelativePath());
IPath modulePath = flatFile.getModuleRelativePath();
But the getModuleRelativePath() seems to be returning the same path I provided to the constructor when the instance was created.
Is there any other way to get the module relative path of a given file?

Saxon .Net read xml from stream

all examples show how to read an xml from an local file. But how do I read a xml from a url or a stream and process it further?
Example: http://www.oreillynet.com/xml/blog/2006/03/hello_saxon_on_net_an_aspnet_i.html
thanks in advance
Look for XsltExamples.cs in the saxon-resources download available on both Sourceforge and www.saxonica.com. The very first example seems to do what you are asking for.
public static void ExampleSimple1(String sourceUri, String xsltUri) {
// Create a Processor instance.
Processor processor = new Processor();
// Load the source document
XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourceUri));
// Create a transformer for the stylesheet.
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltUri)).Load();
// Set the root node of the source document to be the initial context node
transformer.InitialContextNode = input;
// Create a serializer
Serializer serializer = new Serializer();
serializer.SetOutputWriter(Console.Out);
// Transform the source XML to System.out.
transformer.Run(serializer);
}
Are you using an XmlDocument object for reading the XML? If so, you'll want XMLDocument.Load() method, which can take a file path or URL, TextReader or Stream as input.
Likewise, XDocument.Load()(msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.load(v=vs.110).aspx) has a similar set of overloads.

Storing/Saving XML in local asset folder in AIR

AIR in general seems to be storing the xml file in the path where the app is installed.
I am generating an xml and I want to save/store a xml file in local asset folder of AIR application.
Any thoughts on doing this.
The File class has some static variables that point to local directories:
From the docs:
File.applicationStorageDirectory — a storage directory unique to each installed AIR application
File.applicationDirectory — the read-only directory where the application is installed (along with any installed assets)
File.desktopDirectory — the user's desktop directory
File.documentsDirectory — the user's documents directory
File.userDirectory — the user directory
Creating a pointer to the file
Usually you'll want to store files like these in File.applicationStorageDirectory.
So to create the file do:
File.applicationStorageDirectory.resolvePath("my-config.xml");
Alternatively, you can let the user choose where to store the file by using File#browseForSave(), which will display a native 'save' window to choose the location.
Writing the content
Open a FileStream for the File in 'write' mode and write an XML string to the file.
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTF(myXmlContent);
fs.close();
look at http://www.adobe.com/devnet/air/flex/quickstart/articles/xml_prefs.html
http://www.thetechlabs.com/xml/how-to-build-a-contact-manager-in-air-using-xml-part-2/
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html
var prefsFile:File = File.applicationStorageDirectory;
prefsFile = prefsFile.resolvePath("preferences.xml");
stream = new FileStream();
stream.open(prefsFile, FileMode.WRITE);
stream.writeUTFBytes(outputStringXML);
Or something like:
var saveStr:String = xmlToSave.toXMLString();
var file:File = new File('app-storage:/data.xml');
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeUTFBytes(saveStr);
fs.close();