Guruh talabasi Qurbonov Tohirjonning Dasturlash1 fanidan taqdimoti


ps.println("Stackoverflow documentation seems fun.")


Download 98.42 Kb.
bet2/8
Sana18.06.2023
Hajmi98.42 Kb.
#1571789
1   2   3   4   5   6   7   8
Bog'liq
650-22guruh talabasi Qurbonov Tohirjonning Dasturlash1 fanidan taqdimoti

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());

}

}

}

ZIP fayl tarkibiga kirish

  • 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();
}

Download 98.42 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling