Speichern von Objekten

Diskutiere Speichern von Objekten im Allgemeine Java-Themen Bereich.
T

TM69

Ich arbeite gerad an meinem Projekt. Ich habe eine Hilfsklasse FileHelpers:
Code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;

/**
 * this class contains helper function for files
 */
public class FileHelper {
    
    /**
     * save object to File
     *
     * @param path                the filename
     * @param Object            the object to store
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void writeObjectToFile(File path, Object object) throws FileNotFoundException, IOException {
        ObjectOutputStream write= new ObjectOutputStream (new FileOutputStream(path));
        write.writeObject(object);
    }

    /**
     * read object from File
     *
     * @param path                the filename
     * @return Object            the object to read
     * @throws IOException
     * @throws FileNotFoundException
     * @throws ClassNotFoundException
     */
    public static Object readFromFile(File path) throws FileNotFoundException, IOException, ClassNotFoundException {
        Object data = null;

        ObjectInputStream inFile = new ObjectInputStream(new FileInputStream(path));
        data = inFile.readObject();
        
        return data;
    }       
    
    /**
     * get all files with <code>wildcard</code> from directory <code>directoryName</code>
     *
     * @param String directoryName                    the name of the directory
     * @param String wildcard                        the filter
     * @return Collection<File>                        the found files
     */
    public static Collection<File> getFiles(String directoryName, String wildcard) {
          File directory = new File(directoryName);
          return FileUtils.listFiles(directory, new WildcardFileFilter(wildcard), null);       
    }
}
und eine dazugehörige Test
Code:
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.junit.jupiter.api.Test;

// Test Data Class
class TestDataClass {
    private int x;
    private int y;
    
    public TestDataClass(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
    
    
}

// Test Class
class TestClass {
    private int x;
    private int y;
    private String name;
    private List<String> nameList = new ArrayList<String>();
    private List<TestDataClass> dataList = new ArrayList<TestDataClass>();
    
    public TestClass(int x, int y, String name, List<String> nameList, List<TestDataClass> dataList) {
        super();
        this.x = x;
        this.y = y;
        this.name = name;
        this.nameList = nameList;
        this.dataList = dataList;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getNameList() {
        return nameList;
    }

    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }

    public List<TestDataClass> getDataList() {
        return dataList;
    }

    public void setDataList(List<TestDataClass> dataList) {
        this.dataList = dataList;
    }
}

/**
 * test the file helper class
 */
class FileHelperTest {

    /**
     * test writeObjectToFile and readObjectFromFile
     *
     * @throws IOException
     */
    @Test
    void testWriteObjectToFileAndReadObjectFromFile() throws IOException {
        int x = 20;
        int y = 50;
        String name = "name";
        String filename = "D:\\test.tst";
        File path = new File(filename);
        boolean exception = false;
        
        List<String> names = new ArrayList<String>();       
        names.add("names1");
        names.add("names2");
        names.add("names3");
        
        List<TestDataClass> datas = new ArrayList<TestDataClass>();
        datas.add(new TestDataClass(1, 2));
        datas.add(new TestDataClass(2, 3));
        datas.add(new TestDataClass(3, 4));
                
        TestClass testClass = new TestClass(x, y, name, names, datas);
        TestClass readClass = null;
        
        try {
            FileHelper.writeObjectToFile(path, testClass);
            // readClass = (TestClass) FileHelper.readFromFile(path);
        } catch (IOException e) {
            exception = true;
            e.printStackTrace();
        }
        
        assertFalse(exception);
        /*
        assertEquals(x, readClass.getX());
        assertEquals(y, readClass.getY());
        assertEquals(name, readClass.getName());
        assertEquals(names.size(), readClass.getNameList().size());
        assertEquals(datas.size(), readClass.getDataList().size());
        */
        // clean up
        FileUtils.forceDelete(new File( filename ));
    }

}
Mir wird aber ein Runtime-Error ausgegeben und ich sehe ihn momentan einfach nicht
java.io.NotSerializableException: com.metho.bpmndesigner.helper.TestClass
at java.io_ObjectOutputStream.writeObject0(Unknown Source)
at java.io_ObjectOutputStream.writeObject(Unknown Source)
at com.metho.bpmndesigner.helper.FileHelper.writeObjectToFile(FileHelper.java:36)
at com.metho.bpmndesigner.helper.FileHelperTest.testWriteObjectToFileAndReadObjectFromFile(FileHelperTest.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:125)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:104)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:62)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.util.ArrayList.forEach(Unknown Source)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Weis jemand was ich hier gerade falsch mache? Danke!!!
 
Thema: 

Speichern von Objekten

Passende Stellenanzeigen aus deiner Region:
Anzeige

Neue Themen

Anzeige

Anzeige
Oben