ps.println("Stackoverflow documentation seems fun."); ps.println(); ps.println("I love Java!"); ps.printf("Today is: %1$tm/%1$td/%1$tY", LocalDate.now()); ps.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } Katalogda takrorlash va fayl bo'yicha filtrlashkengaytma public void iterateAndFilter() throws IOException { Path dir = Paths.get("C:/foo/bar"); PathMatcher imageFileMatcher = FileSystems.getDefault().getPathMatcher( "regex:.*(?i:jpg|jpeg|png|gif|bmp|jpe|jfif)"); try (DirectoryStream
stream = Files.newDirectoryStream(dir, entry -> imageFileMatcher.matches(entry.getFileName()))) { for (Path path : stream) { System.out.println(path.getFileName()); } } } - Java 7-ning FileSystem API fayl tizimidagi Java NIO fayl API-si yordamida Zip faylidan yoki unga yozuvlarni oʻqish va qoʻshish imkonini beradi.
- boshqa fayl tizimida ishlash kabi.
- FileSystem - bu foydalanishdan keyin to'g'ri yopilishi kerak bo'lgan resurs, shuning uchun resurslarni sinab ko'rish bloki
- foydalanilsin.
- Mavjud fayldan o'qish
- Path pathToZip = Paths.get("path/to/file.zip");
- try(FileSystem zipFs = FileSystems.newFileSystem(pathToZip, null)) {
- Path root = zipFs.getPath("/");
- ... //access the content of the zip file same as ordinary files
- } catch(IOException ex) {
- ex.printStackTrace();
- }
Creating a new file
Map env = new HashMap<>();
env.put("create", "true"); //required for creating a new zip file
env.put("encoding", "UTF-8"); //optional: default is UTF-8
URI uri = URI.create("jar:file:/path/to/file.zip");
try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
Path newFile = zipFs.getPath("/newFile.txt");
//writing to file
Files.write(newFile, "Hello world".getBytes());
} catch(IOException ex) {
ex.printStackTrace();
}
Do'stlaringiz bilan baham: |