IO streams Java file processing, input and output

File type:

 To java file (this file refers to the file name and path) is processed, you first need to do is how to find the corresponding file, java need to create an instance of the corresponding files through the File class.

 File class constructor:

  File (Stinr path): In general we create a File, it is to create the path string. The path can be to a file or directory, or may be absent path. Of course, there are other methods of construction, but so far I have not used.

 File class has a number of methods, the most commonly used method is as follows:

  boolean File.exists (): whether the file exists, returns true presence, absence access false;

  File.createNewFile (): Creates a path to the file filename

  File.mkdir () and File.mkdirs (): create a directory with a path pointing to a directory

  Stirng File.list (): all sub file name and path of the File object list.

 

 File filters FilenameFilter:

  There is a list of the File class method in the above method, the method may receive a FilenameFilter parameter list only qualified by the parameter file.

  Code is as follows: the role of the program is to return the current directory, subdirectory has a name that contains the suffix ".java" file

public class IO {
    //文件过滤器
    public void fileNameFilter(){
        File file = new File(".");
        System.out.println(file.getAbsolutePath());
//        FilenameFilter filenameFileter = new FilenameFilter() {
//            
//            @Override
//            public boolean accept(File dir, String name) {
//                // TODO Auto-generated method stub
//                System.out.println("accept方法中的name="+name);
//                return name.endsWith(".java") || new File(name).isDirectory();
//            }
 //         };
 //         String [] = File.List for nameList (filenameFileter);
         // use Lambda expressions, corresponding to the above code 
        String [] nameList = file.list (( dir, name) -> name.endsWith ( ".java") || new new File (name) .isDirectory ());
         for (String name: nameList) { 
            System.out.println (name); 
        } 
    } 

    public  static  void main (String [] args) { 
        the IO IO = new new the IO (); 
        io.fileNameFilter (); 
    } 
}

Operating results are as follows:

D:\programme\javaEE\project\fzkjshop_project\fzkjshop_test\.
.settings
src
target

File class can only find the file or directory, you can create delete files and directories, but the file can not read and write. In order to achieve read and write files

 

I 流:

IO stream input / output basis, it can easily achieve data input / output operation, not only can achieve read and write files, you can also input and output of realistic network traffic, and so on. In Java, the different input / output source (keyboard, files, network connections, etc.) as the abstract representation of the "flow" (Stream), through the flow way, it allows Java programs to use the same way to access the different input / output sources. It is an ordered data stream from the origin (source) to the receiver (sink) of.

 The direction of flow may be divided into: an input stream and an output stream, an input only, can not be output; only the other output, not the input

  The transmission type may be divided into: byte stream and character stream, using a byte transfer, additional use of a character transfer

  The roles can be divided into: nodes with the process flow stream, from / write to a particular data stream IO devices (e.g., hard disk, network) read /, referred to as Node stream; flow stream encapsulated or nodes connected call processing . Process flow simpler than the flow nodes in the programming operation, while more efficient execution.

 4 provides an abstract base class in accordance with an input / output stream, and character / byte stream, the IO stream, i.e., the input character stream (Reader), the output character stream (Writer), input stream of bytes (the InputStream), the output stream of bytes ( the OutputStream), the plurality of classes IO stream 40 is provided by the abstract base class 4 derived and the following diagram:

 

According to the chart, we learned that the stream is used to access a file, a copy of the file now write a program through which flow:

public  class the IO {    
     // copy files 
    public  void copyFile (path String, String copyPath, int fileType) { 
        File file_1 = new new File (path);      
        File file_2 = new new File (copyPath); 
        
        the try {
             IF (! file_2.exists () ) { 
                file_2.createNewFile (); 
            } 
            IF (! file_1.exists ()) { 
                System.out.println ( "path to copy the file does not exist" ); 
            } 
            IF (file_1.isFile () && file_2.isFile()){
                byte[] readByte = new byte[1024];
                char[] chars = new char[512];
                int hasRead,account = 0;
                if(fileType == 0){ //当文件时字符类型时
                    Reader read  = new FileReader(file_1);    
                    Writer write = new FileWriter(file_2); 
                    while((hasRead = read.read(chars))>0){
                        write.write(chars,0,hasRead);
                    }
                    read.close();
                    write.close();
                }
                if(fileType == 1){ //当文件时字符类型时
                    InputStream in = new FileInputStream(file_1);    
                    OutputStream out = new FileOutputStream(file_2);
                    while((hasRead = in.read(readByte)) >0){
                        out.write(readByte,0,hasRead);
                    }
                    in.close();
                    out.close();                    
                }
            }    
        }catch(Exception e){}
    }
    
    public static void main(String[] args){
        IO io = new IO();
//        io.fileNameFilter();
        
        String bytePath = "D:\\data\\file\\img.jpg";
        String byteCopyPath ="D:\\data\\file\\img_1.jpg";
        String charPath = "D:\\data\\file\\book.txt";
        String charCopyPath = "D:\\data\\file\\book_1.txt";
        io.copyFile(charPath,charCopyPath,0);
        io.copyFile(bytePath, byteCopyPath,1);
    }
}

In the above code, we have realized a method of copying files, parameters are passed to copy the file path, after placing the copied file in accordance with the path and file type, and copy into byte characters copied by file type, each the flow is different.

Guess you like

Origin www.cnblogs.com/hjlin/p/11425815.html