Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active February 23, 2016 19:18
Show Gist options
  • Select an option

  • Save Viacheslav77/fc897d8c93191e615ce6 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/fc897d8c93191e615ce6 to your computer and use it in GitHub Desktop.
Написать ф-ю, которая сохранит содержимое каталога в список и выведет первые 5 элементов на экран.
package SafeCatalog;
import java.io.File;
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);
}
}
package SafeCatalog;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadFile {
public 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++;
}
}
}
}
package SafeCatalog;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class WriteToFileList {
public WriteToFileList(String path, ArrayList<File> list) 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);
}
}
}
}
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);
WriteToFileList wtf = new WriteToFileList (pathFile, ff.getList());
ReadFile rf = new ReadFile (pathFile, 5) ;
}
}
----------------------------------
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
----------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment