How to assign new object to existing one - java

I've got a problem with reading an object from file..
in main, I made eshop end call a method loadshop.
MyShop eshop = new MyShop();
eshop.loadShop("eshop.txt");
and also I'm getting the shop object from file.
#Override
public void loadShop(String filename) {
try{
ObjectInput out = new ObjectInputStream(new FileInputStream(filename));
MyShop shop = (MyShop)out.readObject();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
But I just can't figure out how to assign "shop" to "eshop"
Thank you for any answer..:-) (Sorry for my English)

Just redesign, you don't need to write
MyShop eshop = new MyShop();
Instead, write
MyShop eshop = MyShop.loadShop("eshop.txt");
and make the loadShop method static. And make it return the MyShop instance, if it isn't already doing that.
If this is not an option, it is still better to get that instance through a hack than to write manual copying code:
public class MyShop {
private MyShop readShop;
public MyShop getReadShop() { return readShop; }
public void loadShop(String fName) {
try {
ObjectInput out = new ObjectInputStream(new FileInputStream(filename));
this.readShop = (MyShop)out.readObject();
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
You'd use this like
MyShop tempShop = new MyShop();
tempShop.loadShop();
MyShop eshop = tempShop.getReadShop();

Make your loadShop method a static method. Make the method return the MyShop object. Call it on the MyShop class. Assign the return value to eshop. Example:
public class MyShop {
public static MyShop loadShop(String filename) {
// ...
MyShop shop = (MyShop)out.readObject();
// ...
return shop;
}
}
Where you call the method:
MyShop eshop = MyShop.loadShop("eshop.txt");

Simply edit all eshop's fields, to match shop.

Related

How to write data in file in specific format with FileOutputStream and PrintWriter

I have class Artical:
first variable is code of artical, second variable is name of article and third is price of article.
public class Artical {
private final String codeOfArtical;
private final String nameOfArtical;
private double priceOfArtical;
public Artical(String codeOfArtical, String nameOfArtical, double priceOfArtical) {
this.codeOfArtical= codeOfArtical;
this.nameOfArtical= nameOfArtical;
this.priceOfArtical= priceOfArtical;
}
public void setPriceOfArtical(double priceOfArtical) {
this.priceOfArtical= priceOfArtical;
}
public String getCodeOfArtical() {
return codeOfArtical;
}
public String getNameOfArtical() {
return nameOfArtical;
}
public double getPriceOfArtical() {
return priceOfArtical;
}
}
I want in main class to write something like:
Artical a1 = new Artical("841740102156", "LG Monitor", 600.00);
new ShowArticalClass(a1).do();
new WriteArticalInFileClass(new File("baza.csv"), a1).do();
so that data in file will be written in format like this:
841740102156; Monitor LG; 600.00;
914918414989; Intel CPU; 250.00;
Those 2 classes ShowArticalClass and WriteArticalInFileClass arent important, those are abstract classes.*
So my question is: How do I set format to look like this, where every line is new Artical.
A very naive implementation can be the following:
Create a class that in turn creates a CSVWriter (assuming you want to write to a CSV). That class will expose a public method allowing you to pass in a path where the desired csv file lives as well as the Artical object you want to write to this file. Using that class you will format your data and write them to the file. An example of this could be:
public class CsvWriter {
private static final Object LOCK = new Object();
private static CsvWriter writer;
private CsvWriter() {}
public static CsvWriter getInstance() {
synchronized (LOCK) {
if (null == writer) {
writer = new CsvWriter();
}
return writer;
}
}
public void writeCsv(String filePath, Artical content) throws IOException {
try (var writer = createWriter(filePath)) {
writer.append(getDataline(content)).append("\n");
}
}
private String getDataline(Artical content) {
return String.join(",", content.getCode(), content.getName(), Double.toString(content.getPrice()));
}
private PrintWriter createWriter(String stringPath) throws IOException {
var path = Paths.get(stringPath);
try {
if (Files.exists(path)) {
System.out.printf("File under path %s exists. Will append to it%n", stringPath);
return new PrintWriter(new FileWriter(path.toFile(), true));
}
return new PrintWriter(path.toFile());
} catch (Exception e) {
System.out.println("An error has occurred while writing to a file");
throw e;
}
}
}
Note that this will take into account where the file provided is already in place (thus appending to it). In any other case the file will be created and written to directly.
Call this write method in a fashion similar to this:
public static void main(String... args) throws IOException {
var artical = new Artical("1", "Test", 10.10);
CsvWriter.getInstance().writeCsv("/tmp/test1.csv", artical);
var artical2 = new Artical("2", "Test", 11.14);
CsvWriter.getInstance().writeCsv("/tmp/test1.csv", artical2);
}
With that as a starting point you can go ahead and modify the code to be able to handle list of Artical objects.
If you really need to support CSV files though I would strongly recommend into looking at the various CSV related libraries that are out there instead of implementing your own code.

Serialization of an object "that has fields reverenced by a SWT class"

I'm kind of stuck with a problem. I do understand the concept of serialization. Nevertheless I'm getting errors when I try to serialize/deserialize (deepCopy) an object:
I have a basic domain objects that hold information (two files):
public class DomainObject implements java.io.Serializable {
private String defaultDescription = "";
private List<Translation> translations;
public DomainObject() {
;
}
public void setTranslations(final List<Translation> newTranslations) {
this.translations = newTranslations;
}
public List<Translation> getTranslations() {
return this.translations;
}
public void setDefaultDescription(final String newDescription) {
this.defaultDescription = newDescription;
}
public String getDefaultDescription() {
return this.defaultDescription;
}
}
public class Translations implements java.io.Serializable {
private String description = "";
public Translation() {
;
}
public void setDescription(final String newDescription) {
this.description = newDescription;
}
public String getDescription() {
return this.description;
}
}
I also have a frame so the user can fill in all the necessary information for this domain object. Since I have multiple domain objects (this example only shows one) with different fields I have different frames for each domain object. Each of these frames includes a "MultiLanguageFrame" which gives the user the ability to add optional translations for this domain object's description.
public class MultiLanguageFrame extends org.eclipse.swt.widgets.Composite {
private List<Translation> translations = new ArrayList<Translation>();
public MultiLanguageFrame(final Composite parent, final int style) {
super(parent, style);
...
}
public List<Translation> getTranslations() {
return translations;
}
}
I deepCopy objects via this method:
...
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception t) {
logger.error(deepCopy() error: " + t.getMessage()); //$NON-NLS-1$
throw new RuntimeException("deepCopy() error", t); //$NON-NLS-1$
}
So now to the error:
When i try to do something like this:
MultiLanguageFrame frame = new MultiLanguageFrame(parent, SWT.NONE);
DomainObject dom = new DomainObject();
dom.setDefaultDescription("Testobject");
dom.setTranslations(frame.getTranslations())
deepCopy(dom);
I receive an error telling me that MultiLanguageFrame is not Serializable. Why would Java try to serialize the frame when I only want that DomainObject?
I thought maybe it is because of the reference in frame. So when I add the Serializable-Interface to MultiLanguageFrame and markt the SWT-Components as transient it tells me that no valid constructor was found. I can't add a parameterless constructor because it would logically make no sense and also SWT-Components need a parent to exist.
I'm really stuck with this problem because I do not know how to work around this. Thanks for answers in advance!
I found the solution myself. I'll just post this so others can see it, it might help.
Thanks to #greg-449 who lead the way. I do have an inner class TranslationHelper which extends Translation in MultiLanguageFrame. The purpose of this is so I can save some flags (deleted, changed, new) for Translations without changing Translation itself. When I call frame.getTranslations() I cast the elements from TranslationsHelper to Translation. The instance of the object remains a TranslationHelper though.
Now it all makes sense that MultiLanguageFrame was involved in all of this.

How do I call a Local Variable from Outside of its Method in Java

public void main(String[] args) throws Exception {
URL url = new URL("https://api.coinmarketcap.com/v1/global/");
InputStreamReader reader = new InputStreamReader(url.openStream());
MyData data = new Gson().fromJson(reader, MyData.class);
}
public static class MyData {
String total_market_cap_usd;
}
So what I need is to call this local variable "data" in a different class. So what I need is to somehow create a global variable with the same value as this local one, the problem is that the class this local variable is made from doesn't allow anything but final before it so could someone please help me out here. I'm really stumped and I haven't been able to find any resources to help me.
I'm sorry if I sound like a complete noob here because my experience in lower level coding languages is limited.
Based on your requirement you mentioned in comment, I tried achieving. See if it helps.
public class ChatBot {
String response;
public static void main(String[] args) throws Exception {
ChatBot c1 = new ChatBot();
String qry1 = c1.getResponse("total_market_cap_usd");
System.out.println("response: "+qry1);
}
public static class MyData {
String total_market_cap_usd;
String total_24h_volume_usd;
}
public String getResponse(String query) {
URL url = null;
try {
url = new URL("https://api.coinmarketcap.com/v1/global/");
InputStreamReader reader = new InputStreamReader(url.openStream());
MyData data = new Gson().fromJson(reader, MyData.class);
switch (query) {
case "total_market_cap_usd":
response = data.total_24h_volume_usd;
break;
case "total_24h_volume_usd":
response = data.total_24h_volume_usd;
break;
default:
throw new IllegalArgumentException("Query not recognized!");
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
Here is your answer :
https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
Since you wish to use your variable in a different class,you can use extends keyword as:
public class SubClass extends SuperClass{
//you can override the implementation
//#Override
public void getVariable(){
<datatype> var = this.localVariableOfPreviousClass;
}
}

JAVA Property load static variable inputstream return null value

public class PropertyDemo {
private static InputStream in = PropertyDemo.class.getClassLoader().getResourceAsStream("address.properties");
public void test() {
try {
// InputStream in = PropertyDemo.class.getClassLoader().getResourceAsStream("address.properties");
Properties pro = new Properties();
pro.load(in);
String address = pro.getProperty("returnInfoRegister");
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new PropertyDemo().test();
new PropertyDemo().test();
}}
In above code the first run will return correct value but the second run return null value I don't know why ,but when I change "in" variable to non static (I mean a local variable) things goes right but why?
When you move through the stream reading it, you are at the end of it. Using this as a static, saves that state (as it is the same variable in both class instances you have declared in main). So the next time you use it, it is already at the end of the stream. When you declare it as non-static, it is a new instance for each class instance and you are good.
But there really is no reason to declare this as a class level variable. Your inner variable is much better. You should also consider closing the stream at the end.
public class PropertyDemo {
//private static InputStream in = PropertyDemo.class.getClassLoader().getResourceAsStream("address.properties");
public void test() {
InputStream in = null;
try {
in = PropertyDemo.class.getClassLoader().getResourceAsStream("address.properties");
Properties pro = new Properties();
pro.load(in);
String address = pro.getProperty("returnInfoRegister");
System.out.println(address);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {in.close();} catch (Exception e) {}
}
}
}
public static void main(String[] args) {
new PropertyDemo().test();
new PropertyDemo().test();
}
}

Java Object Serialization nested objects

I was studying Serialization in Java when I came across saving the state of objects which are not serializable and are referenced in Class(instance variables) to be serialized. In the following code, I am having class Dog (Serializable) which has reference to class Collar(not serializable); which in turn has reference to class Color (not serializable). I am getting error despite trying all the possibilities. This is the latest code I came up with:
class Color {
private String colorName;
public String getColorName() {
return colorName;
}
public void setColorName(String colorName) {
this.colorName = colorName;
}
Color(String color) {
this.colorName = color;
}
}
class Collar {
private Color color;
private int size;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
Collar(int size, Color color) {
this.size = size;
this.color = color;
}
}
class Dog implements Serializable {
Dog(String breed, Collar collar) {
this.breed = breed;
this.collar = collar;
}
private String breed;
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public Collar getCollar() {
return collar;
}
public void setCollar(Collar collar) {
this.collar = collar;
}
transient private Collar collar;
private void writeObject(ObjectOutputStream os) {
try {
os.defaultWriteObject();
os.writeInt(this.getCollar().getSize());
os.writeUTF(this.getCollar().getColor().getColorName());
os.close();
} catch (IOException ex) {
Logger.getLogger(Dog.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void readObject(ObjectInputStream is) {
try {
is.defaultReadObject();
int size = is.readInt();
String colorName = is.readUTF();
this.setCollar(new Collar(size, new Color(colorName)));
is.close();
} catch (Exception ex) {
Logger.getLogger(Dog.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public class App0001 {
public static void main(String[] args) {
try {
Dog d = new Dog("Great Dane", new Collar(3, new Color("RED")));
//System.out.println(d.getCollar().getColor().getColorName());
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("obj.ser"));
os.writeObject(d);
os.close();
ObjectInputStream is = new ObjectInputStream(new FileInputStream("obj.ser"));
d = (Dog) is.readObject();
System.out.println(d.getCollar().getColor().getColorName());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
And I am getting following error:
java.io.IOException: Write error
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:260)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1847)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1756)
at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1257)
at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1211)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1395)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:333)
at Serialization.App0001.main(App0001.java:121)
This is not a production code. This is just for practice and understanding.
You must not close the streams in readObject and writeObject! If you do so, the next write/read attempt fails.
Usually, streams (as other resources) should be treated as follows:
If your method owns the stream, i.e. your method opened it - close it
in the same method (usually this is done in a try-with-resource
statement).
If your method does NOT own the stream, i.e. it got the stream passed from somewhere else (usually passed via method parameter), don't close it as you don't know what the owner of the stream wants to do with it after your method returns.
When writing to a stream, the "Write error" in an IOException occurs, if the stream is closed.
Analyzing your code, I see that you have a custom writeObject method in your class Dog. In that you must not close the stream, as it is needed for continued writing. So just remove the line
os.close();
in your writeObject method. Oh, and also remove the line
is.close();
in the readObject method.
Ok, I will explain it a bit more. You have the following code in your main method:
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("obj.ser"));
os.writeObject(d);
os.close();
Here you are creating the stream, using it, and afterwards you close it. This is the correct place to close it, as this is the responsible place for the stream.
Imagine, you have a nested structure of serializable objects, whose class definitions all contain a custom writeObject method. When calling the writeObject method of an ObjectOutputStream, it walks through the object graph with calling the writeObject method of each object. The ObjectOutputStream is controlling the write order, and it also write control bytes itself. Creating and closing must be done outside (as you already did).

Categories