⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutils.html

📁 用JAVA编写的,在做实验的时候留下来的,本来想删的,但是传上来,大家分享吧
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<a name="166" href="#166">166</a>             }<a name="167" href="#167">167</a>             <strong>for</strong> (<strong>int</strong> i = 0; i &lt; list.length; i++) {<a name="168" href="#168">168</a>                 copyFiles(<strong>new</strong> File(src, list[i]), filter,<a name="169" href="#169">169</a>                     <strong>new</strong> File(dest, list[i]), inSortedOrder, overwrite);<a name="170" href="#170">170</a>             }<a name="171" href="#171">171</a>         } <strong>else</strong> {<a name="172" href="#172">172</a>             copyFile(src, dest, overwrite);<a name="173" href="#173">173</a>         }<a name="174" href="#174">174</a>     }<a name="175" href="#175">175</a> <a name="176" href="#176">176</a>     <em>/**<em>*</em></em><a name="177" href="#177">177</a> <em>     * Copy the src file to the destination.</em><a name="178" href="#178">178</a> <em>     * </em><a name="179" href="#179">179</a> <em>     * @param src</em><a name="180" href="#180">180</a> <em>     * @param dest</em><a name="181" href="#181">181</a> <em>     * @return True if the extent was greater than actual bytes copied.</em><a name="182" href="#182">182</a> <em>     * @throws FileNotFoundException</em><a name="183" href="#183">183</a> <em>     * @throws IOException</em><a name="184" href="#184">184</a> <em>     */</em><a name="185" href="#185">185</a>     <strong>public</strong> <strong>static</strong> <strong>boolean</strong> copyFile(<strong>final</strong> File src, <strong>final</strong> File dest)<a name="186" href="#186">186</a>     throws FileNotFoundException, IOException {<a name="187" href="#187">187</a>         <strong>return</strong> copyFile(src, dest, -1, DEFAULT_OVERWRITE);<a name="188" href="#188">188</a>     }<a name="189" href="#189">189</a>     <a name="190" href="#190">190</a>     <em>/**<em>*</em></em><a name="191" href="#191">191</a> <em>     * Copy the src file to the destination.</em><a name="192" href="#192">192</a> <em>     * </em><a name="193" href="#193">193</a> <em>     * @param src</em><a name="194" href="#194">194</a> <em>     * @param dest</em><a name="195" href="#195">195</a> <em>     * @param overwrite If target file already exits, and this parameter is</em><a name="196" href="#196">196</a> <em>     * true, overwrite target file (We do this by first deleting the target</em><a name="197" href="#197">197</a> <em>     * file before we begin the copy).</em><a name="198" href="#198">198</a> <em>     * @return True if the extent was greater than actual bytes copied.</em><a name="199" href="#199">199</a> <em>     * @throws FileNotFoundException</em><a name="200" href="#200">200</a> <em>     * @throws IOException</em><a name="201" href="#201">201</a> <em>     */</em><a name="202" href="#202">202</a>     <strong>public</strong> <strong>static</strong> <strong>boolean</strong> copyFile(<strong>final</strong> File src, <strong>final</strong> File dest,<a name="203" href="#203">203</a>         <strong>final</strong> <strong>boolean</strong> overwrite)<a name="204" href="#204">204</a>     throws FileNotFoundException, IOException {<a name="205" href="#205">205</a>         <strong>return</strong> copyFile(src, dest, -1, overwrite);<a name="206" href="#206">206</a>     }<a name="207" href="#207">207</a>     <a name="208" href="#208">208</a>     <em>/**<em>*</em></em><a name="209" href="#209">209</a> <em>     * Copy up to extent bytes of the source file to the destination</em><a name="210" href="#210">210</a> <em>     *</em><a name="211" href="#211">211</a> <em>     * @param src</em><a name="212" href="#212">212</a> <em>     * @param dest</em><a name="213" href="#213">213</a> <em>     * @param extent Maximum number of bytes to copy</em><a name="214" href="#214">214</a> <em>     * @return True if the extent was greater than actual bytes copied.</em><a name="215" href="#215">215</a> <em>     * @throws FileNotFoundException</em><a name="216" href="#216">216</a> <em>     * @throws IOException</em><a name="217" href="#217">217</a> <em>     */</em><a name="218" href="#218">218</a>     <strong>public</strong> <strong>static</strong> <strong>boolean</strong> copyFile(<strong>final</strong> File src, <strong>final</strong> File dest,<a name="219" href="#219">219</a>         <strong>long</strong> extent)<a name="220" href="#220">220</a>     throws FileNotFoundException, IOException {<a name="221" href="#221">221</a>         <strong>return</strong> copyFile(src, dest, extent, DEFAULT_OVERWRITE);<a name="222" href="#222">222</a>     }<a name="223" href="#223">223</a> <a name="224" href="#224">224</a> 	<em>/**<em>*</em></em><a name="225" href="#225">225</a> <em>     * Copy up to extent bytes of the source file to the destination</em><a name="226" href="#226">226</a> <em>     *</em><a name="227" href="#227">227</a> <em>     * @param src</em><a name="228" href="#228">228</a> <em>     * @param dest</em><a name="229" href="#229">229</a> <em>     * @param extent Maximum number of bytes to copy</em><a name="230" href="#230">230</a> <em>	 * @param overwrite If target file already exits, and this parameter is</em><a name="231" href="#231">231</a> <em>     * true, overwrite target file (We do this by first deleting the target</em><a name="232" href="#232">232</a> <em>     * file before we begin the copy).</em><a name="233" href="#233">233</a> <em>	 * @return True if the extent was greater than actual bytes copied.</em><a name="234" href="#234">234</a> <em>     * @throws FileNotFoundException</em><a name="235" href="#235">235</a> <em>     * @throws IOException</em><a name="236" href="#236">236</a> <em>     */</em><a name="237" href="#237">237</a>     <strong>public</strong> <strong>static</strong> <strong>boolean</strong> copyFile(<strong>final</strong> File src, <strong>final</strong> File dest,<a name="238" href="#238">238</a>         <strong>long</strong> extent, <strong>final</strong> <strong>boolean</strong> overwrite)<a name="239" href="#239">239</a>     throws FileNotFoundException, IOException {<a name="240" href="#240">240</a>         <strong>boolean</strong> result = false;<a name="241" href="#241">241</a>         <strong>if</strong> (LOGGER.isLoggable(Level.FINE)) {<a name="242" href="#242">242</a>             LOGGER.fine(<span class="string">"Copying file "</span> + src + <span class="string">" to "</span> + dest + <span class="string">" extent "</span> +<a name="243" href="#243">243</a>                 extent + <span class="string">" exists "</span> + dest.exists());<a name="244" href="#244">244</a>         }<a name="245" href="#245">245</a>         <strong>if</strong> (dest.exists()) {<a name="246" href="#246">246</a>             <strong>if</strong> (overwrite) {<a name="247" href="#247">247</a>                 dest.delete();<a name="248" href="#248">248</a>                 LOGGER.finer(dest.getAbsolutePath() + <span class="string">" removed before copy."</span>);<a name="249" href="#249">249</a>             } <strong>else</strong> {<a name="250" href="#250">250</a>                 <em class="comment">// Already in place and we're not to overwrite.  Return.</em><a name="251" href="#251">251</a>                 <strong>return</strong> result;<a name="252" href="#252">252</a>             }<a name="253" href="#253">253</a>         }<a name="254" href="#254">254</a>         FileInputStream fis = <strong>null</strong>;<a name="255" href="#255">255</a>         FileOutputStream fos = <strong>null</strong>;<a name="256" href="#256">256</a>         FileChannel fcin = <strong>null</strong>;<a name="257" href="#257">257</a>         FileChannel fcout = <strong>null</strong>;<a name="258" href="#258">258</a>         <strong>try</strong> {<a name="259" href="#259">259</a>             <em class="comment">// Get channels</em><a name="260" href="#260">260</a>             fis = <strong>new</strong> FileInputStream(src);<a name="261" href="#261">261</a>             fos = <strong>new</strong> FileOutputStream(dest);<a name="262" href="#262">262</a>             fcin = fis.getChannel();<a name="263" href="#263">263</a>             fcout = fos.getChannel();<a name="264" href="#264">264</a>             <strong>if</strong> (extent &lt; 0) {<a name="265" href="#265">265</a>                 extent = fcin.size();<a name="266" href="#266">266</a>             }<a name="267" href="#267">267</a> <a name="268" href="#268">268</a>             <em class="comment">// Do the file copy</em><a name="269" href="#269">269</a>             <strong>long</strong> trans = fcin.transferTo(0, extent, fcout);<a name="270" href="#270">270</a>             <strong>if</strong> (trans &lt; extent) {<a name="271" href="#271">271</a>                 result = false;<a name="272" href="#272">272</a>             }<a name="273" href="#273">273</a>             result = <strong>true</strong>; <a name="274" href="#274">274</a>         } <strong>catch</strong> (IOException e) {<a name="275" href="#275">275</a>             <em class="comment">// Add more info to the exception. Preserve old stacktrace.</em><a name="276" href="#276">276</a>             <em class="comment">// We get 'Invalid argument' on some file copies. See</em><a name="277" href="#277">277</a>             <em class="comment">// http://intellij.net/forums/thread.jsp?forum=13&amp;thread=63027&amp;message=853123</em><a name="278" href="#278">278</a>             <em class="comment">// for related issue.</em><a name="279" href="#279">279</a>             String message = <span class="string">"Copying "</span> + src.getAbsolutePath() + <span class="string">" to "</span> +<a name="280" href="#280">280</a>                 dest.getAbsolutePath() + <span class="string">" with extent "</span> + extent +<a name="281" href="#281">281</a>                 <span class="string">" got IOE: "</span> + e.getMessage();<a name="282" href="#282">282</a>             <strong>if</strong> (e.getMessage().equals(<span class="string">"Invalid argument"</span>)) {<a name="283" href="#283">283</a>                 LOGGER.severe(<span class="string">"Failed copy, trying workaround: "</span> + message);<a name="284" href="#284">284</a>                 workaroundCopyFile(src, dest);<a name="285" href="#285">285</a>             } <strong>else</strong> {<a name="286" href="#286">286</a>                 IOException <strong>new</strong>E = <strong>new</strong> IOException(message);<a name="287" href="#287">287</a>                 newE.setStackTrace(e.getStackTrace());<a name="288" href="#288">288</a>                 <strong>throw</strong> newE;<a name="289" href="#289">289</a>             }<a name="290" href="#290">290</a>         } <strong>finally</strong> {<a name="291" href="#291">291</a>             <em class="comment">// finish up</em><a name="292" href="#292">292</a>             <strong>if</strong> (fcin != <strong>null</strong>) {<a name="293" href="#293">293</a>                 fcin.close();<a name="294" href="#294">294</a>             }<a name="295" href="#295">295</a>             <strong>if</strong> (fcout != <strong>null</strong>) {<a name="296" href="#296">296</a>                 fcout.close();<a name="297" href="#297">297</a>             }<a name="298" href="#298">298</a>             <strong>if</strong> (fis != <strong>null</strong>) {<a name="299" href="#299">299</a>                 fis.close();<a name="300" href="#300">300</a>             }<a name="301" href="#301">301</a>             <strong>if</strong> (fos != <strong>null</strong>) {<a name="302" href="#302">302</a>                 fos.close();<a name="303" href="#303">303</a>             }<a name="304" href="#304">304</a>         }<a name="305" href="#305">305</a>         <strong>return</strong> result;<a name="306" href="#306">306</a>     }<a name="307" href="#307">307</a>     <a name="308" href="#308">308</a>     <strong>protected</strong> <strong>static</strong> <strong>void</strong> workaroundCopyFile(<strong>final</strong> File src,<a name="309" href="#309">309</a>             <strong>final</strong> File dest)<a name="310" href="#310">310</a>     throws IOException {<a name="311" href="#311">311</a>         FileInputStream from = <strong>null</strong>;<a name="312" href="#312">312</a>         FileOutputStream to = <strong>null</strong>;<a name="313" href="#313">313</a>         <strong>try</strong> {<a name="314" href="#314">314</a>             from = <strong>new</strong> FileInputStream(src);<a name="315" href="#315">315</a>             to = <strong>new</strong> FileOutputStream(dest);<a name="316" href="#316">316</a>             byte[] buffer = <strong>new</strong> byte[4096];<a name="317" href="#317">317</a>             <strong>int</strong> bytesRead;<a name="318" href="#318">318</a>             <strong>while</strong> ((bytesRead = from.read(buffer)) != -1) {<a name="319" href="#319">319</a>                 to.write(buffer, 0, bytesRead);<a name="320" href="#320">320</a>             }<a name="321" href="#321">321</a>         } <strong>finally</strong> {<a name="322" href="#322">322</a>             <strong>if</strong> (from != <strong>null</strong>) {<a name="323" href="#323">323</a>                 <strong>try</strong> {<a name="324" href="#324">324</a>                     from.close();<a name="325" href="#325">325</a>                 } <strong>catch</strong> (IOException e) {<a name="326" href="#326">326</a>                     e.printStackTrace();<a name="327" href="#327">327</a>                 }<a name="328" href="#328">328</a>             }<a name="329" href="#329">329</a>             <strong>if</strong> (to != <strong>null</strong>) {<a name="330" href="#330">330</a>                 <strong>try</strong> {<a name="331" href="#331">331</a>                     to.close();<a name="332" href="#332">332</a>                 } <strong>catch</strong> (IOException e) {<a name="333" href="#333">333</a>                     e.printStackTrace();<a name="334" href="#334">334</a>                 }<a name="335" href="#335">335</a>             }<a name="336" href="#336">336</a>         }<a name="337" href="#337">337</a>     }<a name="338" href="#338">338</a> <a name="339" href="#339">339</a> 	<em>/**<em>* Deletes all files and subdirectories under dir.</em></em>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -