Results 1 to 4 of 4
  1. #1
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default StreamCorruptedException problem

    I'm trying to create a program that reads and writes text files. I'm new to this concept. I can write text to a file in the form of a string. When I try to retrieve this text from the file to print it to the console, I get java.io.StreamCorruptedException. This is my method that writes to the file:
    Java Code:
    public void createFile(){
    		System.out.println("What is the name of the File you wish to create?");
    		dir = new File("files");
    		file = new File(dir, input.nextLine()+".txt");
    		
    		if(file.exists()){
    			file.delete();
    		}
    		try {
    			file.createNewFile();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		do{
    			System.out.println("Please input the Employees name:");
    			employee.append("Name: "+input.nextLine()+"\n");
    			System.out.println("Please input the Employees SSN");
    			employee.append("SSN: "+input.nextLine()+"\n");
    			System.out.println("Please input the Employees Job Title");
    			employee.append("Job Title: "+input.nextLine()+"\n");
    			System.out.println("Do you wish to create another Employee?");
    			String response = input.nextLine();
    			Boolean cont = false;
    			while(cont == false){
    				if(response.toLowerCase().equals("y") || response.toLowerCase().equals("yes")){
    					goOn = false;
    					cont = true;
    				}
    				else if(response.toLowerCase().equals("n")|| response.toLowerCase().equals("no")){
    					goOn = true;
    					cont = true;
    				}
    				else{
    					System.out.println("Please enter 'y' or 'n'");
    					cont = false;
    				}
    			}
    		}
    		while(!goOn);
    		try {
    			OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file));
    			out.write(employee.toString());
    			out.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}


    This is my reader method:
    Java Code:
    public void displayFile(){
    		File all = new File("files");
    		File view = null;
    		String[] files = all.list();
    		for(int i = 0; i < files.length; i++){
    			System.out.println(files[i].substring(0, files[i].length()-4));
    		}
    		do{
    			System.out.println("Which file would you like to view?");
    			view = new File(all, input.nextLine()+".txt");
    			if(!view.exists()){
    				System.out.println("That text file does not exist.");
    				goOn = false;
    			}
    			else{
    				goOn = true;
    			}
    		}
    		while(!goOn);
    		ObjectInputStream display = null;
    		FileInputStream dis = null;
    		try {
    			/////////////This is where the error is thrown//////////////
    			dis = new FileInputStream(view);
    			display = new ObjectInputStream(dis);
    			System.out.println((display.readObject()));
    			dis.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    I've tried deleting my text file and starting clean, but that doesn't fix it. Any help would be greatly appreciated.
    Last edited by rizowski; 04-19-2011 at 12:26 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

    You are getting the error because you have written no object data, which is non-human readable. You wrote plain characters to the file, so you should read it as plain characters. If you want to write objects of Strings, use ObjectOutputStream when writing to file.

  3. #3
    rizowski is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default

    Thanks. I didn't realize. Error fixed.

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

Similar Threads

  1. StreamCorruptedException
    By Fortu in forum Advanced Java
    Replies: 1
    Last Post: 04-10-2011, 10:56 PM
  2. Replies: 8
    Last Post: 11-05-2010, 11:26 PM
  3. StreamCorruptedException
    By wikisb in forum New To Java
    Replies: 3
    Last Post: 10-28-2010, 08:16 AM
  4. StreamCorruptedException
    By cristo_haris in forum Advanced Java
    Replies: 11
    Last Post: 04-20-2009, 04:44 PM
  5. java.io.StreamCorruptedException
    By elizabeth in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 07: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
  •