I have used this code to store Object to a file:
try{
FileOutputStream saveFile=new FileOutputStream("SaveObj.sav");
ObjectOutputStream save = new ObjectOutputStream(saveFile);
save.writeObject(x);
save.close();
}
catch(Exception exc){
exc.printStackTrace();
}
}
}
How remove the single Object??
How clear the file??
Well, emptying out a file is very easy -- just open it for writing, and close it again:
new FileOutputStream("SaveObj.sav").close();
That will empty it out. If you were trying to erase one object out of many, though, that's a lot more complicated. You'd either have to read in all the objects and write out only the ones you want to keep, or you'd have to keep an index of the file offsets at which each object starts (probably in a separate file.) At that point you'd want to consider using an object database instead.
Ernest is right in that a removal of a particular object from the object-stream is slightly more complicated. He is also right that when you want to empty a file, you can simply open it for writing and close it. But if you want to remove it from the file-system, it is fine to do it using the File object (do not forget to handle the exceptions and return values correctly). The following example may not be perfect, but it should give you a hint on how to achieve your goals with pure Java. Hope this helps...
package test;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
String filename = "object.serialized";
{
List objects = new ArrayList();
objects.add("String1");
objects.add("String2");
objects.add("String3");
writeObjectsToFile(filename, objects);
}
{
List objects = readObjectsFromFile(filename);
objects.remove(1);
writeObjectsToFile(filename, objects);
}
{
List objects = readObjectsFromFile(filename);
for (Object object : objects) {
System.out.println(object);
}
}
emptyFile(filename);
deleteFile(filename);
}
private static void emptyFile(String filename) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(filename);
} finally {
if (os != null) {
os.close();
}
}
}
private static void deleteFile(String filename) {
File f = new File(filename);
if (f.delete()) {
System.out.println(filename + " deleted sucessfully...");
} else {
System.out.println(filename + " deletion failed!");
}
}
private static void writeObjectsToFile(String filename, List objects) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(filename);
ObjectOutputStream oos = new ObjectOutputStream(os);
for (Object object : objects) {
oos.writeObject(object);
}
oos.flush();
} finally {
if (os != null) {
os.close();
}
}
}
private static List readObjectsFromFile(String filename) throws IOException, ClassNotFoundException {
List objects = new ArrayList();
InputStream is = null;
try {
is = new FileInputStream(filename);
ObjectInputStream ois = new ObjectInputStream(is);
while (true) {
try {
Object object = ois.readObject();
objects.add(object);
} catch (EOFException ex) {
break;
}
}
} finally {
if (is != null) {
is.close();
}
}
return objects;
}
}
Outputs:
String1
String3
object.serialized deleted sucessfully...
I know there was a long time from this subject, but just to help future coming people, what works for me was to write the object again as a null value:
public static void writeIncidentsObjectsInCache(Object object) throws IOException {
writeObject(INCIDENTS_CACHE, object); }
public static Object readIncidentsObjectFromCache() throws IOException,
ClassNotFoundException {
return readObject(INCIDENTS_CACHE); }
public static void clearIncidents() throws IOException, ClassNotFoundException {
writeIncidentsObjectsInCache(null); }
public static void writeObject(String key, Object object) throws IOException {
FileOutputStream fos = TheAAApp.getApp().openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
public static Object readObject(String key) throws IOException,
ClassNotFoundException {
FileInputStream fis = TheAAApp.getApp().openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}
Related
I am a little perplexed by the behavior I see in my proof-of-concept test program.
My Java application uses a file that is placed in "resource" folder in the Java project. The application will occasionally read numeric data from it, use it, increment the number and write it back to the same file for the next cycle.
The following test application mimics the above (wanted) behavior:
public class ReadWriteFile {
private static final String TEMP_EMAIL_ID_DATAFILE_PATH = "main/resources/TempEmailId.dat";
public static void main(String[] args) throws ParseException {
try {
int id = readTempId();
System.out.println("Current value = " + id);
writeTempId(id+5);
System.out.println("Updated value = " + readTempId());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int readTempId() throws IOException {
InputStream is = ReadWriteFile.class.getClassLoader().getResourceAsStream(TEMP_EMAIL_ID_DATAFILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
int currentValue = 0;
while ((line = br.readLine()) != null) {
currentValue = Integer.parseInt(line);
}
br.close();
return currentValue;
}
public static void writeTempId(int currentId) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("src" + File.separator + TEMP_EMAIL_ID_DATAFILE_PATH));
bw.write(Integer.toString(Math.abs(currentId)));
bw.flush();
bw.close();
return;
}
}
When I run the test, the following is seen:
Current value = 100000054
Updated value = 100000054
My gut feeling is that the use of
ReadWriteFile.class.getClassLoader().getResourceAsStream(TEMP_EMAIL_ID_DATAFILE_PATH);
is causing the issue. I am using this to access the file within the JAVA project.
Can it be true?
Also, note that for creating the BufferedWriter object, I have to pre-pend the Java constant with "src/" - else the file could not be found :(
Thanks.
Resources are intended to be read-only. The only way they could become writable is if they were extracted into the file system, but that's not how they are intended to be used and is not portable as resources are normally in a jar. Write to a file instead
This should work:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.ParseException;
public class ReadWriteFile {
private static final String TEMP_EMAIL_ID_DATAFILE_PATH = "TempEmailId.dat";
public static void main(String[] args) throws ParseException, URISyntaxException {
try {
int id = readTempId();
System.out.println("Current value = " + id);
writeTempId(id+5);
System.out.println("Updated value = " + readTempId());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int readTempId() throws IOException {
InputStream is = ReadWriteFile.class.getClassLoader().getResourceAsStream(TEMP_EMAIL_ID_DATAFILE_PATH);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
int currentValue = 0;
while ((line = br.readLine()) != null) {
currentValue = Integer.parseInt(line);
}
br.close();
return currentValue;
}
public static void writeTempId(int currentId) throws IOException, URISyntaxException {
URL resource = ReadWriteFile.class.getClassLoader().getResource(TEMP_EMAIL_ID_DATAFILE_PATH);
File file = new File(resource.toURI());
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(Integer.toString(Math.abs(currentId)));
bw.flush();
bw.close();
return;
}
}
The 2 key lines for writing to file was doing it as such:
URL resource = ReadWriteFile.class.getClassLoader().getResource(TEMP_EMAIL_ID_DATAFILE_PATH);
File file = new File(resource.toURI());
I need to save an arraylist in a txt when I close a window and load it when I return to open the program so that it shows what is saved in a JTable.
This is my arraylist
ArrayList<Usuarios> Encuestados = new ArrayList<>();
And I'm saving in this way but I would not know how to load the saved txt to the arraylist
public void guardarTxt() throws FileNotFoundException, IOException, ClassNotFoundException{
FileOutputStream fout=new FileOutputStream("Datos/Encuestados.txt");
try (ObjectOutputStream out = new ObjectOutputStream(fout)) {
out.writeObject(Encuestados);
}
}
You just need to serialize and deserialize the objects in the array. You can search for serialize and deserialize objects in java. I have implemented a code below.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Usuarios implements Serializable { // will need to implement Serializable class
private static final long serialversionUID = 129348938L; // this is needed
String name;
int age;
// Default constructor
public Usuarios(String name, int age) {
this.name = name;
}
}
public class Main { // Example class for Serialization and deserialization
// method for printing the object
public static void printdata(Usuarios object1) {
System.out.println("name = " + object1.name);
System.out.println("age = " + object1.age);
}
public static void serialize(ArrayList<Usuarios> list, String filename){
// Serialization
try {
// Saving of object in a file
FileOutputStream file = new FileOutputStream
(filename);
ObjectOutputStream out = new ObjectOutputStream
(file);
// Method for serialization of object
out.writeObject(list);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch (IOException ex) {
System.out.println("IOException is caught");
}
}
public static ArrayList<Usuarios> deserialize(String filename){
// Deserialization
try {
// Reading the object from a file
FileInputStream file = new FileInputStream
(filename);
ObjectInputStream in = new ObjectInputStream
(file);
// Method for deserialization of object
ArrayList<Usuarios> list = (ArrayList<Usuarios>)in.readObject();
System.out.println("Object has been deserialized");
in.close();
file.close();
return list;
}
catch (IOException ex) {
System.out.println("IOException is caught");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException" +
" is caught");
}
return null;
}
public static void main(String[] args) {
Usuarios object1 = new Usuarios ("ab", 20);
Usuarios object2 = new Usuarios ("cd", 21);
String filename = "s.txt";
ArrayList<Usuarios> EncuestadosBeforeSerialization = new ArrayList<>();
EncuestadosBeforeSerialization.add(object1);
EncuestadosBeforeSerialization.add(object2);
System.out.println("Data before Deserialization.");
for (Usuarios object: EncuestadosBeforeSerialization) {
printdata(object);
};
serialize(EncuestadosBeforeSerialization, filename);
System.out.println("\n\nData will be Deserialize.");
ArrayList<Usuarios> EncuestadosAfterSerialization = deserialize(filename);
System.out.println("Data after Deserialization.");
for (Usuarios object: EncuestadosAfterSerialization) {
printdata(object);
};
}
}
Result:
Data before Deserialization.
name = ab
age = 0
name = cd
age = 0
Object has been serialized
Data will be Deserialize.
Object has been deserialized
Data after Deserialization.
name = ab
age = 0
name = cd
age = 0
I want to read and write an object to a file. This is my attempt:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class SaveOpen implements Serializable
{
private static String fileName;
private ArrayList<Person> list = new ArrayList<Person>();
public SaveOpen() {
fileName = "file.txt";
}
//Reader
public static Object deserialize() throws IOException,
ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(fis);
ObjectInputStream ois = new ObjectInputStream(bis);
Object obj = ois.readObject();
ois.close();
return obj;
}
//Writer
public static void serialize(Object obj)
throws IOException {
FileOutputStream fos = new FileOutputStream(fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.close();
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public ArrayList<Person> getListPersons() {
return list;
}
}
However, I do not know if this is the correct way nor how to implement this in a class. The object is Person and I want to save and read that object from a file. Is it supposed to be done to a .txt file? Anyone who can clearify things? Thanks!
if you want the file to be human readable i would suggest to save it as xml.
Example :
Object Class
import java.io.Serializable;
public class Person implements Serializable
{
private String username;
private int id;
public String UserName() { return username; }
public void setUserName(String str) { username = str;}
public int ID() { return id; }
public void setID(int ID) { id = ID; }
}
-Serializer/Deserializer
import Settings.Person;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class XmlSerializer
{
//File path to serialize to and deserialize from
private static final String SERIALIZED_FILE_NAME = "yourSavePath.xml";
//Insert person object and save as xml file to chosen filepath
public static void Serialize(Person person)
{
try
{
FileOutputStream os = new FileOutputStream(SERIALIZED_FILE_NAME);
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(person);
encoder.close();
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
}
//Deserialize xml file into person object
public static Person Deserialize()
{
try
{
FileInputStream os = new FileInputStream(SERIALIZED_FILE_NAME);
XMLDecoder decoder = new XMLDecoder(os);
Person p = (Person)decoder.readObject();
decoder.close();
return p;
}
catch(FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
return null;
}
}
You're doing it right already. You can safe Objects in a txt file altough it makes not much sense, I'd rather go with a binary file.
To store multiple Objects in a single File, simply pack them in a Collection and then serialize the Collection object.
When reading an Object from a File, check its Class via instanceof and cast it to whatever it is.
I'm appending Object with that code.
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class ClaseAppendObjectStream extends ObjectOutputStream
{
public ClaseAppendObjectStream(OutputStream os) throws IOException
{
super(os);
}
protected void writeStreamHeader() throws IOException
{
reset();
}
}
And that writes it on my file correctly with writeObject methos, but when i use the "readObject()" with the "objectinputStream".
More information:
I've used "readObjectOverride"(using a subclass) and it has give me the same error.
It appeared that error:
"invalid stream header: 79757200."
I solucionated this error BUT it read the ".dat" file incorrectly.
My file .dat had 4 rows but i only read 1 row. My code for reading is that:
ObjectInputStream objetoInStr = new ObjectInputStream(new FileInputStream(sPath))
{
protected void readStreamHeader() throws IOException
{
}
};
ClassEmployer[] getData = (ClassEmployer[])objetoInStr.readObject();
objetoInStr.close();
String sPhrase="";
for(ClassEmployer e : getData )
{
sPhrase=sPhrase+"Name: " + e.getName() + " Salary: "+ e.getSalary();
}
objTPane.setText(sPhrase);
It only shows me the last row.
I write my rows like that:
ClassEmployer[] employers= new ClassEmployer[1];
employers[0]= new ClassEm,ployer(objctotext1.getText().trim(),objecttext2.getText().trim());
FileOutputStream objetoFileOutputStream = new FileOutputStream(sPath,true);
BufferedOutputStream objetooutputBuffer = new BufferedOutputStream(objetoFileOutputStream);
ClaseAppendObjectStream objetoOutStr = new ClaseAppendObjectStream(objetooutputBuffer);
objetoOutStr.writeObject(employers)
I find my owm misunderstanding. I' am reader other ask and answers of stack overflow.
First i had written my file correctly with AppendClass:
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
public class ClaseAppendObjectStream extends ObjectOutputStream
{
public ClaseAppendObjectStream(OutputStream os) throws IOException
{
super(os);
}
protected void writeStreamHeader() throws IOException
{
reset();
}
}
Read my file like that:
ObjectInputStream objetoInStr = new ObjectInputStream(new FileInputStream(sPath))
{
protected void readStreamHeader() throws IOException
{
}
};
And finally read my object like that:
ClassEmployer Employer;
String sText="";
try
{
//Infinit reading
while(true)
{
//that code wil have crashed with an EOFEXception
Employer = (ClasseEmployer)objetoInStr.readObject();
sText=sText+"Name: " + Employer.getName() + " Salary: "+ Employer.getSalary() +"\n";
}
}
catch(EOFException ex)
{
objetotextoGrande.setText(sText);
}
And all of that id the solution. I'll hope helps other programmers like me.
This seems to be simple but I failed to get a serialized JsonNode deserialized. Here is my test class
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Foo implements Serializable {
private String string;
private transient JsonNode jsonNode;
public Foo(String string, JsonNode jsonNode) {
this.string = string;
this.jsonNode = jsonNode;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if (this.jsonNode != null) out.writeObject((new ObjectMapper()).writeValueAsBytes(this.jsonNode));
// out.writeObject(this.jsonNode.textValue());
}
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
in.defaultReadObject();
this.jsonNode = (new ObjectMapper()).readValue(in, JsonNode.class);
}
}
When I tried to deserialize I got this error
com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
Here is the unit test
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.testng.annotations.Test;
import java.io.*;
import static org.testng.Assert.assertEquals;
public class FooTest {
#Test
public void testSerialization() {
JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
ObjectNode node = nodeFactory.objectNode();
ObjectNode child = nodeFactory.objectNode(); // the child
child.put("message", "test");
node.put("notification", child);
Foo foo = new Foo("Bar", node);
String fileName = "foo.ser";
try (
OutputStream file = new FileOutputStream(fileName);
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput output = new ObjectOutputStream(buffer);
){
output.writeObject(foo);
}
catch(IOException ex){
ex.getStackTrace();
}
Foo fooNew = null;
//deserialize the ser file
try(
InputStream file = new FileInputStream(fileName);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream (buffer);
){
//deserialize the Object
fooNew = (Foo) input.readObject();
}
catch(ClassNotFoundException ex){
ex.printStackTrace();
}
catch(IOException ex){
ex.printStackTrace();
}
assertEquals(foo, fooNew);
}
}
Your read and write operations are not matched.
On the write side you use ObjectOutputStream.writeObject(Object) to write a byte[] containing the serialized JSON content. On the read side you try to read raw bytes off the stream with ObjectMapper.readValue(InputStream, Class) when you actually need to read a byte[] object first as that is what you wrote and then use ObjectMapper.readValue(byte[], Class).
Alternatively and probably a better solution is you could use ObjectMapper.writeValue(OutputStream, Object) instead on the write side.
Try this:
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
if(jsonNode == null){
out.writeBoolean(false);
} else {
out.writeBoolean(true);
new ObjectMapper().configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false).writeValue(out, jsonNode);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
if(in.readBoolean()){
this.jsonNode = new ObjectMapper().configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false).readValue(in, JsonNode.class);
}
}