HDFSUtils { public static void write(Configuration config, byte[] bytes, String path) throws IOException { Path outPath = new Path(path); FileSystem fs = FileSystem.get(outPath.toUri(), config); fs.mkdirs(outPath.getParent()); try (FSDataOutputStream outputStream = fs.create(outPath)) { outputStream.write(bytes); outputStream.flush(); } }  static byte[] readBytes(String path); static byte[] readBytes(Path inPath); static List<String> readFile(String path); static List<String> readFile(Configuration config, String path); static void write(Configuration config, byte[] bytes, String path);  }
@Test public void writes_file_to_local_fs() throws Exception { String outText = "small brown bike and casket lottery"; String outFile = tempDir.getRoot().getAbsolutePath() + "/outfile.txt"; Configuration config = new Configuration(); config.set("fs.default.name", "file: HDFSUtils.write(config, outText.getBytes(StandardCharsets.UTF_8), outFile); String actual = TestUtils.read(new File(outFile)); assertThat("Text should match", actual, equalTo(outText)); }
@Test public void writes_file_to_local_fs_with_scheme_defined_only_in_uri() throws Exception { String outText = "small brown bike and casket lottery"; String outFile = tempDir.getRoot().getAbsolutePath() + "/outfile.txt"; Configuration config = new Configuration(); HDFSUtils.write(config, outText.getBytes(StandardCharsets.UTF_8), "file: String actual = TestUtils.read(new File(outFile)); assertThat("Text should match", actual, equalTo(outText)); }