Last active
February 23, 2016 22:56
-
-
Save Viacheslav77/42edb41f8b388874183b to your computer and use it in GitHub Desktop.
Написать ф-ю, которая сохранит содержимое каталога в список и выведет первые 5 элементов на экран, через методы
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package SafeCatalog; | |
| import java.io.BufferedReader; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| public class findFiles { | |
| ArrayList <File> list; | |
| public findFiles (String Path) throws IOException{ | |
| System.out.println("----------------------------------"); | |
| System.out.println("A list of files a direcktory..." + Path); | |
| System.out.println("----------------------------------"); | |
| list = new ArrayList<File>(); | |
| File f = new File (Path); | |
| File[] listFiles = f.listFiles(); | |
| int i=0; | |
| for(File f1 :listFiles){ | |
| list.add(f1); | |
| System.out.println(list.get(i++)); | |
| } | |
| } | |
| public ArrayList<File> getList(){ | |
| return list; | |
| } | |
| public File getIndex(int i){ | |
| return list.get(i); | |
| } | |
| public void WriteToFileList(String path) throws IOException{ | |
| System.out.println("------------------------------------"); | |
| System.out.println("Write to the file list..." + path); | |
| System.out.println("------------------------------------"); | |
| byte [] bt; | |
| try (FileOutputStream inFile = new FileOutputStream(path)){ | |
| for(File f: list){ | |
| bt = (f.getCanonicalPath() + System.getProperty("line.separator")).getBytes(); | |
| inFile.write(bt); | |
| } | |
| } | |
| } | |
| public void ReadFile (String path, int cont) throws IOException{ | |
| System.out.println("----------------------------------------"); | |
| System.out.println("Shows the first five files..." + path); | |
| System.out.println("----------------------------------------"); | |
| String line; | |
| try(BufferedReader reader = new BufferedReader(new FileReader(path))){ | |
| int i = 0; | |
| while ((line = reader.readLine()) != null) { | |
| System.out.println(line); | |
| if(i ==(cont-1)){ | |
| System.out.println("----------------------------------------"); | |
| return; | |
| } | |
| i++; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ---------------------------------- | |
| A list of files a direcktory...d:\1 | |
| ---------------------------------- | |
| d:\1\2 | |
| d:\1\5.txt | |
| d:\1\D.docx | |
| d:\1\eAtygv | |
| d:\1\FileWriteThreat.txt | |
| d:\1\info.txt | |
| d:\1\N1.txt | |
| d:\1\n2.txt | |
| d:\1\n3.txt | |
| d:\1\NA.java | |
| d:\1\Новая папка | |
| ------------------------------------ | |
| Write to the file list...d:\1\5.txt | |
| ------------------------------------ | |
| ---------------------------------------- | |
| Shows the first five files...d:\1\5.txt | |
| ---------------------------------------- | |
| D:\1\2 | |
| D:\1\5.txt | |
| D:\1\D.docx | |
| D:\1\eAtygv | |
| D:\1\FileWriteThreat.txt | |
| ---------------------------------------- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package SafeCatalog; | |
| /*Написать ф-ю, которая сохранит содержимое каталога в список и | |
| выведет первые 5 элементов на экран.*/ | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.util.ArrayList; | |
| public class MyClass { | |
| public static void main(String[] args) throws IOException { | |
| String pathDir = "d:\\1"; | |
| String pathFile = "d:\\1\\5.txt"; | |
| findFiles ff = new findFiles(pathDir); | |
| ff.WriteToFileList(pathFile); | |
| ff.ReadFile(pathFile, 5); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment