java file operations File class

1, the file path operations

testing method

@Test
    public void test5() {
        StringBuffer succBuffer = new StringBuffer("D:\\home\\weblogic\\tmp\\busdatagram\\141100\\20190627\\171320");
        System.out.println(succBuffer.toString());
        StringBuffer s1 = succBuffer;
        s1.insert(s1.indexOf("busdatagram") , "SUCCESS_DATA\\");
        System.out.println(s1);
        succBuffer.insert(succBuffer.indexOf("busdatagram") + 12, "SUCCESS_DATA\\");
        System.out.println(succBuffer.toString());
    }

Test Results

D:\home\weblogic\tmp\busdatagram\141100\20190627\171320
D:\home\weblogic\tmp\SUCCESS_DATA\busdatagram\141100\20190627\171320
D:\home\weblogic\tmp\SUCCESS_DATA\busdatagram\SUCCESS_DATA\141100\20190627\171320
succBuffer path D: \ home \ weblogic \ tmp \ busdatagram \ 141100 \ 20190627 \ 171320 written D: \\ home \\ weblogic \\ tmp \\ busdatagram \\ 141100 \\ 20190627 \\ 171320, 
if the direct write D: \ home \ weblogic \ tmp \ busdatagram \ 141100 \ 20190627 \ 171320 will not find path
s1.insert (A, B) from the first character s1 1st attribute A start date

2, to find non-folder files

@Test
     public  void Test3 () { 
        String path = "D: \\ WebLogic Home \\ \\ \\ tmp 20.19112 million busdatagram \\ \\ \\ 113 316 141 100" ; 
        traverseFolder2 (path); 
    } 

    Private  void traverseFolder2 (String path) { 
        file file = new new file (path);
         IF (File.Exists ()) {
             // the listFiles is to obtain the absolute path of all files and directories in the directory 
            file [] = files File.listFiles ();
             IF ( null == == 0 || files.length files ) { 
                System.out.println ( "folder is empty!" );
                return ; 
            } the else {
                 for (File file2: Files) {
                     IF (file2.isDirectory ()) { 
                        System.out.println ( "Folder:" + file2.getAbsolutePath ()); 
                        traverseFolder2 (file2.getAbsolutePath ()); 
                    } the else { 
                        System.out.println ( "file:" + file2.getAbsolutePath ()); 
                    } 
                } 
            } 
        } the else { 
            System.out.println ( "folder path does not exist"  );
        }
    }

result

Folder: D: \ home \ WebLogic \ tmp \ busdatagram \ 141 100 \ 20.19112 million \ 113 316 \ AA 
folder: D: \ home \ WebLogic \ tmp \ busdatagram \ 141 100 \ 20.19112 million \ 113 316 \ AA \ bb 
file: D: \ home \ WebLogic \ tmp \ busdatagram \ 141 100 \ 20.19112 million \ 113 316 \ AA \ bb \ ad.docx 
file: D: \ home \ WebLogic \ tmp \ busdatagram \ 141 100 \ 20.19112 million \ 113 316 \ AA \ cc.docx 
file: D: \ home \ WebLogic \ tmp \ busdatagram \ 141.1 thousand \ 20.19112 million \ 113 316 \ BCPR.doc





Guess you like

Origin www.cnblogs.com/hoje/p/11898378.html