/** * Opens file and reads it to byte array. * * @param fs IgniteFs. * @param path File path. * @throws IgniteException If file can't be opened. * @throws IOException If data can't be read. */ private static void read(IgniteFileSystem fs, IgfsPath path) throws IgniteException, IOException { assert fs != null; assert path != null; assert fs.info(path).isFile(); byte[] data = new byte[(int) fs.info(path).length()]; try (IgfsInputStream in = fs.open(path)) { in.read(data); } System.out.println(); System.out.println(">>> Read data from " + path + ": " + Arrays.toString(data)); }
/** * Deletes file or directory. If directory is not empty, it's deleted recursively. * * @param fs IGFS. * @param path File or directory path. * @throws IgniteException In case of error. */ private static void delete(IgniteFileSystem fs, IgfsPath path) throws IgniteException { assert fs != null; assert path != null; if (fs.exists(path)) { boolean isFile = fs.info(path).isFile(); try { fs.delete(path, true); System.out.println(); System.out.println(">>> Deleted " + (isFile ? "file" : "directory") + ": " + path); } catch (IgfsException e) { System.out.println(); System.out.println( ">>> Failed to delete " + (isFile ? "file" : "directory") + " [path=" + path + ", msg=" + e.getMessage() + ']'); } } else { System.out.println(); System.out.println(">>> Won't delete file or directory (doesn't exist): " + path); } }
/** * Opens file and appends provided data to it. * * @param fs IGFS. * @param path File path. * @param data Data. * @throws IgniteException If file can't be created. * @throws IOException If data can't be written. */ private static void append(IgniteFileSystem fs, IgfsPath path, byte[] data) throws IgniteException, IOException { assert fs != null; assert path != null; assert data != null; assert fs.info(path).isFile(); try (OutputStream out = fs.append(path, true)) { System.out.println(); System.out.println(">>> Opened file: " + path); out.write(data); } System.out.println(); System.out.println(">>> Appended data to file: " + path); }
/** * Lists files in directory. * * @param fs IGFS. * @param path Directory path. * @throws IgniteException In case of error. */ private static void list(IgniteFileSystem fs, IgfsPath path) throws IgniteException { assert fs != null; assert path != null; assert fs.info(path).isDirectory(); Collection<IgfsPath> files = fs.listPaths(path); if (files.isEmpty()) { System.out.println(); System.out.println(">>> No files in directory: " + path); } else { System.out.println(); System.out.println(">>> List of files in directory: " + path); for (IgfsPath f : files) System.out.println(">>> " + f.name()); } System.out.println(); }
/** * Prints information for file or directory. * * @param fs IGFS. * @param path File or directory path. * @throws IgniteException In case of error. */ private static void printInfo(IgniteFileSystem fs, IgfsPath path) throws IgniteException { System.out.println(); System.out.println("Information for " + path + ": " + fs.info(path)); }