编写一个程序,指定一个文件夹,能自动计算出其总容量
package Chengji;
import java.io.File;
import java.util.ArrayList;
public class Rongliang {
static long rl=0;
private static ArrayList<String> filelength=new ArrayList<String>();
public void getFiles(String filePath)
{
File root=new File(filePath);
File[] files=root.listFiles();
for(File file:files)
{
if(file.isDirectory())
{
getFiles(file.getAbsolutePath());
filelength.add(file.getAbsolutePath());
}
else
{
rl+=file.getAbsolutePath().length();
}
}
System.out.println("大小为:"+rl);
}
public static void main(String[] args)
{
Rongliang s=new Rongliang();
String filePath="C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684";
s.getFiles(filePath);
}
import java.util.ArrayList;
public class Rongliang {
static long rl=0;
private static ArrayList<String> filelength=new ArrayList<String>();
public void getFiles(String filePath)
{
File root=new File(filePath);
File[] files=root.listFiles();
for(File file:files)
{
if(file.isDirectory())
{
getFiles(file.getAbsolutePath());
filelength.add(file.getAbsolutePath());
}
else
{
rl+=file.getAbsolutePath().length();
}
}
System.out.println("大小为:"+rl);
}
public static void main(String[] args)
{
Rongliang s=new Rongliang();
String filePath="C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684";
s.getFiles(filePath);
}
}
编写一个文件加解密程序,通过命令行完成加解密工作;
package Chengji;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Secret {
private static final int n=0x99;
private static int dt=0;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Secret {
private static final int n=0x99;
private static int dt=0;
public static void main(String[] args) {
File srcFile=new File("a.txt");
File encFile=new File("b.txt");
File decFile=new File("c.txt");
try {
EncFile(srcFile,encFile);
DecFile(encFile,decFile);
}catch(Exception e) {
e.printStackTrace();
}
}
private static void EncFile(File srcFile,File encFile)throws Exception{
if(!srcFile.exists()) {
System.out.println("不存在");
}
if(!encFile.exists()) {
System.out.println("已被创建");
encFile.createNewFile();
}
InputStream is=new FileInputStream(srcFile);
OutputStream os=new FileOutputStream(encFile);
while((dt=is.read())>-1) {//当读到文件内容时
os.write(dt^n);//将读出的内容加密后写入
}
is.close();
os.flush();
os.close();
}
private static void DecFile(File encFile,File decFile)throws Exception{
if(!encFile.exists()) {
System.out.println("不存在");
}
if(!decFile.exists()) {
System.out.println("已被创建");
decFile.createNewFile();
}
InputStream fis=new FileInputStream(encFile);
OutputStream fos=new FileOutputStream(decFile);
while((dt=fis.read())>-1) {
fos.write(dt^n);
}
fis.close();
fos.flush();
fos.close();
}
File srcFile=new File("a.txt");
File encFile=new File("b.txt");
File decFile=new File("c.txt");
try {
EncFile(srcFile,encFile);
DecFile(encFile,decFile);
}catch(Exception e) {
e.printStackTrace();
}
}
private static void EncFile(File srcFile,File encFile)throws Exception{
if(!srcFile.exists()) {
System.out.println("不存在");
}
if(!encFile.exists()) {
System.out.println("已被创建");
encFile.createNewFile();
}
InputStream is=new FileInputStream(srcFile);
OutputStream os=new FileOutputStream(encFile);
while((dt=is.read())>-1) {//当读到文件内容时
os.write(dt^n);//将读出的内容加密后写入
}
is.close();
os.flush();
os.close();
}
private static void DecFile(File encFile,File decFile)throws Exception{
if(!encFile.exists()) {
System.out.println("不存在");
}
if(!decFile.exists()) {
System.out.println("已被创建");
decFile.createNewFile();
}
InputStream fis=new FileInputStream(encFile);
OutputStream fos=new FileOutputStream(decFile);
while((dt=fis.read())>-1) {
fos.write(dt^n);
}
fis.close();
fos.flush();
fos.close();
}
}
编写一个文件分割工具,能把一个大文件分割成多个小的文件。并且能再次把它们合并起来得到完整的文件。
package Chengji;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Fenge {
private static void cutFile(String src, String endsrc, int num) {
FileInputStream fis = null;
File f = null;
try {
fis = new FileInputStream(src);
f = new File(src);
byte[] b = new byte[num];
int len = 0;
int name1 = 1;
while ((len = fis.read(b)) != -1) {
String name2 = f.getName();
int lastIndexOf = name2.lastIndexOf(".");
String substring = name2.substring(0, lastIndexOf);
String substring2 = name2.substring(lastIndexOf, name2.length());
FileOutputStream fos = new FileOutputStream(endsrc + "\\"+ substring + "-" + name1 + substring2);
fos.write(b, 0, len);
fos.close();
name1++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void togetherFile(String src, String endsrc){
FileOutputStream fos = null;
File file1 = null;
File file2 = null;
try {
file1 = new File(endsrc);
file2 = new File(src);
fos = new FileOutputStream(endsrc);
for(File file : file2.listFiles()){
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Fenge {
private static void cutFile(String src, String endsrc, int num) {
FileInputStream fis = null;
File f = null;
try {
fis = new FileInputStream(src);
f = new File(src);
byte[] b = new byte[num];
int len = 0;
int name1 = 1;
while ((len = fis.read(b)) != -1) {
String name2 = f.getName();
int lastIndexOf = name2.lastIndexOf(".");
String substring = name2.substring(0, lastIndexOf);
String substring2 = name2.substring(lastIndexOf, name2.length());
FileOutputStream fos = new FileOutputStream(endsrc + "\\"+ substring + "-" + name1 + substring2);
fos.write(b, 0, len);
fos.close();
name1++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void togetherFile(String src, String endsrc){
FileOutputStream fos = null;
File file1 = null;
File file2 = null;
try {
file1 = new File(endsrc);
file2 = new File(src);
fos = new FileOutputStream(endsrc);
for(File file : file2.listFiles()){
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
byte[] b = new byte[1024*1024];
int len = 0;
while((len = fis.read(b)) != -1){
fos.write(b, 0, len);
}
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
cutFile("C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS\Harry Potter and the Sorcerer's Stone.txt", "C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS",10 * 10 * 20);
togetherFile("C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS","C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS\Harry Potter and the Sorcerer's Stone.txt");
}
}
int len = 0;
while((len = fis.read(b)) != -1){
fos.write(b, 0, len);
}
fis.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos != null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
cutFile("C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS\Harry Potter and the Sorcerer's Stone.txt", "C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS",10 * 10 * 20);
togetherFile("C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS","C:\Users\北方\Desktop\1-信1807-5 郝子嘉 20183684\HPATSS\Harry Potter and the Sorcerer's Stone.txt");
}
}