Probleme mit Exception und Jcifs

obmib

Grünschnabel
Hallo,

ich versuche mich gerade an Jcifs und habe die Beispieldatei "SlowRead.java" gefunden. Die sieht so aus:
Code:
import jcifs.smb.SmbFile;

import jcifs.smb.SmbFileInputStream;

import java.io.FileOutputStream;

public class SlowRead {



    public static void main( String argv[] ) throws Exception{



        SmbFileInputStream in = new SmbFileInputStream("smb://freigabe/text.txt");



        byte[] b = new byte[10];

        int n, tot = 0;

        while(( n = in.read( b )) > 0 ) {

            System.out.write( b, 0, n );

	    System.out.println(b);

            tot += n;

            Thread.sleep( 1700 );

        }



        in.close();

    }

}

Bis hierhin funktioniert noch alles. Was mache ich jetzt allerdings, wenn ich SmbFileInputStream nicht in der main sondern in einer anderen Methode oder Funktion benutzen möchte.
Bei der main hilft mir das throws Exception. Bei anderen Methoden kann ich das allerdings nicht benutzen und bekomme massig Fehlermeldungen vom Compiler zugeschmissen:

Code:
unreported exception jcifs.smb.SmbException; m
ust be caught or declared to be thrown

        SmbFileInputStream in = new SmbFileInputStream("smb://testserver/test.txt");

                                ^

unreported exception java.io.IOException; must
 be caught or declared to be thrown

        while(( n = in.read( b )) > 0 ) {

                           ^

unreported exception java.lang.InterruptedExce
ption; must be caught or declared to be thrown

                Thread.sleep( 1700 );

                            ^

unreported exception java.io.IOException; must
 be caught or declared to be thrown

        in.close();

                ^

Hat jemand eine Idee wie ich das Problem lösen kann?
 
Zuletzt bearbeitet:
Hallo

Ich denke es genügt den gesamten Teil in einen try-catch Block zu packen um auftauchende Exceptions abzufangen. Also etwa so:

Code:
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import java.io.FileOutputStream;

public class SlowRead
{
    public static void main( String argv[] ) throws Exception
    {
        try
        {
            SmbFileInputStream in = new SmbFileInputStream("smb://freigabe/text.txt");
            byte[] b = new byte[10];
            int n, tot = 0;
            while(( n = in.read( b )) > 0 )
            {
                System.out.write( b, 0, n );
	        System.out.println(b);
                tot += n;
                Thread.sleep( 1700 );
            }
            in.close();
        }
        catch(SmbExcepion e1)
        {
             e1.printStackTrace();
        }
        catch(IOException e2)
        {
             e2.printStackTrace();
        }
        catch(InterruptedException e3)
        {
             e3.printStackTrace();
        }
    }
}

Freundliche Grüsse
CKingZesi
 
Zuletzt bearbeitet:
Zurück