Operation file attributes

1) Create a file object with three ways "d: \ java123 \ abc.txt" , and get the name of this file and the file size, whether hidden files, path, an absolute path, parent path, last modified time information.
(2) the method by createNewFile directory in the current New File "a.txt", and get the path of this file, the absolute path, parent path, then delete, delete information based on whether or not to print successfully deleted the return value.
(3) listed in the directory "d: \ java123" all files and directories; directory: under "d \ java123" all suffix "java" file.

代码:
package class2;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyFile{
    public static void main(String[] args) {
        Date now=new Date();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        File file = new File("d:/java123/abc.txt");
        File file1= new File("d://java123//abc.txt");
        File file2= new File("d:\\java123\\abc.txt");
        System.out.println("文件的名称是:"+file.getName());
        System.out.println("文件的大小是:"+file.length());
        System.out.println("文件是否是隐藏文件:"+file.isHidden());
        System.out.println("文件的路径为:"+file.getPath());
        System.out.println("文件的绝对路径为:"+file.getAbsolutePath());
        System.out.println("文件的父路径为:"+file.getParent());
        System.out.println("文件最后修改时间是:"+dateFormat.format(file.lastModified()));
        System.out.println("\n");
        File f = new File("d:/java123/a.txt");
        if(!f.exists()) {
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
        System.out.println("文件的路径为:"+f.getPath());
        System.out.println("文件的绝对路径为:"+f.getAbsolutePath());
        System.out.println("文件的父路径为:"+f.getParent());
        if(f.delete()) {
            System.out.println("文件删除成功!");
        }
        System.out.println("\n");
        File dir=new File("d:/java123");
        String[] fileNames=dir.list();
        System.out.println("list all files..."+fileNames.length);
        for(int i=0;i<fileNames.length;i++) {
            System.out.println(fileNames[i]);
        }
        System.out.println("\n");
        
        FileAccept con=new FileAccept("java");
        fileNames =dir.list(con);
        System.out.println("list java files..."+fileNames.length);
        for(int i=0;i<fileNames.length;i++) {
            System.out.println(fileNames[i]);
        }
    }
}

class FileAccept implements FilenameFilter{
    String extName="";
    FileAccept(String extName){
        this.extName=extName;
    }
    public boolean accept(java.io.File file,String fileName) {
        return fileName.endsWith(extName);
    }
}

Run shot:

Read file content
(1) 15.3 [Example P278] materials, the use of the class FileOutputStream WORD.TXT write information to a file, and then by the FileInputStream file to read the data on the console.

代码:
package class2;

import java.io.*;
public class MyIO {
    public static void main(String[] args) {
        String str="abcd1234文件...---";
        File file=new File("d:/java123/word.txt");
        try {
            FileOutputStream out=new FileOutputStream(file,true);
            FileInputStream put=new FileInputStream(file);
            byte[] byt1=str.getBytes();
            out.write(byt1);
            out.close();
            byte[] byt2=new byte[(int)file.length()];
            put.read(byt2);
            String str1=new String(byt2);
            System.out.print(str1);
            put.close();
        } catch (FileNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

Run shot:

(2) a class FileOutputStream and FileInputStream class implementation file copy, and verify word, jpg, avi, text files (txt) of replication.

代码:
package class2;

import java.io.*;
public class MyCopy {
    public static void main(String[] args) {
        File file1=new File("d:/java123/copy1.txt");
        File file2=new File("d:/java123/copy2.txt");
        try {
            FileInputStream iput=new FileInputStream(file1);
            FileOutputStream oput=new FileOutputStream(file2);
            FileInputStream iput1=new FileInputStream(file2);
            byte[] byt=new byte[(int)file1.length()];
            iput.read(byt);
            String str1=new String(byt);
            oput.write(byt);
            oput.close();
            byte[] byt2=new byte[(int)file2.length()];
            iput1.read(byt2);
            String str2=new String(byt2);
            if(str1.equals(str2)) {
                System.out.println("文件复制成功!");
            }
            iput.close();
            iput1.close();
        } catch (FileNotFoundException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}

Run shot:

Guess you like

Origin www.cnblogs.com/quan-2723365710/p/12163882.html