Results 1 to 5 of 5
  1. #1
    Aubanis is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default StreamCorruptedException what?

    It's throwing this error at the code with orange text

    employees is an array list of Employee objects
    Java Code:
    public void displayFileContents(){
    		File[] files = directory.listFiles();
    		File chosenDirectory = new File(files[chooseFile(directory.list(), files) - 1].getPath());
    		try {
    			FileInputStream fileIn = new FileInputStream(chosenDirectory);
    			ObjectInputStream objectIn = new ObjectInputStream(fileIn);
    			while(fileIn.available() > 0){
    				try {
    					[COLOR="DarkOrange"]employees.add((Employee) objectIn.readObject());[/COLOR]
    				} catch (Exception e) {
    					e.printStackTrace();
    					System.exit(0);
    					System.out.println("Error while reading object");
    				}
    			}
    			objectIn.close();
    			for(int i = 0; i < employees.size(); i++){
    				System.out.println(employees.get(i).toString());
    			}
    		} catch (IOException e) {
    			System.out.println("Error in displayFileContent");
    			e.printStackTrace();
    		}
    	}
    The "Choose file" method
    Java Code:
    public int chooseFile(String[] fileNames, File[] files){
    		int i = 0, choice = 1, counter =  fileNames.length;
    		while (counter > 0){
    			System.out.println("[" + choice + "]" + fileNames[i]);
    			choice++;
    			i++;
    			counter--;
    		}
    		return getInput(1, fileNames.length);
    	}
    Last edited by Aubanis; 04-16-2011 at 06:12 AM.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Are you sure the object you're reading in has been properly written to the file?
    A common mistake newbies make is use multiple output streams, such as writing objects and plain strings at the same time. Double check how you wrote the objects.

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    431
    Rep Power
    6

    Default

    make sure that the file you use to store object is valid or not.
    Mak
    (Living @ Virtual World)

  4. #4
    Aubanis is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Here's how I write objects to the file
    Java Code:
    public void writeEmployeeToFile(File dir) {
    		boolean done = false;
    		while(!done){
    			try {
    				FileInputStream fileIn = new FileInputStream(dir);
    				ObjectInputStream objectIn = new ObjectInputStream(fileIn);
    				while(fileIn.available() > 0){
    					try {
    						employees.add((Employee) objectIn.readObject());
    					} catch (ClassNotFoundException e) {
    						System.out.println("Error while reading object");
    					}
    				}
    				objectIn.close();
    				FileOutputStream fileOut = new FileOutputStream(dir, true);
    				ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
    				for(int i = 0; i < employees.size(); i++){
    					objectOut.writeObject(employees.get(i));
    				}
    				objectOut.writeObject(getEmployee());
    				employees.clear();
    				objectOut.flush();
    				objectOut.close();
    			} catch (IOException e) {
    				try {
    					FileOutputStream fileOut = new FileOutputStream(dir, true);
    					ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
    					objectOut.writeObject(getEmployee());
    					objectOut.flush();
    					objectOut.close();
    				} catch (IOException e1) {
    					System.out.println("You REALLY fucked up");
    				}
    			}
    			System.out.println("Add another employee to " + dir.getName() + "?\n[1] Yes\n[2] No");
    			int selection = getInput(1, 2);
    			if(selection == 2){
    				done = true;
    			}
    		}
    	}
    Using this method to get the Employee object
    Java Code:
    public Employee getEmployee() {
    		System.out.println("Employee ID: ");
    		int ID = getInput(1, Integer.MAX_VALUE);
    		System.out.println("Employee name: ");
    		String name = scan.nextLine();
    		System.out.println("Employee occupation: ");
    		String job = scan.nextLine();
    		Employee emp = new Employee(ID, name, job);
    		return emp;
    	}
    Last edited by Aubanis; 04-16-2011 at 06:12 AM.

  5. #5
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Please use [ code ] code goes here [ /code ] (without the spaces) and indent your code so it can be easier to read. The only thing that stands out is you supplied "true" as the second parameter to FileOutputStream. This means you are appending data to the file. Are you sure there were previous objects in that file? That's where the problem might be at.

Similar Threads

  1. java.io.StreamCorruptedException
    By goto in forum Networking
    Replies: 8
    Last Post: 11-14-2013, 01:44 PM
  2. StreamCorruptedException
    By Fortu in forum Advanced Java
    Replies: 1
    Last Post: 04-10-2011, 09:56 PM
  3. StreamCorruptedException
    By wikisb in forum New To Java
    Replies: 3
    Last Post: 10-28-2010, 07:16 AM
  4. StreamCorruptedException
    By cristo_haris in forum Advanced Java
    Replies: 11
    Last Post: 04-20-2009, 03:44 PM
  5. java.io.StreamCorruptedException
    By elizabeth in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •