I'm having problems with appending a generic object within an existing file. This method is supposed to append the object to the existing file if the parameters are "true" and overwrites the entire file if "false". The "false" statement works perfectly fine, it overwrites the entire file but I can't seem to get the append one to work. It seems to do nothing at first glance but when I placed a simple System.out.println("test"); in the while (true) loop, it runs forever. How can I fix this?
public <T> void writeOneObject(T type, boolean append) throws NotSerializableException{
if (append == true){
//TODO
if (file.exists ()){
ObjectOutputStream ois = null;
try{
ois = new ObjectOutputStream (new FileOutputStream (file, true));
while (true){
ois.writeObject(type);
}
}catch (StreamCorruptedException e){
}catch (EOFException e){
}catch (Exception e){
e.printStackTrace ();
}finally{
try{
if (ois != null) ois.close();
}catch (StreamCorruptedException e){
}catch (IOException e){
e.printStackTrace ();
}
}
}
}
else { //overwrites the entire file
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(type);
oos.flush();
oos.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (StreamCorruptedException e) {
System.out.println("error");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I also have this inside the class:
class NoHeaderObjectOutputStream extends ObjectOutputStream {
public NoHeaderObjectOutputStream(OutputStream os) throws IOException {
super(os);
}
protected void writeStreamHeader() {}
}
Related
I have encountered a weird behaviour of the ObjectOutputStream / ObjectInputStream.
I want to write a specific amount of Objects of the type Sitzplatz to the file belegung. So far so good. But if I try to read these Objects again they all just return null
Here is my Code:
public void getBelegtePlaetze() {
if (belegung.exists()) {
try {
FileInputStream fis = new FileInputStream(belegung);
ObjectInputStream ois = new ObjectInputStream(fis);
Sitzplatz platz = (Sitzplatz) ois.readObject();
while (platz != null) {
System.out.println(platz.getId());
platz = (Sitzplatz) ois.readObject();
}
ois.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void speichereSitzplatzDaten() {
FileOutputStream fos;
if (!belegung.exists()) {
try {
belegung.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
fos = new FileOutputStream(belegung, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < kundenListe.size(); i++) {
Sitzplatz platz = kundenListe.get(i).getPlatz();
platz.setId(Integer.toString(i));
oos.writeObject(platz);
}
oos.writeObject(null); // Markiert EOF
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Since I have already worked with me ArrayList kundenListe quite a bit in my program, I can be 100% sure that the data in it is properly set.
I have also already used the ObjectOuputStreamin this program before and nearly copied the code, but still it does not work.
Can you tell me what I'm doing wrong?
UPDATE:
I've tried the suggestions from the comments, but I still have the same issues.
I have reduced the code to the little piece below, which should work 100%, since I have that code already in use in another class. No idea why it works in the other class and not in that one.
private File belegung = new File("belegung.kos");
public void getBelegtePlaetze() {
if (belegung.exists()) {
try {
FileInputStream fis = new FileInputStream(belegung);
ObjectInputStream ois = new ObjectInputStream(fis);
Pakett platz = (Pakett) ois.readObject();
System.out.println(platz.getId());
ois.close();
fis.close();
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void speichereSitzplatzDaten() {
FileOutputStream fos;
if (!belegung.exists()) {
try {
belegung.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
fos = new FileOutputStream(belegung);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Pakett platz = new Pakett();
platz.setId("test");
oos.writeObject(platz);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
I have this code:
private void save(Bitmap bitmap) {
try {
FileOutputStream fos = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Do I need to close the FileOutputStream in FileNotFoundException catch block?
If that exception is thrown means that file could not be opened so I think that it would not be necessary. However, I think it would be nice to do it in IOException catch block.
Could it cause any memory leak error or something similar if I don't do it?
Thanks.
If you are working in Java 7 or above you should use a try with resources and let the system decide.
private void save(Bitmap bitmap) {
try (FileOutputStream fos = new FileOutputStream(path)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
If not then just ensure that the stream is not null first and do it in a finally.
private void save(Bitmap bitmap) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
}
}
There is nothing to close. The FileOutputStream constructor threw an exception; the stream was never constructed; the fos variable has never been assigned; and it is out of scope in the catch block.
You should always close the file once you have finished to read or write. Otherwise you will keep the resource busy and in general could be a problem.
If you modify your code in this way you will not have to care about close the file and java will think about it.
private void save(Bitmap bitmap) {
try(FileOutputStream fos = new FileOutputStream(path)) {
bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESSION_QUALITY, fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
in one of the activities when a button is pressed i want to create a file on the extran storage. so i wrote
the below code to do so.
but in the end the file is created but empty..why?
code:
public void tx(byte[] data) {
Log.w(TAG, CSubTag.bullet("tx"));
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
OutputStream os = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(os);
try {
bos.write("data_stream".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Please close the OutputStream and BufferedOutputStream inside finally block. Otherwise, the data will not be written.
Why can't I create an ObjectInputStream object? Every time I try to create one I get EOFException and I can't figure why. Can someone help me?
Below is the code with which I have the problem and the stack trace obtained from the execution. The file is empty.
public void loadFromFileStudent() throws IOException, ClassNotFoundException {
try{
InputStream inputStream = new FileInputStream("student.txt");
System.out.println(inputStream.toString());
ObjectInputStream objectInputStream;
objectInputStream = new ObjectInputStream(inputStream);
System.out.println(objectInputStream.toString());
this.repo=(Dictionary<Integer, Student>) objectInputStream.readObject();
objectInputStream.close();
inputStream.close();
}catch (EOFException e){
e.printStackTrace();;
//System.out.println(e.getMessage());
}
}
.
java.io.FileInputStream#65ddcac5
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2324)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2793)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:799)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at repository.Repository.loadFromFileStudent(Repository.java:94)
at repository.Repository.<init>(Repository.java:112)
at utils.DataStructure.createRepository(DataStructure.java:16)
at controller.Controller.<init>(Controller.java:9)
at utils.DataStructure.createController(DataStructure.java:20)
at application.RunMenu.<init>(RunMenu.java:15)
at application.App.main(App.java:5)
EOFException is thrown when end-of-file is reached. That is, you have read the whole file. Therefore you should not close your streams within the try statement, but use try-with-resources to automatically close them.
Try something simple like this:
public void loadFromFileStudent() throws IOException, ClassNotFoundException {
try (InputStream inputStream = new FileInputStream("student.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(inputStream)) {
this.repo = (Dictionary<Integer, Student>) objectInputStream.readObject();
} catch (FileNotFoundException e) {
System.out.println ("File not found");
} catch (IOException e) {
System.out.println ("Error while reading");
} catch (ClassNotFoundException e) {
System.out.println ("No class");
} catch (ClassCastException e) {
System.out.println ("Could not cast to class");
}
}
Writing is equally simple:
public void writeObject ( Object o ) {
try (FileOutputStream fos = new FileOutputStream ( this.filename );
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(o);
oos.flush();
} catch (NotSerializableException e) {
System.out.println ("Object wont be serialized");
e.printStackTrace();
} catch (IOException e) {
System.out.println ("Error while writing to file");
e.printStackTrace();
}
}
From my understanding of the question I assume OP is doing some thing like below, and which should works. May be OP would have missed something during writing/reading. Hope this helps to figure out.
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2();
t.create();
t.read();
}
public void create(){
try{
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("D:\\test\\ab.txt"));
Student st = new Student("chevs");
Dictionary<Integer, Student> dict = new Hashtable<Integer, Student>();
dict.put(1, st);
os.writeObject(dict);
}catch(Exception e){
e.printStackTrace();
}
}
public void read()
{
try{
InputStream inputStream = new FileInputStream("D:\\test\\a.txt");
System.out.println(inputStream.toString());
ObjectInputStream objectInputStream;
objectInputStream = new ObjectInputStream(inputStream);
System.out.println(objectInputStream.toString());
private Dictionary<Integer, Student> repo=(Dictionary<Integer, Student>) objectInputStream.readObject();
System.out.println(repo.get(1));
objectInputStream.close();
inputStream.close();
}catch (Exception e){
e.printStackTrace();;
}
}
public class Student implements Serializable{
public String name=null;
public Student(String name){
this.name=name;
}
public String toString() {
return name.toString();
}
}
}
I have following code :
// Read properties file.
Properties properties = new Properties();
try {
properties.load(new FileInputStream("filename.properties"));
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
}catch (IOException e) {
system.out.println("IOEXCeption");
}
Is it required to close the FileInputStream? If yes, how do I do that? I am getting a bad practice error in my code checklist . Asking it to put finally block.
You must the close the FileInputStream, as the Properties instance will not. From the Properties.load() javadoc:
The specified stream remains open after this method returns.
Store the FileInputStream in a separate variable, declared outside of the try and add a finally block that closes the FileInputStream if it was opened:
Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream("filename.properties");
properties.load(fis);
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
} catch (IOException e) {
system.out.println("IOEXCeption");
} finally {
if (null != fis)
{
try
{
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Use try-with-resources since Java 7:
final Properties properties = new Properties();
try (final FileInputStream fis =
new FileInputStream("filename.properties"))
{
properties.load(fis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound: " + e.getMessage());
} catch (IOException e) {
System.out.println("IOEXCeption: " + e.getMessage());
}
You should always close your streams, and doing it in the finally block is a good practice. The reason for this is that the finally block always gets executed, and you want to make sure that the stream is always closed, even if Something Bad happens.
FileInputStream inStream = null;
try {
inStream = new FileInputStream("filename.properties");
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
} finally {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
If you are using Java 7, this becomes much easier, since a new try-with syntax was introduced. Then you can write like this:
try(FileInputStream inStream = new FileInputStream("filename.properties")){
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
}
and the stream is closed automatically.
here is an example:
public class PropertiesHelper {
public static Properties loadFromFile(String file) throws IOException {
Properties properties = new Properties();
FileInputStream stream = new FileInputStream(file);
try {
properties.load(stream);
} finally {
stream.close();
}
return properties;
}
}
You can use Lombok #Cleanup to do it simply.
http://projectlombok.org/features/Cleanup.html
Properties properties = new Properties();
try {
#Cleanup FileInputStream myFis = new FileInputStream("filename.properties");
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}
Or, only if your are using Java 7, there is the "try with resource" new feature.
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Properties properties = new Properties();
try (FileInputStream myFis = new FileInputStream("filename.properties")) {
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}