zsh_root
2024-01-02 7b595546af704983dbafcd0d385c8768ddacefc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package Method;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
 
public class NIOFileChannel {
    static FileOutputStream fileoutputstream=null;//ÎļþÊä³öÁ÷
 
    //´´½¨Ò»¸öÊä³öÁ÷->channle
 
    /**ÓÃNIO½«ÎļþдÈëij¸öÎļþ¼Ð
     * @param filepath ÎļþдÈë·¾¶*/
    public static void setFileToFolder(String filepath,String str){
        try {
            fileoutputstream=new FileOutputStream(filepath);
 
            //ͨ¹ýfileputstream »ñÈ¡¶ÔÓ¦µÄFileChannle
            //filechannelµÄÕæÊµÀàÐÍÊÇfilechannleImpl
            FileChannel fileChannle=fileoutputstream.getChannel();
 
            //´´½¨Ò»¸ö»º³åÇø ByteBuffel
            ByteBuffer byteBuffer=ByteBuffer.allocate(1024);
 
            //½«Str·ÅÈë byteBuffer
            byteBuffer.put(str.getBytes());
 
            //¶Ô byteBuffer½øÐÐflip£¬¶ÁдÇл»
            byteBuffer.flip();
 
            //½«byteBufferÖеÄÊý¾ÝдÈëµ½filechannel
            try {
                fileChannle.write(byteBuffer);
            } catch (IOException e) {
                // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
                e.printStackTrace();
            }
            if(fileoutputstream !=null) {
                try {
                    fileoutputstream.close();
                } catch (IOException e) {
                    // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
    }
 
    /**½«Îļþ´ÓÎļþ¼Ð¶ÁÈë
     * @throws FileNotFoundException */
    public static void setFolderToFile(String pathname) throws FileNotFoundException {
        //´´½¨ÎļþµÄÊäÈëÁ÷
        File file=new File(pathname);
 
        FileInputStream fileInputStream=new FileInputStream(file);
 
        //ͨ¹ýfileInputStream»ñÈ¡¶ÔÓ¦µÄfilechannle
        FileChannel fileChannle=fileInputStream.getChannel();
 
        //´´½¨»º³åÇø
        ByteBuffer byteBuffer=ByteBuffer.allocate((int)file.length());
 
        //½«Í¨µÀµÄÊý¾Ý¶ÁÈëµ½bufferÖÐ
        try {
            fileChannle.read(byteBuffer);
            //½«byteBufferתΪStringÊä³ö
 
            System.out.println(new String(byteBuffer.array()));
 
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
 
        try {
            fileInputStream.close();//¹Ø±ÕÊäÈëÁ÷
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
 
    }
 
    /**Îļþ¿½±´*/
    public static void fileTofile(File in,File out) {
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream = new FileInputStream(in);
        } catch (FileNotFoundException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
        FileChannel fileChannel01 = fileInputStream.getChannel();
 
        
        try {
            fileOutputStream = new FileOutputStream(out);
        } catch (FileNotFoundException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
        FileChannel fileChannel02 = fileOutputStream.getChannel();
 
        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
 
        while (true) { //Ñ­»·¶ÁÈ¡
 
            //ÕâÀïÓÐÒ»¸öÖØÒªµÄ²Ù×÷£¬Ò»¶¨²»ÒªÍüÁË            
            byteBuffer.clear(); //Çå¿Õbuffer
            int read=0;
            try {
                read = fileChannel01.read(byteBuffer);
            } catch (IOException e) {
                // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
                e.printStackTrace();
            }
            System.out.println("read =" + read);
            if(read == -1) { //±íʾ¶ÁÍê
                break;
            }
            //½«buffer ÖеÄÊý¾ÝдÈëµ½ fileChannel02 -- 2.txt
            byteBuffer.flip();
            try {
                fileChannel02.write(byteBuffer);
            } catch (IOException e) {
                // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
                e.printStackTrace();
            }
        }
 
        //¹Ø±ÕÏà¹ØµÄÁ÷
        try {
            fileInputStream.close();
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
    }
    
    /**Îļþ¿ìËÙ¿½±´·½·¨
     * @param sourse ÎļþÔ´
     * @param destÎļþÄ¿±êµØÖ·*/
    
    public static void file_kao_bei(File sourse,File dest) {
         //´´½¨Ïà¹ØÁ÷
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream = new FileInputStream(sourse);
        } catch (FileNotFoundException e1) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e1.printStackTrace();
        }
       
        try {
            fileOutputStream = new FileOutputStream(dest);
        } catch (FileNotFoundException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
 
        //»ñÈ¡¸÷¸öÁ÷¶ÔÓ¦µÄfilechannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();
 
        //ʹÓÃtransferFormÍê³É¿½±´
        try {
            destCh.transferFrom(sourceCh,0,sourceCh.size());
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
        
        //¹Ø±ÕÏà¹ØÍ¨µÀºÍÁ÷
        try {
            sourceCh.close();
            destCh.close();
            fileInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            // TODO ×Ô¶¯Éú³ÉµÄ catch ¿é
            e.printStackTrace();
        }
        
        
    }
 
}