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
Related
I'm using the Upload component in a Vaadin8 project to get a file up on the server, as shown in the source code on this page:
https://demo.vaadin.com/sampler/#ui/data-input/other/upload
After I choose the file on my pc and click upload, the window opens up just like in the sampler, and the progress bar goes all the way to the end, but the file is nowhere to be found in the project file system. Is there another step I'm supposed to be doing? How to I configure the destination folder for the uploaded files?
Taken from official documentation here Receiving upload:
The uploaded files are typically stored as files in a file system, in a database, or as temporary objects in memory. The upload component writes the received data to an java.io.OutputStream so you have plenty of freedom in how you can process the upload content.
So in your case, an uploaded file is stored as a temporary object. In V8 documentation example is cut-off, but it's presented in V7: Receiving Upload Data
public OutputStream receiveUpload(String filename,
String mimeType) {
// Create upload stream
FileOutputStream fos = null; // Stream to write to
try {
// Open the file for writing.
file = new File("/tmp/uploads/" + filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e) {
new Notification("Could not open file<br/>",
e.getMessage(),
Notification.Type.ERROR_MESSAGE)
.show(Page.getCurrent());
return null;
}
return fos; // Return the output stream to write to
}
public void uploadSucceeded(SucceededEvent event) {
// Show the uploaded file in the image viewer
image.setVisible(true);
image.setSource(new FileResource(file));
}
So the idea is that you could create a file yourself, once upload has succeed.
I am using the Export plugin in Grails for generating PDF/Excel report. I am able to generate single report on PDF/Excel button click. But, now I want to generate multiple reports on single button click. I tried for loop, method calls but no luck.
Reference links are ok. I don't expect entire code, needs reference only.
If you take a look at the source code for the ExportService in the plugin you will notice there are various export methods. Two of which support OutputStreams. Using either of these methods (depending on your requirements for the other parameters) will allow you to render a report to an output stream. Then using those output streams you can create a zip file which you can deliver to the HTTP client.
Here is a very rough example, which was written off the top of my head so it's really just an idea rather than working code:
// Assumes you have a list of maps.
// Each map will have two keys.
// outputStream and fileName
List files = []
// call the exportService and populate your list of files
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream, ...)
// files.add([outputStream: outputStream, fileName: 'whatever.pdf'])
// ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream2, ...)
// files.add([outputStream: outputStream2, fileName: 'another.pdf'])
// create a tempoary file for the zip file
File tempZipFile = File.createTempFile("temp", "zip")
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempZipFile))
// set the compression ratio
out.setLevel(Deflater.BEST_SPEED);
// Iterate through the list of files adding them to the ZIP file
files.each { file ->
// Associate an input stream for the current file
ByteArrayInputStream input = new ByteArrayInputStream(file.outputStream.toByteArray())
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.fileName))
// Transfer bytes from the current file to the ZIP file
org.apache.commons.io.IOUtils.copy(input, out);
// Close the current entry
out.closeEntry()
// Close the current input stream
input.close()
}
// close the ZIP file
out.close()
// next you need to deliver the zip file to the HTTP client
response.setContentType("application/zip")
response.setHeader("Content-disposition", "attachment;filename=WhateverFilename.zip")
org.apache.commons.io.IOUtils.copy((new FileInputStream(tempZipFile), response.outputStream)
response.outputStream.flush()
response.outputStream.close()
That should give you an idea of how to approach this. Again, the above is just for demonstration purposes and isn't production ready code, nor have I even attempted to compile it.
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();
I read an Excel 2003 file with a text editor to see some markup language.
When I open the file in Excel it displays incorrect characters. On inspection of the file I see that the encoding is Windows 1252 or some such. If I manually replace this with UTF-8, my file opens fine. Ok, so far so good, I can correct the thing manually.
Now the trick is that this file is generated automatically, that I need to process it automatically (no human interaction) with limited tools on my desktop (no perl or other scripting language).
Is there any simple way to open this XL file in VBA with the correct encoding (and ignore the encoding specified in the file)?
Note, Workbook.ReloadAs does not function for me, it bails out on error (and requires manual action as the file is already open).
Or is the only way to correct the file to go through some hoops? Either: text in, check line for encoding string, replace if required, write each line to new file...; or export to csv, then import from csv again with specific encoding, save as xls?
Any hints appreciated.
EDIT:
ADODB did not work for me (XL says user defined type, not defined).
I solved my problem with a workaround:
name2 = Replace(name, ".xls", ".txt")
Set wb = Workbooks.Open(name, True, True) ' open read-only
Set ws = wb.Worksheets(1)
ws.SaveAs FileName:=name2, FileFormat:=xlCSV
wb.Close False ' close workbook without saving changes
Set wb = Nothing ' free memory
Workbooks.OpenText FileName:=name2, _
Origin:=65001, _
DataType:=xlDelimited, _
Comma:=True
Well I think you can do it from another workbook. Add a reference to AcitiveX Data Objects, then add this sub:
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
Dim stream As ADODB.stream
Set stream = New ADODB.stream
With stream
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, adSaveCreateOverWrite
.Close
End With
Set stream = Nothing
Workbooks.Open sPath
End Sub
Then call this sub with the path to file with the off encoding.
I have looked online with mixed results, but is there a way to programmatically extract a zip file on the BB? Very basic my app will display different encrypted file types, and those files are delivered in a zip file. My idea was to have the user browse to the file on their SDCard, select it, and I extract what i need as a stream from the file. is this possible?
Use GZIPInputStream
Example:
try
{
InputStream inputStream = httpConnection.openInputStream();
GZIPInputStream gzis = new GZIPInputStream(inputStream);
StringBuffer sb = new StringBuffer();
char c;
while ((c = (char)gzis.read()) != -1)
{
sb.append(c);
}
String data = sb.toString();
gzis.close();
}
catch(IOException ioe)
{
}
Just two things:
In BB API there are only GZip and ZLib support, and no multiple files compression support, so it's not possible to compress several files and extract only one of them.
Up to my experience, such functionality will fly on simulator, but may be really performance-killing on real device
See How to retrieve data from a attached zip file in Blackberry application?
PS Actually you can implement custom multi-entries stream and parse it after decompress, but that seems to be useless, if you want this archive format to be supported in other applications.