Ввод - вывод. Файлы

Содержание

Слайд 2

V. Ввод - вывод 1. Файлы

V. Ввод - вывод
1. Файлы


Слайд 3

Класс File

Класс File

Слайд 4

Класс File package java.io; public class File { private String path;

Класс File

package java.io;
public class File {
private String path;
static

private FileSystem fs = FileSystem.getFileSystem();
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}
public File(String parent, String child)
public File(File parent, String child)
public boolean exists()
public String getPath() {
return path;
}
...
}

C

Слайд 5

Имя файла public class FileNameDemo { public static void main(String[] args)

Имя файла
public class FileNameDemo {
public static void main(String[] args) {

File file = new File("Hello World!");
System.out.println("File \""+ file.getPath() +"\" exists? " + file.exists());
}
}
File "Hello World!" exists? false
Слайд 6

Получение информации о пути к файлу и об имени файла


Получение информации
о пути к файлу и об имени файла

Слайд 7

Путь к файлу и имя файла public class File { ...

Путь к файлу и имя файла

public class File {
...

public String getName()
public File getParentFile()
public String getParent()
public File getAbsoluteFile()
public String getAbsolutePath()
}

C

Слайд 8

Имя файла public class PathInfoDemo { public static void main(String[] args)

Имя файла

public class PathInfoDemo {
public static void main(String[] args) {

File file = new File("I:\\FileIO\\somefile.txt");
System.out.println("File name: " + file.getName());
System.out.println("File directory: " + file.getParent());
System.out.println("File full path: " + file.getAbsolutePath());
}
}
File name: somefile.txt
File directory: I:\FileIO
File full path: I:\FileIO\somefile.txt
Слайд 9

Создание, удаление и переименование файлов


Создание, удаление и переименование файлов

Слайд 10

Класс File package java.io; public class File { ... public boolean

Класс File

package java.io;
public class File {
...
public boolean createNewFile()

public boolean delete()
public boolean renameTo(File dest)
}

C

Слайд 11

Создание файла public class CreateFileDemo { public static void main(String[] args)

Создание файла

public class CreateFileDemo {
public static void main(String[] args) {

try {
File file = new File("I:\\FileIO\\somefile.txt");
if (file.createNewFile()) {
System.out.println("File " + file.getName() + " was created");
} else {
System.out.println("Create operation failed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Слайд 12

Создание файла File somefile.txt was created

Создание файла
File somefile.txt was created

Слайд 13

Переименование файла public class RenameFileDemo { public static void main(String[] args)

Переименование файла

public class RenameFileDemo {
public static void main(String[] args) {

File oldfile = new File("I:\\FileIO\\somefile.txt");
File newfile = new File("I:\\FileIO\\newfile.txt");
if (oldfile.renameTo(newfile)) {
System.out.println("Rename succesful " + oldfile.getName() + " was renamed to " + newfile.getName());
} else {
System.out.println("Rename failed");
}
}
}
Слайд 14

Переименование файла Rename succesful somefile.txt was renamed to newfile.txt

Переименование файла
Rename succesful somefile.txt was renamed to newfile.txt

Слайд 15

Удаление файла public class DeleteFileDemo { public static void main(String[] args)

Удаление файла

public class DeleteFileDemo {
public static void main(String[] args) {

File file = new File("I:\\FileIO\\somefile.txt");
if (file.delete()) {
System.out.println(file.getName() + " was deleted!");
} else {
System.out.println("Delete operation failed.");
}
}
}
Слайд 16

Удаление файла newfile.txt was deleted!

Удаление файла
newfile.txt was deleted!

Слайд 17

Запрашивание свойств


Запрашивание свойств

Слайд 18

package java.io; public class File { ... public boolean isDirectory() public


package java.io;
public class File {
...
public boolean isDirectory()
public

boolean isFile()
public boolean isHidden()
public boolean canRead ()
public boolean canWrite ()
public boolean canExecute ()
public long lastModified()
public long length()
}

C

Слайд 19

Запрашивание атрибутов public class ListAttributesDemo { public static void main(String[] args)

Запрашивание атрибутов

public class ListAttributesDemo {
public static void main(String[] args) {

File file = new File("I:\\FileIO\\somefile.txt");
if (file.isHidden()) {
System.out.println("This file is hidden");
} else {
System.out.println("This file is not hidden");
}
if (file.canRead()) {
System.out.println("This file is readable");
} else {
System.out.println("This file is not readable");
}
if (file.canWrite()) {
System.out.println("This file is writable");
} else {
System.out.println("This file is not writable");
}
if (file.canExecute()) {
System.out.println("This file is executable");
} else {
System.out.println("This file is not executable");
}
}
}
Слайд 20

Запрашивание атрибутов This file is hidden This file is readable This

Запрашивание атрибутов
This file is hidden
This file is readable
This file is not

writable
This file is executable
Слайд 21

Запрашивание времени последнего изменения public class TimeDemo { public static void

Запрашивание времени последнего изменения

public class TimeDemo {
public static void main(String[]

args) {
File file = new File("F:\\FileIO\\somefile.txt");
System.out.println("Before Format : " + file.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println("After Format : " + sdf.format(file.lastModified()));
}
}
Слайд 22

Запрашивание времени последнего изменения Before Format : 1354014410437 After Format : 11/27/2012 18:06:50

Запрашивание времени последнего изменения
Before Format : 1354014410437
After Format : 11/27/2012 18:06:50

Слайд 23

Изменение свойств


Изменение свойств

Слайд 24

Изменение свойств public class File { ... public boolean setReadOnly() public

Изменение свойств

public class File {
...
public boolean setReadOnly()
public boolean

setWritable(boolean writable)
public boolean setExecutable(boolean executable)
public boolean setLastModified(long time)
}

C

Слайд 25

Изменение атрибутов public class SetAttributesDemo { public static void main(String[] args)

Изменение атрибутов

public class SetAttributesDemo {
public static void main(String[] args) {

File file = new File("I:\\FileIO\\somefile.txt");
file.setWritable(false);
//file.setReadOnly();
if (file.canRead()) {
System.out.println("This file is readable");
} else {
System.out.println("This file is not readable");
}
if (file.canWrite()) {
System.out.println("This file is writable");
} else {
System.out.println("This file is not writable");
}
}
}
Слайд 26

Изменение атрибутов This file is readable This file is not writable

Изменение атрибутов
This file is readable
This file is not writable

Слайд 27

Изменение времени последней модификации public class SetTimeDemo { public static void

Изменение времени последней модификации

public class SetTimeDemo {
public static void main(String[]

args) {
File file = new File("I:\\FileIO\\somefile.txt");
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
System.out.println("Last modified : " + sdf.format(file.lastModified()));
long mils = file.lastModified();
mils += 1000L * 60 * 60 * 24 * 365;
if (file.setLastModified(mils)) {
System.out.println("File " + file.getName()
+ " last modified time was changed");
} else {
System.out.println("Last modified change operation failed.");
}
System.out.println("Last modified : " + sdf.format(file.lastModified()));
}
}
Слайд 28

Изменение времени последней модификации Last modified : 12/20/2015 11:43:20 File somefile.txt

Изменение времени последней модификации
Last modified : 12/20/2015 11:43:20
File somefile.txt last modified

time was changed
Last modified : 12/19/2016 11:43:20
Слайд 29

Работа с директориями


Работа с директориями

Слайд 30

Работа с директориями public class File { ... public boolean mkdir()

Работа с директориями

public class File {
...
public boolean mkdir()
public

boolean mkdirs()
public boolean isDirectory()
public File[] listFiles()
public boolean delete()
public boolean renameTo(File dest)
}

C

Слайд 31

Получение списка файлов и директорий public class ListDirDemo { public static

Получение списка файлов и директорий

public class ListDirDemo {
public static void

main(String args[]) {
String dirname = "F:\\eclipse";
File file = new File(dirname);
if (file.isDirectory()) {
System.out.println("Directory of " + dirname);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println(files[i].getAbsolutePath()
+ " is a directory");
} else {
System.out.print(files[i] + " is a file ");
System.out.println(" is hidden? : " + files[i].isHidden());
}
}
} else {
System.out.println(dirname + " is not a directory");
}
}
}
Слайд 32

Получение списка файлов и директорий Directory of F:\eclipse F:\eclipse\.eclipseproduct is a

Получение списка файлов и директорий
Directory of F:\eclipse
F:\eclipse\.eclipseproduct is a file is

hidden? : false
F:\eclipse\artifacts.xml is a file is hidden? : false
F:\eclipse\configuration is a directory
F:\eclipse\dropins is a directory
F:\eclipse\eclipse.exe is a file is hidden? : false
F:\eclipse\eclipse.ini is a file is hidden? : false
F:\eclipse\eclipsec.exe is a file is hidden? : false
F:\eclipse\epl-v10.html is a file is hidden? : false
F:\eclipse\features is a directory
F:\eclipse\hs_err_pid3840.log is a file is hidden? : false
F:\eclipse\notice.html is a file is hidden? : false
F:\eclipse\p2 is a directory
F:\eclipse\plugins is a directory
F:\eclipse\readme is a directory
Слайд 33

Создание директорий public class CreateDirDemo { public static void main(String args[])

Создание директорий

public class CreateDirDemo {
public static void main(String args[]) {

String dirname = "I:\\FileIO";
File dir = new File(dirname);
if (dir.exists()) {
System.out.println("Directory of " + dirname +" exists");
}
else {
System.out.println("Directory of " + dirname +" does not exist");
}
if (dir.mkdir()) {
System.out.println("Directory " + dir.getName() + " was created");
} else {
System.out.println("Create operation failed.");
}
}
}
Слайд 34

Создание директорий Directory of I:\FileIO does not exist Directory FileIO was created

Создание директорий
Directory of I:\FileIO does not exist
Directory FileIO was created