Hi I have created a model with Apache Jena and can output it this way:
model.write(System.out, "Turtle");
Is it possible to save the turtle file as .ttl on disk?
To write your model to a file you just pass an OutputStream instead of System.out, like this:
OutputStream out = new FileOutputStream("output-model.ttl");
RDFDataMgr.write(out, model, Lang.TURTLE);
or:
OutputStream out = new FileOutputStream("output-model.ttl");
model.write(out, Lang.TURTLE);
Don't forget to close your stream once the file is fully written.
Is there any way through which we can write the response from GUI(I was updating some data into a text box) into a file(In any format) using java code.
I am updating some values in GUI and in our java code I want to get all that data and store it into a file.
Yes you can write objects into a file.ser (serialized file).
Below is an example of how to write to a file using FOS.
FileOutputStream fout = new FileOutputStream("c:\\object.ser");
ObjectOutputStream oos = new ObjectOutputStream(fout);
Objec Obj ;
oos.writeObject(Obj);
oos.close();
My response from server is in xml format, I am able to open this file using excel and excel will auto generate the table for me. After that I can save this file as xls, and my java program is capable of retreiving those information through loops. My question is how do I use java code to replace the step " open xml using excel, wait for auto generated table, then save this file as xls". I tried several ways, it is not going to work, if I open the xml file using java code, it will give me invalid header error. Any help would be greatly appreciated. My code is as follow:
FileInputStream input = new FileInputStream("Y:\\xx\\xx .xml");
POIFSFileSystem fs = new POIFSFileSystem( input );
HSSFWorkbook wb = new HSSFWorkbook(fs);
FileOutputStream fos = new FileOutputStream("Y:\\xx\\xx.xls") ;
wb.write(fos);
fose.close();
This will give me invalid header error, please help.
I created a file using FileOutputStream and it is an excel file (using HSSF Liberary)
FileOutputStream fileOut = new FileOutputStream(text+".xls");
then I write what I need in my excel file (workbook) and then close the file
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
After closing it I need to display the path of the file to user, (I know that it creates in the folder of my application but I still need to display it to user, maybe via joption/message box)
I tried this :
String absolutePath = fileOut.getAbsolutePath();
JOptionPane.showMessageDialog(null, absolutePath);
but it shows error and it says that it cannot find the method "getAbsolutePath". what should I do ? is there anyway that I can get this path ?
You can change your code to use a file instead as an intermediary.
File myFile = new File(text + ".xls");
FileOutputStream fileOut = new FileOutputStream(myFile);
And then just get the path of that:
String absolutePath = myFile.getAbsolutePath();
Make sure to close the stream when you're done:
fileOut.close();
Ideally though, you shouldn't just create the file wherever the Java path happens to be set. You should probably rethink this and instead ask the user where they want to save the file.
Use new File(text+".xls").getAbsolutePath(). The FileOutputStream doesn't allow accessing the underlying File.
You should get into the habit of reading the javadoc instead of trying random methods. You'll then see what methods exists and what methods don't.
I'm reading a bunch of files from an FTP. Then I need to unzip those files and write them to a fileshare.
I don't want to write the files first and then read them back and unzip them. I want to do it all in one go. Is that possible?
This is my code
FTPClient fileclient = new FTPClient();
..
ByteArrayOutputStream out = new ByteArrayOutputStream();
fileclient.retrieveFile(filename, out);
??????? //How do I get my out-stream into a File-object?
File file = new File(?);
ZipFile zipFile = new ZipFile(file,ZipFile.OPEN_READ);
Any ideas?
You should use a ZipInputStream wrapped around the InputStream returned from FTPClient's retrieveFileStream(String remote).
You don't need to create the File object.
If you want to save the file you should pipe the stream directly into a ZipOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(out);
// do whatever with your zip file
If, instead, you want to open the just retrieved file work with the ZipInputStream:
new ZipInputStream(fileClient.retrieveFileStream(String remote));
Just read the doc here and here
I think you want:
ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream( out.toByteArray() ) );
Then read your data from the ZipInputStream.
As others have pointed out, for what you are trying to do, you don't need to write the downloaded ZIP "file" to the file system at all.
Having said that, I'd like to point out a misconception in your question, that is also reflected in some of the answers.
In Java, a File object does no really represent a file at all. Rather, it represents a file name or *path". While this name or path often corresponds to an actual file, this doesn't need to be the case.
This may sound a bit like hair-splitting, but consider this scenario:
File dir = new File("/tmp/foo");
boolean isDirectory = dir.isDirectory();
if (isDirectory) {
// spend a long time computing some result
...
// create an output file in 'dir' containing the result
}
Now if instances of the File class represented objects in the file system, then you'd expect the code that creates the output file to succeed (modulo permissions). But in fact, the create could fail because, something deleted the "/tmp/foo", or replaced it with a regular file.
It must be said that some of the methods on the File class do seem to assume that the File object does correspond to a real filesystem entity. Examples are the methods for getting a file's size or timestamps, or for listing the names in a directory. However, in each case, the method is specified to throw an exception if the actual file does not exist or has the wrong type for the operation requested.
Well, you could just create a FileOutputStream and then write the data from that:
FileOutputStream fos = new FileOutputStream(filename);
try {
out.writeTo(fos);
} finally {
fos.close();
}
Then just create the File object:
File file = new File(filename);
You need to understand that a File object doesn't represent any real data on disk - it's just a filename, effectively. The file doesn't even have to exist. If you want to actually write data, that's what FileOutputStream is for.
EDIT: I've just spotted that you didn't want to write the data out first - but that's what you've got to do, if you're going to pass the file to something that expects a genuine file with data in.
If you don't want to do that, you'll have to use a different API which doesn't expect a file to exist... as per Qwerky's answer.
Just change the ByteArrayOutputStream to a FileOutputStream.