日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

动手又动脑

發(fā)布時(shí)間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 动手又动脑 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.編寫一個(gè)程序,指定一個(gè)文件夾,能自動(dòng)計(jì)算出其總?cè)萘俊?/p> import java.io.File; import java.io.IOException; public class FileEdit { double size=0.0; //計(jì)算文件或文件夾的大小,單位MB public double getSize(File file){ //判斷文件是否存在 if(file.exists()) { if(!file.isFile()) { //獲取文件大小 File[] fl = file.listFiles(); double ss=0; for(File f : fl) ss += getSize(f); return ss; }else { double ss = (double) file.length()/1024/1024; System.out.println(file.getName()+":"+ss+"MB"); return ss; } }else { System.out.println("文件或文件夾不存在,請檢查文件路徑是否正確!"); return 0.0; } } public static void main(String[] args) throws IOException{ FileEdit fd = new FileEdit(); double all = fd.getSize(new File("C:\\Users\\FuHeishi826\\Desktop\\壹青年")); System.out.println("All: "+all+"MB"); } }

2.編寫一個(gè)文件加解密程序,通過命令行完成加解密工作。

import java.io.File; import java.io.InputStream; import java.io.OutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; //編寫一個(gè)文件加解密程序,通過命令行完成加解密工作 public class FileCode {private static final int numOfEncAndDec=0x99;//加密解密密鑰private static int dataOfFile=0;//文件字節(jié)內(nèi)容public static void main(String[] args) {File srcFile=new File("E:\\新建文件夾\\poem.txt");//初始化文件File encFile=new File("E:\\新建文件夾\\poem1.txt"); //加密文件File decFile=new File("E:\\新建文件夾\\poem2.txt"); //解密文件try {//EncFile(srcFile,encFile); //加密操作//DecFile(encFile,decFile);//解密操作 EncFile(srcFile,decFile); //加密操作 DecFile(decFile,encFile);}catch(Exception e) {e.printStackTrace();}}private static void EncFile(File srcFile,File encFile)throws Exception{if(!srcFile.exists()) {System.out.println("source file not exixt");}if(!encFile.exists()) {System.out.println("encrypt file created");encFile.createNewFile();//若無加密文件,新建一個(gè)加密文件 }InputStream fis=new FileInputStream(srcFile);OutputStream fos=new FileOutputStream(encFile);while((dataOfFile=fis.read())>-1) {//當(dāng)讀到文件內(nèi)容時(shí)fos.write(dataOfFile^numOfEncAndDec);//將讀出的內(nèi)容加密后寫入 }fis.close();fos.flush();fos.close();}private static void DecFile(File encFile,File decFile)throws Exception{if(!encFile.exists()) {System.out.println("encrypt file not exixt");}if(!decFile.exists()) {System.out.println("decrypt file created");decFile.createNewFile();}InputStream fis=new FileInputStream(encFile);OutputStream fos=new FileOutputStream(decFile);while((dataOfFile=fis.read())>-1) {fos.write(dataOfFile^numOfEncAndDec);}fis.close();fos.flush();fos.close();}}

3.編寫一個(gè)文件分割工具,能把一個(gè)大文件分割成多個(gè)小的文件。并且能再次把它們合并起來得到完整的文件。

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;//編寫一個(gè)文件分割工具,能把一個(gè)大文件分割成多個(gè)小的文件。并且能再次把它們合并起來得到完整的文件 public class CutFile {public static void main(String[] args) {//調(diào)用cutFile()函數(shù) 傳人參數(shù)分別為 (原大文件,切割后存放的小文件的路徑,切割規(guī)定的內(nèi)存大小)cutFile("E:\\新建文件夾\\poem2.txt", "E:\\80747",1024 * 1024 * 20);}private static void cutFile(String src, String endsrc, int num) {FileInputStream fis = null;File file = null;try {fis = new FileInputStream(src);file = new File(src);//創(chuàng)建規(guī)定大小的byte數(shù)組byte[] b = new byte[num];int len = 0;//name為以后的小文件命名做準(zhǔn)備int name = 1;//遍歷將大文件讀入byte數(shù)組中,當(dāng)byte數(shù)組讀滿后寫入對應(yīng)的小文件中while ((len = fis.read(b)) != -1) {//分別找到原大文件的文件名和文件類型,為下面的小文件命名做準(zhǔn)備String name2 = file.getName();int lastIndexOf = name2.lastIndexOf(".");String substring = name2.substring(0, lastIndexOf);String substring2 = name2.substring(lastIndexOf, name2.length());FileOutputStream fos = new FileOutputStream(endsrc + "\\\\"+ substring + "-" + name + substring2);//將byte數(shù)組寫入對應(yīng)的小文件中fos.write(b, 0, len);//結(jié)束資源 fos.close();name++;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fis != null) {//結(jié)束資源 fis.close();}} catch (IOException e) {e.printStackTrace();}}} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/kt-xb/p/9985728.html

總結(jié)

以上是生活随笔為你收集整理的动手又动脑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。