11.15java课后作业
1,编写一个程序,指定一个文件夹,能自动计算出其总容量
package Account;
import java.io.File;
import java.util.ArrayList;
public class Size {
static long size=0;
private static ArrayList<String> filelist=new ArrayList<String>();
public static void main(String[] args) {
Size s=new Size();
String filePath="E:\\zxj.txt";//新建的文件夹zxj.txt
s.getFiles(filePath);
}
void getFiles(String filePath) {
File root=new File(filePath); File[] files=root.listFiles(); for(File file:files) { if(file.isDirectory()) { getFiles(file.getAbsolutePath()); filelist.add(file.getAbsolutePath());
} else { size+=file.getAbsolutePath().length(); } } System.out.println("大小是"+size);
}
}
运行结果:

2,编写一个文件加解密程序,通过命令行完成加解密工作
package Account;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
//编写一个文件加解密程序,通过命令行完成加解密工作
public class FileCode {
private static final int numOfEncAndDec=0x99;//加密解密密钥
private static int dataOfFile=0;//文件字节内容 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();//若无加密文件,新建一个加密文件
}
InputStream fis=new FileInputStream(srcFile);
OutputStream fos=new FileOutputStream(encFile); while((dataOfFile=fis.read())>-1) {//当读到文件内容时
fos.write(dataOfFile^numOfEncAndDec);//将读出的内容加密后写入
}
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,编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
package Account;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileSplit {
private static int nameend = 1;//记录将要在旧名字上加上的编号以生成新文件
public static void main(String[] args) {
File ifile = new File("E:\\zxj,txt");
File ofile = new File("E:\\zxj.txt");
FileSplit fs = new FileSplit();
fs.fileSplitMethod(ifile, ofile, 1024*1024*2);
}
public boolean fileSplitMethod(File ifile,File ofile,long filesize){
boolean success = false;
if(!ifile.exists() || !ifile.isFile() || !ofile.exists() || !ofile.isDirectory() || filesize <= 0)
return success;
int bufl = 1024; //缓冲字节数组的长度
if(filesize < bufl)
bufl = (int) filesize;
byte[] buf = new byte[bufl];//字节缓冲数组
int length = 0;//记录当前读取的字节数
int size = 0;//记录当前文件字节数
long readsize = 0; FileInputStream fis = null;
FileOutputStream fos = null; try {
fis = new FileInputStream(ifile);
fos = new FileOutputStream(getNewFile(ifile,ofile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while((length = fis.read(buf)) != -1){
fos.write(buf,0,length);
size += length;
readsize += length;//记录总已经读取字节数
if((size + bufl) > filesize && readsize < ifile.length()){//如果再读一个数组会大于最大单个文件大小,并且还有未读字节,则创建新的fos。
fos.flush();//将缓冲中的写入文件
fos.close();//关闭这个输出流
fos = new FileOutputStream(getNewFile(ifile,ofile));//重新创建新的输出流
size = 0;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return success;
}
public File getNewFile(File ifile,File ofile){
File file = null;
String oldname = ifile.getName();//得到旧名字,新名字将在旧名字上加上数字
String newfilepath = ofile.getPath() + "\\";//用于保存新的file路径
int endIndex = oldname.lastIndexOf(".");
if(endIndex > 0){
String oldnameend = oldname.substring(endIndex, oldname.length());//保存扩展名
oldname = oldname.substring(0, endIndex);//保存最后一个点左边的名字
newfilepath += oldname + nameend++ + oldnameend;//新路径
}else
newfilepath += oldname + nameend++;
file = new File(newfilepath);
return file;
}
}
---恢复内容结束---
11.15java课后作业的更多相关文章
- day 11课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/3 20:03# @Author : Endless-cloud# @Site : # @File : day 11 课 ...
- python基础一之课后作业:编写登录接口
1 # Author : Mamba 2 3 #python基础一之课后作业:编写登录接口 4 5 # 输入用户名密码 6 # 认证成功后显示欢迎信息 7 # 用户名3次输入错误后,退出程序 8 # ...
- day 10 课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/2 16:35# @Author : Endless-cloud# @Site : # @File : 课后作业.py# ...
- day 09 课后作业
# -*- coding: utf-8 -*-# @Time : 2018/12/28 14:25# @Author : Endless-cloud# @Site : # @File : 08 课后作 ...
- String字符串类课后作业
String动手动脑和课后作业 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 结果: 总结:在Java中,内容相同的字串常量(&quo ...
- JAVA第三周课后作业
JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...
- java课后作业
课后作业之字串加密: 设计思想: 1.输入要加密的英文子串str 2.定义num=str的字符串长度 3.将字符串转化为单个字符 4.每个字符+3,向后移3个 5.定义str1,将新得到的每个字符加到 ...
- 吴恩达课后作业学习1-week4-homework-two-hidden-layer -1
参考:https://blog.csdn.net/u013733326/article/details/79767169 希望大家直接到上面的网址去查看代码,下面是本人的笔记 两层神经网络,和吴恩达课 ...
- 吴恩达课后作业学习1-week4-homework-multi-hidden-layer -2
参考:https://blog.csdn.net/u013733326/article/details/79767169 希望大家直接到上面的网址去查看代码,下面是本人的笔记 实现多层神经网络 1.准 ...
随机推荐
- 安全测试8_Web安全实战1(DVWA部署)
1.渗透神器的打造(火狐浏览器的打造) Firebug HackBar(渗透插件) Tamper Data(抓包.截包.改包等功能) Proxy Switcher(代理设置) 2.PHP环境安装(ph ...
- LeetCode 12. Integer to RomanLeetCode
整数转罗马数字 first submission import math class Solution: def __init__(self): self.roman={1:'I',5:'V',10: ...
- Linux实用命令整理
说明 点击标题可进入详细讲解的章节 0. 基本命令 linux 基本命令整理 1. 压缩 解压 tar -zcvf a.tar.gz a #把a压缩成a.tar.gz tar -zxvf a.tar. ...
- 53.纯 CSS 创作一个文本淡入淡出的 loader 动画
原文地址:https://segmentfault.com/a/1190000015305861 感想:关于两侧动画不在同一水平线上的原因是因为设置其多余高,旋转180度呈现的. HTML code: ...
- SPARK快学大数据分析概要
Spark 是一个用来实现快速而通用的集群计算的平台.在速度方面,Spark 扩展了广泛使用的MapReduce 计算模型,而且高效地支持更多计算模式,包括交互式查询和流处理.在处理大规模数据集时,速 ...
- 04.给linux用户添加sudo权限
linux给用户添加sudo权限: 有时候,linux下面运行sudo命令,会提示类似: xxxis not in the sudoers file. This incident will be r ...
- leetcode1017
这道题目很巧妙,似乎是有些过于巧妙了,属于我未知领域的类型,把原作者的解决方案放这里.(看大家都在给差评,我也顺手给个差评吧--) 参考:https://leetcode.com/problems/c ...
- Swoole 结合TP5搭建文字直播平台
直播模块流程: 主进程服务:主进程同时开启两个服务 http服务,负责向前端传递页面,处理登录等事务 websocket服务,服务处理直播以及聊天室等事务 在项目根目录(框架代码同级目录)建立scri ...
- Windows下卸载Apache、Mysql
卸载Apache 1. 停止服务 2.以管理员身份打开命令环境 3. 删除Apache文件目录 卸载Mysql 一.在控制面板,卸载MySQL的所有组件控制面板——>所有控制面板项——>程 ...
- java中定义的四种类加载器
1,Bootstrap ClassLoader 启动类加载器2,ExtClassLoader 扩展类加载器3,AppClassLoader 系统类加载器4,ClassLoader 类加 ...