codice:import java.io.FileReader; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.util.Scanner; import javax.swing.JFileChooser; import java.util.HashSet; import java.util.Iterator; /** * A class to read file from an input source and generate a data set * @author Simone * */ public class IndexReader { public static HashSet<AudioFile> audioFiles = new HashSet<AudioFile>(); public static HashSet<VideoFile> videoFiles = new HashSet<VideoFile>(); public static HashSet<ImageFile> imageFiles = new HashSet<ImageFile>(); public static HashSet<TextFile> textFiles = new HashSet<TextFile>(); public static void main(String[] args) throws FileNotFoundException, IOException { //Creo un nuovo oggetto per scegliere il percorso del file JFileChooser chooser = new JFileChooser(); FileReader in = null; if (chooser.showOpenDialog(null) == chooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); in = new FileReader(selectedFile); Scanner input = new Scanner(in); PrintStream p = System.out; for (AudioFile a : audioFiles) p.println(a); for (VideoFile a : videoFiles) p.println(a); for (ImageFile a : imageFiles) p.println(a); for (TextFile a : textFiles) p.println(a); input.close(); in.close(); } } /* * Il metodo per leggere da dal file di input. In questo caso sarà proprio * il nostro file index.dat */ public static void read(Scanner s) { if (!s.hasNext()) return; String type = s.next(); if (!s.hasNext()) return; String path = s.next(); if (!s.hasNextInt()) return; int size = s.nextInt(); if (!s.hasNext()) return; String creationData = s.next(); if (!s.hasNext()) return; String lastModifiedData = s.next(); if (!s.hasNext()) return; String description = s.next(); if (!s.hasNext()) return; String format = s.next(); if (type.equalsIgnoreCase("audio")) { if (!s.hasNextInt()) return; int length = s.nextInt(); audioFiles.add(new AudioFile(type, path, size, creationData, lastModifiedData, description, format, length)); } else if (type.equalsIgnoreCase("video")) { if (!s.hasNextInt()) return; int length = s.nextInt(); videoFiles.add(new VideoFile(type, path, size, creationData, lastModifiedData, description, format, length)); } else if (type.equalsIgnoreCase("immagine")) { if (!s.hasNextInt()) return; int width = s.nextInt(); if (!s.hasNextInt()) return; int height = s.nextInt(); imageFiles.add(new ImageFile(type, path, size, creationData, lastModifiedData, description, format, width, height)); } else textFiles.add(new TextFile(type, path, size, creationData, lastModifiedData, description, format)); } }