The I/o classes and Interfaces



Download 182,82 Kb.
Sana20.06.2022
Hajmi182,82 Kb.
#685890
Bog'liq
File

The I/O Classes and Interfaces

Ismoilov Do’stmurod

https://t.me/Ismoilov9922

I/O -

I/O - interfaces are defined by java.io

I/O - constructors of File class

  • File(String directoryPath)
  • File(String directoryPath, String filename)
  • File(File dirObj, String filename)
  • File(URI uriObj)

I/O - Example of File objects

  • File f1 = new File("/");
  • File f2 = new File("/","autoexec.bat");
  • File f3 = new File(f1,"autoexec.bat");

I/O - Note


Fayl manzilida ajratuvchi belgisi Windows va Unixda bir-birdan farq qiladi. Windowsda bu belgi “\” kabi bo’lsa, Unixda esa aksi “/” kabidir. Javaning windowsdagi versiyasidan foydalanayotganda “\” belgi \\ belgi bilan yozilishi kerakligini unutmaslik kerak.

I/O - File methods

I/O - File methods


p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Absolute Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");

I/O -

  • boolean renameTo(File newName)
  • boolean delete( ) – o’chirish
  • void deleteOnExit( ) – Object bilan bog’langan fayl o’chadi va Virtual mashina yopiladi.
  • long getFreeSpace( ) – Bo’shjoyni olish
  • long getTotalSpace( ) – Umumiy joyni olish
  • long getUsableSpace( ) – foydalanish mumkin bo’lgan bo’sh joylar
  • boolean isHidden( ) – ko’rmaslikka tekshirish
  • boolean setLastModified(long millisec) – O’zgartiligan vaqtni o’zgartirish (1970 yil sanoq boshi)
  • boolean setReadOnly( ) – faqat o’qish ruxsatini o’rnatish

I/O - Directories

  • toPath( ) – Bu method JDK 7 dan boshlab qo’shilgan va Path sinfi objectini qaytaradi.
  • String[ ] list( ) – Tarkibida mavjud bo’lgan fayl va direktoriyalarni ro’yhatini qaytaradi.

I/O - Directories


File file = new File("D:\\"); String s[] = file.list(); for (String value : s) { System.out.println(value); File file1 = new File("D:\\" + value); if (file1.isDirectory()){ System.out.println("Directory: " + file1.getName()); } if (file1.isFile()){ System.out.println("File: " + file1.getName()); } }

I/O - All files


public static ArrayList getAllFileInDirectory(String rootPath) { ArrayList list = new ArrayList<>(); File file = new File(rootPath); String s[] = file.list(); if (s != null) { for (String value : s) { File file1 = new File(rootPath + "\\" + value); if (file1.isDirectory()) { list.addAll(getAllFileInDirectory(rootPath + "\\" + value)); } if (file1.isFile()) { list.add(file.getPath() + "\\" + value); } } } return list; }

I/O - All files


ArrayList list = getAllFileInDirectory("D:\\"); list.forEach(System.out::println);
Berilgan katalogdagi barcha fayllarni ko’rish. Masalan D:\ diskdagi barcha fayllarni ko’rish uchun:

I/O - FilenameFilter


boolean accept(File directory, String filename)
public class OnlyExt implements FilenameFilter { String ext; public OnlyExt(String ext) { this.ext = "." + ext; } public boolean accept(File dir, String name) { return name.endsWith(ext); } }

I/O - FilenameFilter


String dirname = "D:\\"; File f1 = new File(dirname); FilenameFilter only = new OnlyExt("cpp"); String s[] = f1.list(only); for (String value : s) { System.out.println(value); }

I/O - Stream

  • Ma’lumotlarni oqimli ketma-ketlikda tas’virlash mumkin. Ikki xil oqim mavjud:
  • InPutStream − bundan foydalaniladi ma’lumotlarni manbaadan o’qish uchun.
  • OutPutStream − bunday foydalaniladi ma’lumotlarni yozish uchun.

I/O - Byte Streams


FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } }

I/O - Character Streams


FileReader in = null; FileWriter out = null; try { in = new FileReader("input.txt"); out = new FileWriter("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } }finally { if (in != null) { in.close(); } if (out != null) { out.close(); } }

Download 182,82 Kb.

Do'stlaringiz bilan baham:




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish