Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active January 29, 2016 12:50
Show Gist options
  • Select an option

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

Select an option

Save Viacheslav77/363e68a55a637f619271 to your computer and use it in GitHub Desktop.
Написать программу, которая создаст текстовый файл и запишет в него список файлов (путь, имя, дата создания) из заданного каталога.
package CreateFile;
/*Написать программу, которая создаст текстовый файл и запишет в него список файлов (путь, имя, дата создания) из заданного каталога.*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
public class createFile {
public static String readText (String pth) throws IOException{
System.out.println("------------------------------------");
System.out.println("Читаем Текст из файла...." + pth);
System.out.println("------------------------------------");
byte[] str;
try (FileInputStream inFile = new FileInputStream(pth)){
str = new byte[inFile.available()];
inFile.read(str);
}
String text = new String(str);
System.out.println(text);
System.out.println("------------------------------------");
return text;
}
public static void writeText (String pth, ArrayList<String> list) throws IOException{
System.out.println("------------------------------------");
System.out.println("Записываем в файл..." + pth);
System.out.println("------------------------------------");
FileOutputStream inFile = new FileOutputStream(pth);
byte[] str;
try {
int i=0;
for (String s : list){
str = s.getBytes();
inFile.write(str);
i+=str.length;
}
} finally {
inFile.close();
}
}
private static void findFiles(String srcPath, ArrayList<String> list) throws IOException {
File dir = new File(srcPath);
System.out.println("----------------------------------");
System.out.println("Список файлов в дирректории: " + srcPath);
System.out.println("----------------------------------");
File[] files = dir.listFiles();
Date d = null;
for (File f : files) {
if (f.isFile()) {
d = new Date(f.lastModified());
list.add(f.getCanonicalPath() + " " + d + "\n");
}
}
for (String s : list)
System.out.print(s);
System.out.println("----------------------------------");
}
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<String>();
findFiles("d:\\1", list);
writeText ("d:\\1\\n3.txt", list);
String text = readText("d:\\1\\n3.txt");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment