I am working on blackberry, I have a Jar file jaudiotagger, but in blackberry java.io.File not available. Is there any Jar file available for it?
Usual java.io.File Java API does not work on BB.
See BB API documentation for javax.microedition.io.Connector and javax.microedition.io.file.FileConnection.
You will need to do something like:
FileConnection fconn = (FileConnection) Connector.open("file:///CFCard/newfile.txt");
// If no exception is thrown, then the URI is valid, but the file may or may not exist.
if (!fconn.exists()) fconn.create(); // create the file if it doesn't exist
OutputStream os = fconn.openOutputStream();
//...
fconn.close();
Related
I'm trying to parse text files using apache tika. I pass a file to the following code:
public String parseFile(File file) throws Exception{
Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
BodyContentHandler handler = new BodyContentHandler(1000000000);
FileInputStream is = new FileInputStream(file);
parser.parse(is, handler, metadata, parseContext);
System.out.println("Content: "+ handler.toString());
return handler.toString();
}
My issue is that Tika recognises SOME files but not ALL files. I'm sorry but I'm unable to actully attach the files that do not work (corporate reasons). If I do find an acceptable example, I will share it. I wanted to know if there was something blatantly obvious which I am doing wrong. I'm not sure how the BodyContentHandler class works. In most of the tutorials I read online, the code is as follows :
ContentHandler handler = new BodyContentHandler ();
However, my eclipse refuses to accept that. And asks me to cast BodyContentHandler to ContentHandler which causes other problems.
I'm trying to support text files, pdf files, word documents, excel files. The text Files that DO NOT work have a certain property: I copy paste an email thread from outlook to notepad. Most files that do not work are of this kind.
I am trying to create HTML files using XSLT, I have used xml file and xsl files to create HTML file. Here some other xsl files which are located in same location are included in xsl file by using <xsl:include href="temp.xsl"/>.
Here Xsl files are located in "D:/XSL_Folder/" path.
I am running Main.java file which is located in D:/Workspace/Webapp_Project/ path.
When i try to create HTML files by using passing "D:/XSL_Folder/root.xsl" and "D:/XML_Folder/data.xml" files to Main.java as arguments, I am getting following error while creating Templates.
Templates lTemplates = TransformerFactory.newInstance().newTemplates(new StreamSource(new FileInputStream(lFileXSL)));
ERROR: 'D:\Workspace\Webapp_Project\temp.xsl (The system cannot find the file specified)'
FATAL ERROR: 'Could not compile stylesheet'
12:20:07 ERROR f.s.t.v.v2.dao.impl.DocUnitDaoImpl - Error while creating a new XslTransformerGetter. The path to the XSL may be wrong.
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:885) ~[na:1.7.0_13]
In error report we can see that parser is checking included xsl file in project path (D:\Workspace\Webapp_Project), not in the path where root.xsl file is located (D:/XSL_Folder/).
Can anyone suggest me why parser searching xsl file in project folder in the path where root.xsl file is located and how to fix this problem?
Code I'm using to create HTML file by using XSL and XML file :
public static void simpleTransform(InputStream lXmlFileStream, File lXSLFile,
StreamResult lHtmlResult, Map<String, String> lArguments) {
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer =
tFactory.newTransformer(new StreamSource(lXSLFile));
for (Entry<String, String> lEntrie : lArguments.entrySet()) {
transformer.setParameter(lEntrie.getKey(), lEntrie.getValue());
}
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new StreamSource(lXmlFileStream), lHtmlResult);
}
catch (Exception e) {
e.printStackTrace();
}
}
You have tagged the question "saxon", and you have said you are using XSLT 2.0, but the error messages show that you are using Xalan. If you specifically want to use Saxon then the best way is to avoid using the JAXP classpath search and instantiate Saxon directly - in place of TransformerFactory.newInstance(), use new net.sf.saxon.TransformerFactory().
Supplying a File as the argument to StreamSource ought to be OK; but I would like to see how the File lXSLFile object is created. My suspicion would be that you have done something like new File ("root.xsl") and it has been resolved relative to the current directory.
You may try to use <xsl:include href="resolve-uri('temp.xsl')"/> instead of <xsl:include href="temp.xsl"/> to avoid this problem.
I try to get a file in my bundle with:
File file = new File(bundleContext.getBundle().
getResource("image/logo.jpg").toURI());
The result is a IllegalArgumentException with the cause "URI scheme is not "file".
This is logical, but how should i open a file with this URL (bundle://28/image/logo.jpg)?
If i use the regular ClassLoader i get the same result.
EDIT:
My Solution:
URL url = this.getClass().getClassLoader().getResource("image/logo.jpg");
InputStream in = new BufferedInputstream(url.openStream());
You cannot open a file since there might not be a file ... So just get the input stream instead. That works for all URLs/URIs.
Is it possible to open file in append mode in blackberry? In Connector class there are constants READ, WRITE, READ_WRITE but I didn't find any constant for append mode.
It is possible, though it isn't a separate mode:
FileConnection fc = (FileConnection) Connector.open(pathToFile, Connector.READ_WRITE);
OutputStream out = fc.openOutputStream(fc.fileSize());
// Now you can write to the output stream and it will append to the end of the file
I need to read a resource file from classpath in my BlackBerry application. The directory structure of my project is pretty common: under src directory there are 2 child dirs, one represents source packages root, another - resources root.
When I try to read any resource from classpath Class.getResourceAsStream method retures null
InputStream rStream = null;
String path = "/res/default_config.xml";
try {
rStream = getClass().getResourceAsStream(path);
} finally {
try {
if (rStream != null) {
byte[] data = IOUtilities.streamToBytes(rStream);
System.out.println(new String(data));
rStream.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
How should I read classpath resource properly?
And have you tried to put xml file directly into src folder and use getClass().getResourceAsStream("default_config.xml"); ?
Actually cannot reproduce.
Tested on simulator 8800 eJDE 4.2.1.
File was placed in src/res/ folder.
I think you specified the path as incorrect way. You just remove the / from the beginning of the path you specified. If you are specifying /. then it will check for you resource folder
Even though it's generated as a COD file for running on the device, the JAR file is also created each build. It might be worth checking to make sure your xml file is being put in the directory that you expect it to be in as you can definitely store resources in sub-directories in your application and retrieve them using getClass().getResourceArStream();