A cikin 'yan lokutan nan, sarrafa fayiloli ya zama muhimmin al'amari a warware ayyuka masu yawa na shirye-shirye. Ɗayan irin wannan aikin gama gari shine karanta abun ciki na fayil cikin kirtani a Java. Wannan labarin yana nufin samar da cikakkiyar mafita ga wannan matsala yayin da yake mai da hankali kan ƙarfi, sauƙin fahimta, da inganci.
Java yana ba da hanyoyi da yawa don karanta fayil a cikin kirtani, daga amfani da ainihin FileReader zuwa ƙarin ɗakunan karatu kamar Apache Commons IO. A cikin wannan labarin, za mu bincika ɗaya irin wannan hanyar ta amfani da ajin Fayiloli a cikin Java NIO (Sabuwar Input/Fitarwa).
Fayilolin Java NIO
Java NIO, wanda aka gabatar a cikin Java 1.4, wani saitin ɗakunan karatu ne da ke da nufin sa ayyukan I/O ba tare da toshewa ba mafi inganci da sauƙin aiki da su. Ajin Fayiloli, wani ɓangare na fakitin java.nio.file, yana ba da hanyoyin amfani da yawa don aiwatar da ayyuka daban-daban na fayil, gami da karanta fayil a cikin kirtani.
Yanzu, bari mu shiga bayanin mataki-mataki na lambar.
Mataki 1: Da farko, za mu buƙaci shigo da fakitin da suka dace:
"Jawa
shigo da java.nio.file.Files;
shigo da java.nio.file.Hanyoyi;
shigo da java.io.IOException;
““
Mataki 2: Na gaba, za mu ƙirƙiri hanyar da ke ɗaukar hanyar fayil azaman shigarwa kuma dawo da abun cikin fayil azaman kirtani:
"Jawa
FayilToString (Path filePath) na jama'a a tsaye
Fayil na igiyaContent = "";
gwada {
fileContent = sabon String (Files.readAllBytes(Paths.get(filePath)));
} kama (IOException e) {
e.printStackTrace();
}
dawo da abun ciki na fayil;
}
““
Mataki 3: A ƙarshe, za mu iya amfani da hanyarmu a cikin babban():
"Jawa
jama'a a tsaye mara amfani babba (Kirtani [] args) {
String filePath = "hanya/to/your/file.txt";
Fayil ɗin kirtani = karantaFileToString(FilePath);
System.out.println(fileContent);
}
““
Yanzu, bari mu zurfafa zurfafa cikin hanyoyin da azuzuwan da ake amfani da su a cikin maganin.
Hanyoyi.samu ()
Ajin java.nio.file.Paths yana ba da tsayayyen hanyoyi don gina abubuwan Tafarki. Ɗaya daga cikin waɗannan hanyoyin shine samun (). Hanyar Paths.get() tana ɗaukar kirtani da ke wakiltar hanyar fayil kuma ta canza shi zuwa abin Hanya. Ana amfani da wannan abu daga baya azaman hujja zuwa hanyar Files.readAllBytes().
- Hanyar Fayil na igiya: Hanyar fayil ɗin an bayar azaman hujja.
- Abun hanya: Abun Hanyar da aka dawo yana wakiltar fayil ɗin a ƙayyadadden hanyar.
Files.readAllBytes()
Hanyar Files.readAllBytes() wani yanki ne na java.nio.file.Files class. Yana karanta duk bytes daga fayil kuma yana dawo da tsararrun byte. Ana amfani da wannan tsararrun byte don gina kirtani, samar da hanya mai sauƙi da inganci don karanta abun cikin fayil.
- Abun hanya: Abun Hanyar da ke wakiltar fayil ɗin.
- byte[]: Tsarin byte mai ɗauke da abun cikin fayil ɗin.
A taƙaice, ta amfani da ajin Fayilolin Java NIO da hanyoyin amfani da shi, za mu iya karanta abun cikin fayil da kyau cikin kirtani. Haɗin hanyoyin Hanyoyi.get() da Files.readAllBytes() yana ba da ƙwaƙƙwa, mai sauƙin fahimta, da ingantaccen bayani ga wannan aikin gama gari. Ta hanyar fahimtar ayyukan ciki na wannan ɗakin karatu mai ƙarfi, zaku iya amfani da cikakkiyar damarsa don buƙatun sarrafa fayil ɗin Java.