Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: ObjectInputStream

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default ObjectInputStream

    Hello,

    i have saved an arrayList to file using the ObjectOutputStream but now when i try to read the file i get problems.

    saveOnFile:
    public void saveOnFile() {
    		try {
    			ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("object.txt"));
    			output.writeObject(mediaList);
    			output.close();
    		} catch(Exception ex) {
    			ex.printStackTrace();
    		}
    	}

    readFromFile:
    public void readFromFile() {
    		try {
    			ObjectInputStream inStream = new ObjectInputStream(new FileInputStream("object.txt"));
    			this.mediaList.add( (Media) inStream.readObject());
    			inStream.close();
    		} catch(Exception ex) {
    			ex.printStackTrace();
    		}
    	}

    Does it know which instance the object is or do i need to handle this?

    ERRORS:
    java.lang.ClassCastException: java.util.ArrayList cannot be cast to project.Media
    	at project.MediaHandler.readFromFile(MediaHandler.java:66)
    	at project.Window.<init>(Window.java:73)
    	at project.Window.main(Window.java:199)

    ED, my classes are serializable.
    Last edited by sebbe605; April 9th, 2014 at 02:59 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ObjectInputStream

    ArrayList cannot be cast to project.Media
    The code reads an ArrayList object and tries to cast it to a Media object?
    Did you try to cast it to an ArrayList?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ObjectInputStream

    I tried with this earlier:
    	public void readFromFile() {
    		try {
    			ObjectInputStream inStream = new ObjectInputStream(new FileInputStream("object.txt"));
    			this.mediaList.addAll( (ArrayList<Media>) inStream.readObject());
    			inStream.close();
    		} catch(Exception ex) {
    			ex.printStackTrace();
    		}
    	}

    It doesn't do anything during execution and gives me warning
    Type safety: Unchecked cast from Object to ArrayList<Media>
    Last edited by sebbe605; April 9th, 2014 at 03:34 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ObjectInputStream

    What warning? Post the full text.

    Try testing by reading into an ArrayList variable and printing out the contents.

    The code works for me.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ObjectInputStream

    This warning: Type safety: Unchecked cast from Object to ArrayList<Media>

  6. #6
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ObjectInputStream

    Okay, i changed some stuff around and i think that this should work, but it doesn't work.

    	public void readFromFile() {
    		  try {  
    			  ObjectInputStream inStream = new ObjectInputStream(new FileInputStream("object.txt"));
    		   try {
    		    ArrayList<Media> mediaList = (ArrayList<Media>) inStream.readObject(); 
    		    } catch (EOFException exc) {
     
    		    }
    		    inStream.close();
    		  } catch(Exception ex) {
    		   ex.printStackTrace();
    		  }
    		}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ObjectInputStream

    it doesn't work.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ObjectInputStream

    Couldn't get it to work so i went with another method.

    	public void saveOnFile() {
    		File file = new File("data.txt");
     
    		try {
    			if(!file.createNewFile()) {
    				file.createNewFile();
    			}
    			FileWriter out = new FileWriter(file.getAbsoluteFile());
    			BufferedWriter bW=new BufferedWriter(out);
     
    			bW.write(this.mediaList.size() + "\r\n");
    			for(int i = 0; i < mediaList.size(); i++) {
    				 bW.write(this.mediaList.get(i).toString());
    			}
    			bW.close();
    		} catch(IOException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public void readFromFile() {
    		FileReader fileReider;
    		try {
    			fileReider = new FileReader("data.txt");
    			BufferedReader bufferedReader = new BufferedReader(fileReider);
    			int tempSize = Integer.parseInt(bufferedReader.readLine());
     
    			for(int i = 0; i < tempSize; i++) {
    				// Get items from file
    				String title = bufferedReader.readLine();
    				Double playTime = Double.parseDouble(bufferedReader.readLine());
    				int year = Integer.parseInt(bufferedReader.readLine());
    				bufferedReader.readLine();
    				String directory = bufferedReader.readLine();
    				String mediaPath = bufferedReader.readLine();
    				String imagePath = bufferedReader.readLine();
    				bufferedReader.readLine();
    				int objectTypeValue = Integer.parseInt(bufferedReader.readLine());
     
    				if(objectTypeValue == 0) { // If Movie add movie else add a TV-show
    					String quality = bufferedReader.readLine();
    					boolean subtitles = Boolean.parseBoolean(bufferedReader.readLine());
    					String language = bufferedReader.readLine();
    					String writer = bufferedReader.readLine();
     
    					addMovie(title, playTime, year, directory, mediaPath, imagePath, quality, subtitles, language, writer);
    				} else {
    					int season = Integer.parseInt(bufferedReader.readLine());
    					int episode = Integer.parseInt(bufferedReader.readLine());
     
    					addTVShow(title, playTime, year, directory, mediaPath, imagePath, season, episode);
    				}
    			}
    			bufferedReader.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}

    This is the way i learned to do it in c++ thought it would be nice to know how ObjectInput-/output-Stream works as well.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ObjectInputStream

    I got it to work as I stated in post#4. I don't know what you did that kept it from working.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Mar 2014
    Location
    sweden
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ObjectInputStream

    Neither do i. I was looking at another thread there someone had similar problems. They ended up changing the ArrayList to List
      List<Media> mediaList = (List<Media>) inStream.readObject();
    but he only had one type of element and i have two. But the way i did it know is easier to understand because i can see every step in the function because i made it myself. I find that thats a problem with "premade" methods "it's hard to get an understanding of what's actually happening".