Datentypen Rückgabewert von "new FileInputStream"

chrisotph84

Neues Mitglied
Servus,

zum abfangen einer Exception möchte ich wissen, ob das Öffnen einer Datei mit FileInputStream erfolgreich war. Wie sieht der Rückgabewert im Fehlerfall aus?

Java:
		FileInputStream fStream = new FileInputStream("datei1.txt");
		int ret = fStream.read();
		fStream.close();
		
		[B][COLOR="Red"]if (fStream.equals(null)) {                       //<<<<<========  Dass frisst er nicht!!!![/COLOR][/B]
			throw new MyIOException();
		}

Wer mehr wissen muss, hier die ganze Klasse.

Java:
import java.io.*;
//import java.util.*;
public class ExceptionsDemo{
	
	/* complete the class MyIOException. */
	public class MyIOException extends Exception {

		public MyIOException(String message) {
			super(message);
		}
		public MyIOException() { super(); }
	}
	public static void main(String[] arg) {
		ExceptionsDemo myDemo = new ExceptionsDemo();
		myDemo.mainFunction();
	}
		
	private Object store;
	private Float f;

	public void mainFunction() {
		
		/*    The function readFile() could create an exception. Write a try ... catch block to explicitly catch the MyIOException exception, 
		      and print out the exception message. */
		try {
			readFile();
		}
		catch (MyIOException e) {
			System.out.println(e.getMessage());
		}
	}

	public void readFile() throws MyIOException {//throws MyIOException {

		/* The file datei1.txt doesn't exist. Write a try ... catch block in readFile() to catch the exception. 
		   Within the exception handler, perform the following: 
                      call printStackTrace() on the exception
                      create and throw a new MyIOException with the message Error in readFile() */
		
	
		FileInputStream fStream = new FileInputStream("datei1.txt");
		
		int ret = fStream.read();
		fStream.close();
		
		if (fStream.equals(null)) {                       //<<<<<========  Dass frisst er nicht!!!!
			throw new MyIOException();
		}		
		
		
	}
}

Danke für eure Hilfe!!!
 
Auszug aus javadoc:
Code:
public FileInputStream(String name) throws FileNotFoundException

Also musst du beim Öffnen auch diese Exception abfangen.
 
Java:
 public class MyIOException extends Exception

Damit fang ich in der aufrufenden Klasse alle Exceptions ab. Ich muss nur wissen, obs erfolgreich war.
 
Java:
 public class MyIOException extends Exception

Damit fang ich in der aufrufenden Klasse alle Exceptions ab. Ich muss nur wissen, obs erfolgreich war.

Damit deklarierst du nur eine Klasse, die von Exception erbt.

1. du könntest
Code:
fStream==null
prüfen
2. Du überprüfst, ob du mit dem Stream was anfangen kannst nach dem du schon aus ihm gelesen etc. hast... rein logisch gesehen ergibt das keinen Sinn.
 
Der Befehl new FileInputStream(…) erzeugt ein Objekt vom Typ FileInputStream, nichts anderes wird „zurückgegeben“. In dem Fall, daß das Objekt nicht erzeugt werden kann, wird eine Ausnahme geworfen, sonst passiert jedoch nichts; auch fStream wird nicht eingerichtet.

fStream.equals(null) ist ganz witzig, denn wenn fStream null wäre, würde die Ausnahme NullPointerException geworfen, denn dann stünde da null.equals(null).

Du möchtest vermutlich so etwas machen:
Java:
try {
    FileInputStream fStream = new FileInputStream("datei1.txt");
    int ret = fStream.read();
    fStream.close();
    … mache etwas mit ret …
} catch(IOException e) {
    e.printStackTrace();
    throw new MyIOException(e.getMessage());
}

Damit es einfach bleibt, habe ich die Reihenfolge erst einmal beibehalten, eigentlich müßte man hier aber noch darauf achten, daß der Strom richtig geschlossen wird.:
Java:
FileInputStream fStream = null;
try {
    fStream = new FileInputStream("datei1.txt");
    int ret = fStream.read();
    … mache etwas mit ret …
} catch(IOException e) {
    e.printStackTrace();
    throw new MyIOException(e.getMessage());
} finally {
    /* finally wird immer am Ende ausgeführt, ob nun der
     * try‐Block erfolgreich beendet wurde oder nicht. */
    if (fStream != null) {
        fStream.close();
    }
}
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben