How to write a GUI response into a txt file - java

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();

Related

InflateInputStream.Read() causing ANR in my app. Am trying to decompress a text file from the app. This function works well in Intelij

I want to decompress a file from my app using the function below. This function works well from my laptop when i run the code in InteliJ; but causes ANR when I install the app on my device. And fails to decompress the file.
//
FileInputStream fis=new FileInputStream(getFilesDir()+"/file1.cmp");
//assign output file: file3 to FileOutputStream for reading the data
FileOutputStream fos=new FileOutputStream(getFilesDir()+"/file3.txt");
//assign inflaterInputStream to FileInputStream for uncompressing the data
InflaterInputStream iis=new InflaterInputStream(fis);
//read data from inflaterInputStream and write it into FileOutputStream
int data;
while((data=iis.read())!=-1)
{
fos.write(data);
}
//close the files
fos.close();
iis.close();

Exporting/Importing ArrayList with objects to my application with file

I am building an application which contains an array and 2 ArrayLists with objects.
One ArrayList for Customers, one for Orders and the Array is for the default items of the shop.
Now I am in the GUI implementation and I am trying to add this functionality:
I want to export to a file the 2 object ArrayLists I got.
I have succesfully exported the ArrayLists like this(the chooser just selects the folder):
FileOutputStream fos;
ObjectOutputStream oos;
fos = new FileOutputStream(chooser.getSelectedFile()+"/orders");
oos = new ObjectOutputStream(fos);
oos.writeObject(save);
oos.close();
Now, I close the application.
I want to import the saved ArrayLists to my app, but even when it says it is successful, my methods for running through the ArrayLists don't work(for example a method that just displays the contents of them).
I use this code to import them:
FileInputStream fis = new FileInputStream(chooser.getSelectedFile()+"/orders");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<CafeOrders> itemOrders = (ArrayList<CafeOrders>) ois.readObject();
ois.close();
Of course there are try-catch etc in the code, I am just posting the import/export methods.
There are separate buttons for each and they both seem to work(I get my file which contains data as well).
I am implementing my classes with Serialization.

Creating ZIP file in memory

I need to create a ZIP file which consists of files that are created on-the-fly and have no persistence on the file system.
For example: I want to create an SQLite database in memory and after populating it with data I want to add it to a - not yet existing - ZIP file and than I want to actually write this ZIP file to the file system.
I found several approaches where the files, which are going to be the content of the archive, have to be read from the file system.
Is there actually a way to archive what I want to do? I hoped that compress-commons would help me but apparently they don't.
Do I miss something?
If the in memory object you are trying to zip is serializable, then this is quite easy.
You can take any serializable instance and turn it in to a byte[]. I have a utility method to do this:
public static byte[] convertToBytes(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
out.flush();
return bos.toByteArray();
}
}
Once you have a that object represented in bytes, you can use a ZipOutputStream to zip it up:
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream out = new GZIPOutputStream(bos); ) {
out.write(bytes);
out.finish();
byte[] compressed = bos.toByteArray(); // this is my compressed data
}
(I use Gzip here for simplicity but you can also create a zip with multiple entries, for example).

Write Object from List to File, using ObjectOutputStream

I'm trying to write the content of a list (object) to disk using ObjectOutputStream.
This is the relevant code:
//Input Filetype is .xlsx with an embedded File (also .xlsx), Output Filetype should be .xlsx (Type of embedded File)
//This code should save the embedded File to D:\\...
List<HSSFObjectData> extrList = new ArrayList<HSSFObjectData>();
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
extrList = embeddedWorkbook.getAllEmbeddedObjects();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\scan_temp\\emb.xlsx"));
oos.writeObject(extrList);
oos.flush();
oos.close();
This code creates a file called emb.xlsx, but the content is not what I expected. If I try to open using notepad, it's something like:
¬í sr java.util.ArrayListxÒ™Ça I sizexp w x
What am I doing wrong here? Thanks for any help.
What am I doing wrong here?
You are doing several things wrong:
You are misusing the .xlsx extension for a file of serialized objects. That extension is for Excel spreadsheets in XML format. You should use something like .bin, .data, .ser, etc.
You are using Serialization when you should be using the I/O facilities built into POI.
You are trying to read a binary file with a text editor.
You are redundantly callling flush() before close().
If anyone else is trying the same thing as I did, use the following code (works!):
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(InputStream);
FileOutputStream fileOut = new FileOutputStream("/outputfilepath.xls");
embeddedWorkbook.write(fileOut);
fileOut.close();
Don't try to get the embedded objects into a list. Just use .write()and that's all. :-)

Sending file through ObjectOutputStream and then saving it in Java?

I have this simple Server/Client application. I'm trying to get the Server to send a file through an OutputStream (FileOutputStream, OutputStream, ObjectOutputStream, etc) and receive it at the client side before saving it into an actual file. The problem is, I've tried doing this but it keeps failing. Whenever I create the file and write the object I received from the server into it, I get a broken image (I just save it as a jpg, but that shouldn't matter). Here are the parts of the code which are most likely to be malfunctioning (all the seemingly un-declared objects you see here have already been declared beforehand):
Server:
ObjectOutputStream outToClient = new ObjectOutputStream(
connSocket.getOutputStream());
File imgFile = new File(dir + children[0]);
outToClient.writeObject(imgFile);
outToClient.flush();
Client:
ObjectInputStream inFromServer = new ObjectInputStream(
clientSocket.getInputStream());
ObjectOutputStream saveImage = new ObjectOutputStream(
new FileOutputStream("D:/ServerMapCopy/gday.jpg"));
saveImage.writeObject(inFromServer.readObject());
So, my problem would be that I can't get the object through the stream correctly without getting a broken file.
A File object represents the path to that file, not its actual content. What you should do is read the bytes from that file and send those over your ObjectOutputStream.
File f = ...
ObjectOutputStream oos = ...
byte[] content = Files.readAllBytes(f.toPath);
oos.writeObject(content);
File f=...
ObjectInputStream ois = ...
byte[] content = (byte[]) ois.readObject();
Files.write(f.toPath(), content);
You are not actually transferring the file, but the File instance from Java. Think of your File object as a (server-)local handle to the file, but not its contents. For transferring the image, you'd actually have to read it on the server first.
However, if you're just going to save the bytes on the client anyway, you can forget about the ObjectOutputStream to begin with. You can just transfer the bytes stored in the File. Take a look at the FileChannel class and its transferTo and transferFrom methods as a start.

Categories

Resources