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


    文件

    什么是文件
            相关记录或放在一起的数据的集合
        文件一般存储在哪里
            硬盘、光盘、软盘
        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

    1. public static void main(String[] args) {
    2. //获取一个File类对象,这个对象指向计算机中F盘的demo.txt文件
    3. File file1 = new File("F:\\demo.txt");
    4. File file2 = new File("F:/test");
    5. System.out.println(file1.exists());
    6. System.out.println(file2.exists());
    7. System.out.println("---------");
    8. System.out.println(file1.isFile());
    9. System.out.println(file2.isFile());
    10. System.out.println(file2.isDirectory());
    11. System.out.println(file1.isDirectory());
    12. System.out.println("---------");
    13. File file3 = new File("F:/a.txt");
    14. //也就是说file指向的文件其所在的文件夹应该存在
    15. try {
    16. file3.createNewFile();
    17. System.out.println("文件创建成功");
    18. } catch (IOException e) {
    19. e.printStackTrace();
    20. }
    21. System.out.println(file1.getPath());//F:\demo.txt
    22. System.out.println(file1.getAbsolutePath());//F:\demo.txt
    23. System.out.println(file1.getName());
    24. File file5 = new File("qqq.txt");
    25. try {
    26. file5.createNewFile();
    27. } catch (IOException e) {
    28. // TODO Auto-generated catch block
    29. e.printStackTrace();
    30. }
    31. System.out.println(file5.getPath());//qqq.txt
    32. System.out.println(file5.getAbsolutePath());//E:\myeworks\Day026输入输出流\qqq.txt
    33. file2.delete();
    34. file3.delete();
    35. file5.delete();
    36. System.out.println(file1.length());//0
    37. }

     2.项目改写

    1. public static void main(String[] args) {
    2. Food food1 = new Food(1, "黄焖鸡", 28.5, 0);
    3. Food food2 = new Food(2, "酸菜鱼", 25.5, 0);
    4. Food food3 = new Food(3, "佛跳墙", 55.5, 0);
    5. Food food4 = new Food(4, "地三鲜", 12.5, 0);
    6. ArrayList alFood = new ArrayList();
    7. alFood.add(food1);
    8. alFood.add(food2);
    9. alFood.add(food3);
    10. alFood.add(food4);
    11. // 查看餐袋原始信息
    12. View view1 = new View(1, "马力", 2, 13, "北大青鸟", food1.getDishMag(), 50, 0);
    13. View view2 = new View(2, "马力", 2, 13, "清华青鸟", food1.getDishMag(), 50, 0);
    14. ArrayList alView = new ArrayList();
    15. alView.add(view1);
    16. alView.add(view2);
    17. a: for (int i = alView.size();;) {
    18. // 判断餐袋中有多少菜品
    19. Scanner sc = new Scanner(System.in);
    20. System.out.println("欢迎使用“吃货联盟订餐系统”");
    21. System.out.println("*************************************");
    22. System.out.println("1.我要订餐");
    23. System.out.println("2.查看餐袋");
    24. System.out.println("3.签收订单");
    25. System.out.println("4.删除订单");
    26. System.out.println("5.我要点赞");
    27. System.out.println("6.退出系统");
    28. System.out.println("*************************************");
    29. int input = sc.nextInt();
    30. switch (input) {
    31. case 1:
    32. System.out.println("菜品序号\t\t菜品信息\t\t单价\t\t点赞数");
    33. for (int j = 1; j <= 4; j++) {
    34. System.out.println(alFood.get(j-1).toString());
    35. }
    36. // 向餐袋中输入数据
    37. int num=i+1;// 订单序号
    38. System.out.println("请输入您的姓名");// 用户姓名
    39. String name=sc.next();
    40. System.out.println("请输入你要点的菜品序号");
    41. int dishInput = sc.nextInt();
    42. String disNam=null;
    43. double disPri=0.0;
    44. switch (dishInput) {
    45. case 1:
    46. disNam =alFood.get(1).getDishMag();
    47. disPri = alFood.get(1).getPrice();
    48. break;
    49. case 2:
    50. disNam =alFood.get(2).getDishMag();
    51. disPri = alFood.get(2).getPrice();
    52. break;
    53. case 3:
    54. disNam =alFood.get(3).getDishMag();
    55. disPri = alFood.get(3).getPrice();
    56. break;
    57. case 4:
    58. disNam =alFood.get(4).getDishMag();
    59. disPri = alFood.get(4).getPrice();
    60. break;
    61. }
    62. // 输入餐品亻分数
    63. System.out.println("请输入您需要几份");
    64. int additionalInput = sc.nextInt();
    65. double disTot = disPri * additionalInput;
    66. if(disTot>50){
    67. disTot-=6;
    68. }
    69. // 输入送餐时间
    70. int timeInput=0;
    71. for (;;) {
    72. System.out.println("请输入送餐时间(12-20)");
    73. timeInput = sc.nextInt();
    74. if (timeInput >= 12 && timeInput <= 20) {
    75. break;
    76. } else {
    77. System.out.println("时间输入错误,请重新输入");
    78. continue;
    79. }
    80. }
    81. //
    82. System.out.println("请输入送餐地址");
    83. String addressInput = sc.next();
    84. System.out.println("订餐完成!");
    85. View view3 = new View(num, name, additionalInput, timeInput, addressInput, food1.getDishMag(), disTot, 0);
    86. alView.add(view3);
    87. System.out.println("输入0返回主页");
    88. int input_1 = sc.nextInt();
    89. if (input_1 == 0) {
    90. break;
    91. }
    92. case 2:
    93. System.out.println("***查看餐袋***");
    94. System.out
    95. .println("序号\t\t订餐人\t\t餐品信息\t\t送餐日期\t\t送餐地址\t\t总金额\t\t订单状态");
    96. for (int j = 0; j < alView.size(); j++) {
    97. System.out.println(alView.get(j).toString());
    98. }
    99. continue;
    100. case 3:
    101. c: for (;;) {
    102. System.out.println("***订单签收***");
    103. System.out
    104. .println("序号\t\t订餐人\t餐品信息\t\t送餐日期\t送餐地址\t\t\t总金额\t订单状态");
    105. for (int j = 0; j < alView.size(); j++) {
    106. System.out.println(alView.get(j).toString());
    107. }
    108. System.out.println("请输入您要签收的订单序号/输入0取消");
    109. int seqInput = sc.nextInt();// 要签收的订单序号
    110. if (seqInput == 0) {
    111. break c;
    112. }
    113. alView.get(seqInput-1).setState(1);
    114. }
    115. System.out.println("输入0返回主页");
    116. int input_3 = sc.nextInt();
    117. if (input_3 == 0) {
    118. break;
    119. }
    120. case 4:
    121. System.out
    122. .println("序号\t\t订餐人\t\t餐品信息\t\t送餐日期\t\t送餐地址\t\t总金额\t\t订单状态");
    123. for (int j = 0; j < alView.size(); j++) {
    124. System.out.println(alView.get(j).toString());
    125. }
    126. d: for (;;) {
    127. System.out.println("请输入您要删除的订单序号/输入0返回");
    128. int deleteInput = sc.nextInt();
    129. if (deleteInput == 0) {
    130. break d;
    131. }
    132. if (alView.get(deleteInput-1).getState() == 0) {
    133. System.out.println("不可以删除未完成订单");
    134. continue;
    135. }
    136. for (int j = deleteInput; j < alView.size(); j++) {
    137. alView.get(j).setSequence(alView.get(j).getSequence()-1);
    138. }
    139. alView.remove(deleteInput-1);
    140. System.out
    141. .println("序号\t\t订餐人\t餐品信息\t\t送餐日期\t送餐地址\t\t\t总金额\t订单状态");
    142. for (int j = 0; j < alView.size(); j++) {
    143. System.out.println(alView.get(j).toString());
    144. }
    145. }
    146. continue;
    147. case 5:
    148. e: for (;;) {
    149. System.out.println("菜品序号\t\t菜品信息\t\t单价\t\t点赞数");
    150. for (int j = 1; j <= 4; j++) {
    151. System.out.println(alFood.get(j-1).toString());
    152. }
    153. System.out.println("请输入您要点赞的菜品/输入0返回主页");
    154. int alikeInput = sc.nextInt();
    155. if (alikeInput == 0) {
    156. break e;
    157. }
    158. alFood.get(alikeInput-1).setAlike(alFood.get(alikeInput-1)
    159. .getAlike() + 1);
    160. }
    161. System.out.println("再按一次确认返回");
    162. int input_5 = sc.nextInt();
    163. if (input_5 == 0) {
    164. break;
    165. }
    166. case 6:
    167. System.out.println("*******谢谢使用,欢迎下次光临*********");
    168. break a;
    169. default:
    170. System.out.println("*******谢谢使用,欢迎下次光临*********");
    171. break a;
    172. }// switch
    173. }// for
    174. }
    175. }

  • 相关阅读:
    效率倍增啊,20个面向数据科学家的自动机器学习(AutoML)库来了
    【电路笔记】-节点电压分析和网状电流分析
    密集计算场景下的 JNI 实战
    Unknown module(s) in QT : datavisualization解决
    Hadoop-2.7.3完全分布式集群搭建(Centos7系统)
    CoCa论文笔记
    Spring-RabbitMQ 生产者消息确认案例分析
    基于HTML+CSS+JavaScript制作简单的大学生网页设计——关于我的家乡湖南网页设计主题
    webstorm/idea配置leetcode刷题
    年薪30W+的IC验证工程师究竟是做什么的?一文为你讲解清楚
  • 原文地址:https://blog.csdn.net/qihaojinqiuma/article/details/126365991