Visualizzazione dei risultati da 1 a 2 su 2

Visualizzazione discussione

  1. #1

    [Java] oggettoFileInputStream e dichiarazione di metodo

    Ciao a tutti,
    è la seconda volta che scrivo su questo bel forum. Studio da pochissimo Java e sto analizzando il codice che riporto integralmente nella parte finale del post.
    In particolare non capisco l'interazione tra le seguenti righe di codice:

    1)FileInputStream fileDaLeggere1 = null;
    //sembra che qui venga creato l'oggetto fileDaLeggere1 e che venga valorizzato a null


    2)String fileName = "TristanoeIsotta.txt";
    // viene creata e valorizzata la stringa fileName
    .......
    3)fileDaLeggere1 = Test1(fileName);// Primo grande punto interrogativo.La stringa associata a fileName è l'argomento di Test1. Ma cos'è Test1 un metodo ??? Prima di usare Test1,ammesso che sia un metodo, non dovrei creare l'istanza della classe in cui è definito (es: se test1 è definito nella classe A la sintassi sarebbe: A test1 = new A(String fileName)) ????
    ......
    4)public static FileInputStream Test1(String fileName) throws Exception {
    FileInputStream nomeOggettoFileInputStream = null;
    ...........
    return nomeOggettoFileInputStream;
    }
    //sembra la definizione del metodo Test1 che ritorna l'oggetto nomeOggettoFileInputStream.
    //copiando l'oggetto nomeOggettoFileInputStream nell'oggetto fileDaLeggere1. E' corretto ??).


    Il nocciolo centrale è al punto
    3. Come faccio ad usare il metodo test1 appartenente alla classe FileInputStream senza aver creato prima l'oggetto ???
    Sotto il codice integrale.

    codice:
    package processo.gestioneEccezioni ;
     
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
     
    public class ProgrammaEccezioni {
     
    @SuppressWarnings("unused")
    public static void main(String args[]) throws Exception {
    FileInputStream fileDaLeggere1 = null;
    FileInputStream fileDaLeggere2 = null;
    String fileName = "TristanoeIsotta.txt";
     
    System.out.println("main: Starting " + ProgrammaEccezioni.class.getName() + " with file name = " + fileName);
     
    // get file input stream 1
    try {
    fileDaLeggere1 = Test1(fileName);
    } catch (FileNotFoundException ex) {
    System.out.println("main: Oops, FileNotFoundException caught");
    } catch (Exception ex) {
    System.out.println("main: Oops, general exception caught");
    }
     
    // get file input stream 2
    fileDaLeggere2 = Test2(fileName);
    System.out.println("main: " + ProgrammaEccezioni.class.getName() + " ended");
    }
     
    public static FileInputStream Test1(String fileName) throws FileNotFoundException {
    FileInputStream nomeOggettoFileInputStream = new FileInputStream(fileName);
    System.out.println("Test1: File input stream created");
    return nomeOggettoFileInputStream;
    }
     
    public static FileInputStream Test2(String fileName) throws Exception {
    FileInputStream nomeOggettoFileInputStream = null;
    try {
    nomeOggettoFileInputStream = new FileInputStream(fileName);
    } catch (FileNotFoundException ex) {
    throw new Exception("Test2: Oops, FileNotFoundException caught");
    //System.out.println("Test2: Oops, FileNotFoundException caught");
    } finally {
    System.out.println("Test2: finally block");
    }
    System.out.println("Test2: Returning from Test2");
    return nomeOggettoFileInputStream;
    }
     
    }
    
    ciao a presto
    Ultima modifica di NutellaPazza; 20-11-2014 a 02:29

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.