Java初级黄金体验 其二

引言:让我们看一下你的C盘有多少个文件和文件夹

初级 Java IO : 第一个纪念碑

小程序大致功能

让我们看一下E盘有多少个文件

上代码

最近太多的作业

代码可以无限改进,君子回头十年不晚,先写软工去

package com.lxy.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* 创建目录
* @author lxy
*
* E:\aa\ff
*/
public class IOt2 {
//定义static属性
static String check_fuc;
static String op_name;
//定义输入函数
public static String shuru() {
Scanner scanner = new Scanner(System.in);
String key = scanner.nextLine();
return key;
}
public static void main(String[] args) throws IOException {
//基础输出
System.out.println("*按Q/q进行一次操作||按其他键退出");
String ss1 = shuru();
while(ss1.equals("Q") || ss1.equals("q")) {
//功能
System.out.println(" -----------------------------------------");
System.out.println("|创建新文件/文件夹--->1|删除文件/文件夹--->2|");
System.out.println(" -----------------------------------------");
System.out.println("|查找文件/文件夹----->3|打开文件/文件夹--->4|");
System.out.println(" -----------------------------------------");
check_fuc = shuru();
switch(check_fuc) {
case "1" :{
System.out.println(" ---------------------------------");
System.out.println("|创建新文件---->1|创建新文件夹---->2|");
System.out.println(" ---------------------------------");
String check_creat = shuru();
switch(check_creat) {
case "1": {
System.out.println("*请输入所要创建的名称:");
op_name = shuru();
Fifo ff = new Fifo(op_name);
ff.creatIt();
break;
}
case "2" :{
System.out.println("*请输入所要创建的名称:");
op_name = shuru();
Directory dr_tmp = new Directory(op_name);
dr_tmp.creatIt();
break;
}
}
break;
}
case "2" :{
System.out.println("*请输入所要删除的名称:");
op_name = shuru();
int tmp = checkName(op_name);
if(tmp == 3) {
System.out.println("文件不存在");
}else if(tmp == 2) {
Fifo ff_tmp = new Fifo(op_name);
ff_tmp.deleteIt();
}else {
Directory dr_tmp = new Directory(op_name);
dr_tmp.deleteIt();
}
break;
}
case "3" :{
System.out.println("*请输入所要查找的名称:");
op_name = shuru();
File ff = new File(op_name);
if(ff.exists()) {
System.out.println(ff.getName()+"存在!");
System.out.println("其位置在:"+ff.getAbsolutePath());
}
break;
}
case "4" :{
System.out.println("*请输入要打开的文件名称");
op_name = shuru();
int tmp = checkName(op_name);
if(tmp == 3) {
System.out.println("文件不存在");
}else if(tmp == 2) {
System.out.println(" ---------------------------------");
System.out.println("|查看内容---->1|增添/修改内容---->2|");
System.out.println(" ---------------------------------");
String cont = shuru();
switch(cont) {
case "1" :{
viewFifo(op_name);
break;
}
case "2" :{
System.out.println(" ----------------------------");
System.out.println("|增添内容---->1|修改内容---->2|");
System.out.println(" ----------------------------");
String mdf = shuru();
switch(mdf) {
case "1" :{
appendFifo(op_name);
break;
}
case "2": {
modifyFifo(op_name);
break;
}
}
break;
}
} }else {
printDirBasicMsg(op_name);
}
break;
}
}
//基础输出
System.out.println("按Q/q进行一次操作||按其他键退出");
ss1 = shuru();
}
System.out.println("你已退出!");
}
//封装文件夹查看功能函数
public static void printDirBasicMsg(String s) {
Directory Dr = new Directory(s);
System.out.println("该文件夹基本信息如下:");
System.out.println("*大小:"+Dr.getDirLength()+"字节");
System.out.println("*子文件夹数量:"+Dr.getDirChilddirNum()+"个");
System.out.println("*子文件数量:"+Dr.getDirChildfileNum()+"个");
System.out.println("*其子文件名称如下");
// Dr.printContent(Dr.fd, 0);
}
//封装文件查看函数及其修改内容的功能
public static void viewFifo(String s) throws FileNotFoundException {
Fifo fo = new Fifo(s);
fo.printContent();
}
//增加内容
public static void appendFifo(String s){
Fifo fo = new Fifo(s);
fo.appendIt();
}
//修改内容
public static void modifyFifo(String s){
Fifo fo = new Fifo(s);
fo.modifyIt();
}
//检查为文件夹/文件函数
public static int checkName(String s) {
int flag;
File f_check = new File(s);
if(f_check.isDirectory()) {
flag = 1;
} else if(f_check.isFile()) {
flag = 2;
}else {
flag = 3;
}
return flag;
}
}
//文件夹类
class Directory{
//
private String path;
//文件夹长度
private int dirLength;
//所有子文件夹的数量
private int dirChilddirNum;
//所有子文件的数量
private int dirChildfileNum;
public File fd;
//
public Directory(String s) {
this.path = s;
this.fd = new File(path);
this.dirLength = 0;
this.dirChilddirNum = 0;
this.dirChildfileNum = 0;
this.openFuc(fd);
}
//文件夹的大小以及子文件/文件夹的数量
private void openFuc(File fd) {
if(fd != null && fd.exists()) {
if(fd.isFile()) {
this.dirChildfileNum++;
dirLength += fd.length();
}else if(fd.isDirectory()) {
this.dirChilddirNum++;
if(fd.listFiles() != null)
for(File f1:fd.listFiles())
openFuc(f1);
}
}
}
//列出所有子文件夹和子文件
public void printContent(File f,int deep) {
for(int i = 0; i < deep; ++i) {
System.out.print("-");
}
System.out.println(f.getName());
if(f == null||!f.exists()) {
return ;
}else if(f.isDirectory()) {
if(f.listFiles() != null)
for(File f1:f.listFiles()) {
printContent(f1,deep+1);
}
}
}
//创建文件夹
public void creatIt() {
if(fd.mkdirs()) {
System.out.println("*创建文件夹成功!");
}else if(fd.exists()) {
System.out.println("*文件夹已存在请勿重复创建!");
}else {
System.out.println("*创建失败!");
}
}
//删除文件夹
public void deleteIt() {
if(fd.delete()) {
System.out.println("*文件夹"+fd.getName()+"已删除!");
}else {
System.out.println("*删除失败!");
} }
public int getDirLength() {
return dirLength;
} public int getDirChilddirNum() {
return dirChilddirNum;
} public int getDirChildfileNum() {
return dirChildfileNum;
}
}
class Fifo{
private String path;
File fdd;
FileReader fread;
FileWriter fwriter;
//构造器
Fifo(String s){
this.path = s;
fdd = new File(path);
}
//查看文件内容
public void printContent() throws FileNotFoundException {
System.out.println("*文件已打开,内容如下");
fread = new FileReader(fdd);
char[] a = new char[100];
try {
fread.read(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(char tmp : a) {
System.out.print(tmp);
}
try {
fread.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------------------");
}
//增添文件内容
public void appendIt() {
Scanner scanner_tmp = new Scanner(System.in);
System.out.println("*请在下方输入你要添加的内容");
String tmp = scanner_tmp.nextLine();
try {
fwriter = new FileWriter(this.fdd,true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fwriter.write(tmp);
fwriter.flush();
fwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("*文件内容添加成功");
}
//修改文件内容
public void modifyIt() {
Scanner scanner_tmp = new Scanner(System.in);
System.out.println("*请在下方输入你要修改的内容");
String tmp = scanner_tmp.nextLine();
try {
fwriter = new FileWriter(this.fdd,false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fwriter.write(tmp);
fwriter.flush();
fwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("*文件内容修改成功");
}
//创建文件
public void creatIt() throws IOException {
if(fdd.createNewFile()) {
System.out.println("*创建新文件成功!");
}else if(fdd.exists()){
System.out.println("*文件"+fdd.getName()+"已存在!"); }else {
System.out.println("*创建新文件失败!");
}
}
//删除文件
public void deleteIt() {
if(fdd.delete()) {
System.out.println("*文件"+fdd.getName()+"已删除!");
}else {
System.out.println("*删除失败!");
}
}
}

Java初级黄金体验 其二的更多相关文章

  1. Java初级黄金体验 其一

    关于作者 作者背景:公元2001年,意大利那不勒斯少年,为了达成梦想,在国庆的三天发现自己替身能力Java.从而加入了黑手党组织,并成为...... 作者目的:入门Java 为了Fighting Go ...

  2. JAVA 11初体验

    JAVA 11初体验 随着JAVA没半年发布一次新版本,前几天JAVA 11隆重登场.在JAVA 11中,增加了一些新的特性和api, 同时也删除了一些特性和api,还有一些性能和垃圾回收的改进. 作 ...

  3. Java 初级软件工程师 认证考试试卷1

    Java 初级软件工程师 认证考试试卷   笔试(A卷)   考试时间150分钟 总分 100分     姓    名_______________________ 身份证号_____________ ...

  4. 引用面试官文章 :如何准备Java初级和高级的技术面试

    本人最近几年一直在做java后端方面的技术面试官,而在最近两周,又密集了面试了一些java初级和高级开发的候选人,在面试过程中,我自认为比较慎重,遇到问题回答不好的候选人,我总会再三从不同方面提问,只 ...

  5. Java初级进阶中高级工程师必备技能

    很多人学了javase以为自己学的已经很OK了,但是其实javase里边有很多的知识点是你不知道的,不管你找的是哪里的javase的视频,大多数是不会讲这些东西,而这些东西你平时业务又不会主动去接触, ...

  6. 在Service Fabric上部署Java应用,体验一把微服务的自动切换

    虽然Service Fabric的Java支持版本还没有正式发布,但是Service Fabric本身的服务管理.部署.升级等功能是非常好用的,那么Java的开发者可以如何利用上Service Fab ...

  7. 【阿里云产品公测】开放搜索服务 opensearch java jdk 应用体验之 机器人聊天

    作者:阿里云用户啊里新人 需求:基本实现智能聊天! 最近在开发一款机器人,希望实现基本的聊天功能,词库是有的,但是如果是做完全匹配这个出来的效果很悲催,   比如词库:你好,回答是:哈哈,很好啊. 如 ...

  8. Java Builder模式 体验(二)

       在上篇文章中,对Java Builder模式的使用体验主要是从Builder对构造器改造方面的优秀特性来说的,感觉并没有从Java Builder模式本身的功能和作用去写,因此决定再从Build ...

  9. 阿里巴巴Java开发规约插件-体验

    插件有哪些功能? 阿里技术公众号于今年的2月9日首次公布<阿里巴巴Java开发规约>,瞬间引起全民代码规范的热潮,上月底又发布了PDF的终极版,大家踊跃留言,期待配套的静态扫描工具开放出来 ...

随机推荐

  1. 在SQL查询结果中添加自增列的两种方法

    解决办法<一>:如果想查询出这个表的信息,并添加一列连续自增的ID,可用如下查询语句: SELECT Row_Number() over ( order by getdate() ) as ...

  2. angularcli 第三篇(数据循环*ngFor、条件判断*ngIf)

    1.数据循环 *ngFor (1)普通循环 <ul> <li *ngFor = "let item of list" > {{ item }} </l ...

  3. 蓝桥杯-入门训练 :Fibonacci数列

    问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1.当n比较大时,Fn也非常大,现在我们想知道,Fn除以10007的余数是多少. 输入格式 输入包含一个整数n. ...

  4. linux系统编程面试题

    说明:所有题目均摘录于网络以及我所见过的面试题目,欢迎补充! 无特殊说明情况下,下面所有题s目都是linux下的32位C程序. 1.堆和栈有什么区别? (1)栈由操作系统分配和释放:堆则是程序员手动去 ...

  5. [MySQL] 行级锁SELECT ... LOCK IN SHARE MODE 和 SELECT ... FOR UPDATE

    一.译文 翻译来自官方文档:Locking Reads If you query data and then insert or update related data within the same ...

  6. 数据库事务和锁(三)——INNODB_LOCKS, INNODB_LOCK_WAITS, INNODB_TRX表的简单介绍

    INNODB_LOCKS, INNODB_LOCK_WAITS, INNODB_TRX是MYSQL中事务和锁相关的表.通常我们遇到事务超时或锁相关问题时,直接运行下面SQL语句即可进行简单检查: -- ...

  7. Windows下通过SSH无密码连接Linux服务器

    一.配置环境 1.本机系统:Windows 10 Pro(64位) 2.服务器:CentOS 6.10(64位) 3.SSH连接软件:SecureCRT 二.配置SSH无密码登录步骤 1.在个人PC机 ...

  8. 163data.com.cn data

    163data.com.cn是什么?终于搞清楚了...   查看文章     163data.com.cn是什么?终于搞清楚了... 2008-05-31 00:41 一场误会,真TN的无聊的吓人从日 ...

  9. python使用二分法实现在一个有序列表中查找指定的元素

    二分法是一种快速查找的方法,时间复杂度低,逻辑简单易懂,总的来说就是不断的除以2除以2... 例如需要查找有序list里面的某个关键字key的位置,那么首先确认list的中位数mid,下面分为三种情况 ...

  10. 《Exceptioning团队》第四次作业:项目需求调研与分析

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.探索团队软件项目需求获取技巧与方法2.学会 ...