当前位置: 首页 > news >正文

太烂的牌也要打完只为自己也不是为了其他什么原因。

day17_io02

1.上课代码敲一遍

2.读取一个文件,这个文件中有随机的一些数字字符,统计这些数字有几个偶数,几个奇数,并且追加写入到该文件末尾。

例如:
a.txt文件:
3241256364789629090126581212515
奇数:xx个
偶数:xx个

package com.cskaoyan._17day;import java.io.*;
import java.util.ArrayList;public class Exercise {public static void main(String[] args) throws IOException {try {BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("a.txt"))));String lineTxt="3241256364789629090126581212515";int a = 0;ArrayList<String> oddnumber=new ArrayList<>();ArrayList<Object> evennumber=new ArrayList<>();while ((lineTxt=br.readLine())!=null){a++;if (a % 2 == 0) {oddnumber.add(lineTxt);} else {evennumber.add(lineTxt);}}br.close();}catch (Exception e){System.out.println("read error: " + e);}}
}

3.在一个磁盘的文件里保存26个英文小写字母(乱序),将他们读入内存中,进行排序,把排好顺序的数再重新追加写到磁盘的该文件中。

package com.cskaoyan._17day;
import java.io.*;
public  class Exercise02 {public static void main(String[] args) throws IOException {FileInputStream sc = new FileInputStream("a.txt");Reader reader = new InputStreamReader( sc );FileOutputStream s = new FileOutputStream("a.txt", true);Writer writer=new OutputStreamWriter( s );char[] chars = new char[1024];int len;while((len = reader.read(chars)) != -1){bubbleSort(chars,len);writer.write("\n");writer.write(chars,0,len);}reader.close();writer.close();}private static void bubbleSort(char arr[] , int len) {for (int i=0; i < len -1; i++) {for (int j=0; j < len -1; j++) {if (arr[j] > arr[j + 1]){char temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}
}

4.递归查找指定目录中(包括子目录中),所有的.java文件,并且,把所有这些找到的java文件,复制(是复制不是移动)到一个指定的目录下

package com.cskaoyan._17day;import java.io.*;
import java.util.ArrayList;public class Exercise03 {public static void main(String[] args) {File sc = new File("D:\\");File file = new File("D:\\ ");file.mkdir();String targetDir = "";eachFile(sc);//复制所有。java文件到目标文件夹for (int i=0; i < file.length(); i++) {FileInputStream input = null;FileOutputStream output = null;try{input = new FileInputStream(file.get(i));InputStream inbuffer = new BufferedInputStream(input);//目标文件由输出流自己创建output = new FileInputStream(targetDir + file.get(i).getName());OutputStream outbuffer=new BufferedOutputStream(output);//利用字节缓冲流复制文件byte[] b = new byte[1024];int len;while ((len = inbuffer.read(b)) != -1){outbuffer.write(b,0,len);}} catch (IOException e) {e.printStackTrace();}finally {closeQuietly(input);closeQuietly(output);}}}public static void closeQuietly(Closeable closeable){try{if (closeable != null){closeable.close();}} catch (IOException e){e.printStackTrace();}}public static ArrayList<File> files = new ArrayList<>();//遍历这个文件夹内的所有子目录和文件public static void eachFile(File file){try{File[] targetFile = file.listFiles();for (int i=0; i < targetFile.length; i++) {if (targetFile[i].isDirectory()){eachFile(targetFile[i]);}else{//找到所有的java文件并且放到集合中if (targetFile[i].getName().endsWith("java")){file.add(targetFile[i]);}}}} catch (Exception e){e.printStackTrace();}}
}

5.开始写阶段6(慢慢写不急着交,周末也可以写)

http://www.lryc.cn/news/207662.html

相关文章:

  • SDL窗口创建以及简单显示(1)
  • 【Html】交通灯问题
  • 用IntelliJ远程打断点调试
  • Spring-Bean的生命周期概述
  • SENet 学习
  • 目前和未来的缓存构建
  • aws亚马逊云免费账号代充值!!!什么是 AWS Lambda?
  • 《从零开始大模型开发与微调 :基于PyTorch与ChatGLM》简介
  • 【LeetCode】102. 二叉树的层序遍历
  • golang连接池检查连接失败时如何重试
  • 从JavaScript到Rust的三年时间小结
  • 【Python机器学习】零基础掌握VotingRegressor集成学习
  • 云计算模式的区域LIS系统源码,基于ASP.NET+JQuery、EasyUI+MVC技术架构开发
  • 面向对象设计原则之接口隔离原则
  • haproxy 负载均衡
  • 在el-dialog中使用tinymce 点击工具栏下拉框被遮挡
  • CloudQuery + StarRocks:打造高效、安全的数据库管控新模式
  • 各类统计模型R语言的详细使用教程-R语言的线性回归使用教程
  • 点云从入门到精通技术详解100篇-基于尺度统一的三维激光点云与高清影像配准
  • <蓝桥杯软件赛>零基础备赛20周--第2周
  • CMake多文件构建初步
  • 游戏研发的解决方案有哪些?
  • Bayes决策:身高与体重特征进行性别分类
  • 【考研数学】数学“背诵”手册 | 需要记忆且容易遗忘的知识点
  • HJ3 明明的随机数
  • 如何恢复u盘删除文件?2023最新分享四种方法恢复文件
  • 8.稳定性专题
  • 基于51单片机的四种波形信号发生器仿真设计(仿真+程序源码+设计说明书+讲解视频)
  • 不同网段的IP怎么互通
  • C#序列化与反序列化详解