教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

Java 基于命令行文件管理器

来源:网络收集 时间:2026-09-09
导读: package filemanager; import java.io.*; import java.security.*; import java.util.*; import javax.crypto.*; public class FileManager { public static void main(String[] args) throws Exception { MainMenu mainmenu=new MainMenu(); mainmenu.Start

package filemanager;

import java.io.*;

import java.security.*;

import java.util.*;

import javax.crypto.*;

public class FileManager {

public static void main(String[] args) throws Exception {

MainMenu mainmenu=new MainMenu();

mainmenu.StartMenu();

}

}

class CreateFile{

CreateFile() throws IOException{

Scanner s=new Scanner(System.in);

System.out.println("Where to create?(D:/aaa/...)");

String CurrentDirectory=s.next();

File file=new File(CurrentDirectory);

if(file.exists()){

System.out.println("Enter the name:");

String Name=s.next();

File f = new File(CurrentDirectory+File.separator+Name) ;// 实例化File类的对象

f.mkdir() ;

System.out.println("success to create!");

}else System.out.println("the directory isn't exist!");

}

}

class DeleteFile{

File path;

public DeleteFile(File path){

this.path=path;

}

public void Delete(File path){

File filelist[]=path.listFiles();

int listlen=filelist.length;

for(int i=0;i<listlen;i++){

if(filelist[i].isDirectory()){

Delete(filelist[i]);

}

else {

filelist[i].delete();

}

}

path.delete();//删除当前目录

}

void Success(){

System.out.println("Success to delete!!:");

}

}

class EnterDirectory{

EnterDirectory(String path) throws IOException{

Runtime.getRuntime().exec("explorer.exe " + path);

}

}

class ListDirectory{

ListDirectory(String path){

// String luj=null;//路径

//如果dir不以文件分隔符结尾,自动添加文件分隔符

if(!path.endsWith(File.separator)){

path = path + File.separator;

}

File dirFile = new File(path);

//如果dir对应的文件不存在,或者不是一个文件夹则退出

if(!dirFile.exists() || (!dirFile.isDirectory())){

System.out.println("List failed!cannot find the directory:"+path);

}

//list方法返回该目录下的所有文件(包括目录)的文件名,文件名不含路径信息

System.out.println(path+" contains below directorise or files");

String[] files = dirFile.list();

for(int i = 0; i < files.length; i++){

System.out.println(files[i]);

}

}

}

class CopyFile{

CopyFile(String source, String dest) throws IOException {

File in = new File(source);

File out = new File(dest);

FileInputStream inFile = new FileInputStream(in);

FileOutputStream outFile = new FileOutputStream(out);

byte[] buffer = new byte[1024];

int i = 0;

while ((i = inFile.read(buffer)) != -1) {

outFile.write(buffer, 0, i);

}//end while

inFile.close();

outFile.close();

System.out.print("Success to copy!");

}

}

class CopyDirectory{

public void copyFolder(String oldPath, String newPath) {

try {

(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹

File a=new File(oldPath);

String[] file=a.list();

File temp=null;

for (int i = 0; i < file.length; i++) {

if(oldPath.endsWith(File.separator)){

temp=new File(oldPath+file[i]);

}

else{

temp=new File(oldPath+File.separator+file[i]);

}

if(temp.isFile()){

FileInputStream input = new FileInputStream(temp);

FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());

byte[] b = new byte[1024 * 5];

int len;

while ( (len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

output.close();

input.close();

}

if(temp.isDirectory()){//如果是子文件夹

copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);

}

}

}

catch (Exception e) {

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

}

}

public void Success(){

System.out.println("Success to copy the directory!");

}

}

class TestDES {

Key key;

public TestDES(){}

public TestDES(String str) {

getKey(str);//生成密匙

}

/**

* 根据参数生成KEY */ public void getKey(String strKey) { try { KeyGenerator _generator = KeyGenerator.getInstance("DES"); _generator.init(new SecureRandom(strKey.getBytes())); this.key = _generator.generateKey(); _generator = null; } catch (Exception e) { throw new RuntimeException("Error initializing SqlMap class. Cause: " + e); } } /* * 文件file进行加密并保存目标文件destFile中 * * @param file 要加密的文件 如c:/test/srcFile.txt * @param destFile 加密后存放的文件名 如c:/加密后文件.txt */

public void encrypt(String file, String destFile) throws Exception { //【具体用法见主函数】

Cipher cipher = Cipher.getInstance("DES");

// cipher.init(Cipher.ENCRYPT_MODE, getKey());

cipher.init(Cipher.ENCRYPT_MODE, this.key);

InputStream is = new FileInputStream(file);

OutputStream out = new FileOutputStream(destFile);

CipherInputStream cis = new CipherInputStream(is, cipher);

byte[] buffer = new byte[1024];

int r;

while ((r = cis.read(buffer)) > 0) {

out.write(buffer, 0, r);

}

cis.close();

is.close(); out.close(); } /* * 文件采用DES算法解密文件 * * @param file 已加密的文件 如c:/加密后文件.txt * * @param destFile * 解密后存放的文件名 如c:/ test/解密后文件.txt */ public void decrypt(String file, String dest) throws Exception { Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, this.key); …… 此处隐藏:5607字,全部文档内容请下载后查看。喜欢就下载吧 ……

Java 基于命令行文件管理器.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/1933815.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)