学习java的第二十六天(file类)

本文介绍了Java中File类的基本操作,如文件和目录的存在性判断、属性获取,以及如何创建、删除文件。同时,通过实例展示了如何在项目中使用File类来管理食品订单信息,包括添加、查看和删除订单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件

什么是文件
        相关记录或放在一起的数据的集合
    文件一般存储在哪里
        硬盘、光盘、软盘
    Java程序如何访问文件属性
        JAVA API :java.io.File 类

File类的常用方法

    boolean exists( )    判断文件或目录是否存在
    boolean isFile( )    判断是否是文件
    boolean isDirectory( )    判断是否是目录
    String getPath( )    返回此对象表示的文件的相对路径名
    String getAbsolutePath( )    返回此对象表示的文件的绝对路径名
    String getName( )    返回此对象表示的文件或目录的名称
    boolean delete( )    删除此对象指定的文件或目录
    boolean createNewFile( )    创建名称的空文件,不创建文件夹
    long  length()    返回文件的长度,单位为字节, 如果文件不存在,则返回 0L

public static void main(String[] args) {
		//获取一个File类对象,这个对象指向计算机中F盘的demo.txt文件
		File file1 = new File("F:\\demo.txt");
		File file2 = new File("F:/test");
		
		System.out.println(file1.exists());
		System.out.println(file2.exists());
		
		System.out.println("---------");
		System.out.println(file1.isFile());
		System.out.println(file2.isFile());
		System.out.println(file2.isDirectory());
		System.out.println(file1.isDirectory());
		
		System.out.println("---------");
		
		File file3 = new File("F:/a.txt");
		//也就是说file指向的文件其所在的文件夹应该存在
		try {
			file3.createNewFile();
			System.out.println("文件创建成功");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println(file1.getPath());//F:\demo.txt
		System.out.println(file1.getAbsolutePath());//F:\demo.txt
		System.out.println(file1.getName());
		
		File file5 = new File("qqq.txt");
		try {
			file5.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(file5.getPath());//qqq.txt
		System.out.println(file5.getAbsolutePath());//E:\myeworks\Day026输入输出流\qqq.txt
		
		file2.delete();
		file3.delete();
		file5.delete();
		
		System.out.println(file1.length());//0
	}

 2.项目改写

public static void main(String[] args) {
		
		Food food1 = new Food(1, "黄焖鸡", 28.5, 0);
		Food food2 = new Food(2, "酸菜鱼", 25.5, 0);
		Food food3 = new Food(3, "佛跳墙", 55.5, 0);
		Food food4 = new Food(4, "地三鲜", 12.5, 0);
		ArrayList<Food> alFood = new ArrayList<Food>();
		alFood.add(food1);
		alFood.add(food2);
		alFood.add(food3);
		alFood.add(food4);
		// 查看餐袋原始信息
		View view1 = new View(1, "马力", 2, 13, "北大青鸟", food1.getDishMag(), 50, 0);
		View view2 = new View(2, "马力", 2, 13, "清华青鸟", food1.getDishMag(), 50, 0);
		ArrayList<View> alView = new ArrayList<View>();
		alView.add(view1);
		alView.add(view2);
		a: for (int i = alView.size();;) {
			// 判断餐袋中有多少菜品
			
			Scanner sc = new Scanner(System.in);
			System.out.println("欢迎使用“吃货联盟订餐系统”");
			System.out.println("*************************************");
			System.out.println("1.我要订餐");
			System.out.println("2.查看餐袋");
			System.out.println("3.签收订单");
			System.out.println("4.删除订单");
			System.out.println("5.我要点赞");
			System.out.println("6.退出系统");
			System.out.println("*************************************");
			int input = sc.nextInt();
			switch (input) {
			case 1:
				System.out.println("菜品序号\t\t菜品信息\t\t单价\t\t点赞数");
				for (int j = 1; j <= 4; j++) {
					System.out.println(alFood.get(j-1).toString());
				}

				// 向餐袋中输入数据
				int num=i+1;// 订单序号
				System.out.println("请输入您的姓名");// 用户姓名
				String name=sc.next();
				System.out.println("请输入你要点的菜品序号");
				int dishInput = sc.nextInt();
				String disNam=null;
				double disPri=0.0;
				switch (dishInput) {
				case 1:
					disNam =alFood.get(1).getDishMag();
					disPri = alFood.get(1).getPrice();
					break;
				case 2:
					disNam =alFood.get(2).getDishMag();
					disPri = alFood.get(2).getPrice();
					break;
				case 3:
					disNam =alFood.get(3).getDishMag();
					disPri = alFood.get(3).getPrice();
					break;
				case 4:
					disNam =alFood.get(4).getDishMag();
					disPri = alFood.get(4).getPrice();
					break;
				}
				// 输入餐品亻分数
				System.out.println("请输入您需要几份");
				int additionalInput = sc.nextInt();
				double disTot = disPri * additionalInput;
				if(disTot>50){
					disTot-=6;
				}
				// 输入送餐时间
				int timeInput=0;
				for (;;) {
					System.out.println("请输入送餐时间(12-20)");
					timeInput = sc.nextInt();

					if (timeInput >= 12 && timeInput <= 20) {
						break;
					} else {
						System.out.println("时间输入错误,请重新输入");
						continue;
					}
				}
				//
				System.out.println("请输入送餐地址");
				String addressInput = sc.next();
				System.out.println("订餐完成!");
				View view3 = new View(num, name, additionalInput, timeInput, addressInput, food1.getDishMag(), disTot, 0);
				alView.add(view3);
				System.out.println("输入0返回主页");
				int input_1 = sc.nextInt();
				if (input_1 == 0) {
					break;
				}
			case 2:
				System.out.println("***查看餐袋***");
				System.out
						.println("序号\t\t订餐人\t\t餐品信息\t\t送餐日期\t\t送餐地址\t\t总金额\t\t订单状态");
				for (int j = 0; j < alView.size(); j++) {
					System.out.println(alView.get(j).toString());
				}
				continue;
			case 3:
				c: for (;;) {
					System.out.println("***订单签收***");
					System.out
							.println("序号\t\t订餐人\t餐品信息\t\t送餐日期\t送餐地址\t\t\t总金额\t订单状态");
					for (int j = 0; j < alView.size(); j++) {
						System.out.println(alView.get(j).toString());
					}
					System.out.println("请输入您要签收的订单序号/输入0取消");
					int seqInput = sc.nextInt();// 要签收的订单序号
					if (seqInput == 0) {
						break c;
					}
					alView.get(seqInput-1).setState(1);
				}
				System.out.println("输入0返回主页");
				int input_3 = sc.nextInt();
				if (input_3 == 0) {
					break;
				}
			case 4:
				System.out
						.println("序号\t\t订餐人\t\t餐品信息\t\t送餐日期\t\t送餐地址\t\t总金额\t\t订单状态");
				for (int j = 0; j < alView.size(); j++) {
					System.out.println(alView.get(j).toString());
				}
				d: for (;;) {
					System.out.println("请输入您要删除的订单序号/输入0返回");
					int deleteInput = sc.nextInt();
					if (deleteInput == 0) {
						break d;
					}
					if (alView.get(deleteInput-1).getState() == 0) {
						System.out.println("不可以删除未完成订单");
						continue;
					}
					
					for (int j = deleteInput; j < alView.size(); j++) {
						alView.get(j).setSequence(alView.get(j).getSequence()-1);
					}
					alView.remove(deleteInput-1);
					System.out
							.println("序号\t\t订餐人\t餐品信息\t\t送餐日期\t送餐地址\t\t\t总金额\t订单状态");
					for (int j = 0; j < alView.size(); j++) {
						System.out.println(alView.get(j).toString());
					}
				}
				continue;
			case 5:
				e: for (;;) {
					System.out.println("菜品序号\t\t菜品信息\t\t单价\t\t点赞数");
					for (int j = 1; j <= 4; j++) {
						System.out.println(alFood.get(j-1).toString());
					}
					System.out.println("请输入您要点赞的菜品/输入0返回主页");
					int alikeInput = sc.nextInt();
					if (alikeInput == 0) {
						break e;
					}
					alFood.get(alikeInput-1).setAlike(alFood.get(alikeInput-1)
							.getAlike() + 1);
				}
				System.out.println("再按一次确认返回");
				int input_5 = sc.nextInt();
				if (input_5 == 0) {
					break;
				}
			case 6:
				System.out.println("*******谢谢使用,欢迎下次光临*********");
				break a;
			default:
				System.out.println("*******谢谢使用,欢迎下次光临*********");
				break a;
			}// switch
		}// for
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放在糖果旁的是我很想回忆的甜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值