在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理Java知识点的第二十五篇博客。
本篇博客介绍了Java的IO流的应用。
本系列博客所有Java代码都使用IntelliJ IDEA编译运行,版本为2022.1。所用JDK版本为JDK11。
目录
接下来几个案例,应用了IO流相关内容。
- import java.io.*;
- import java.util.ArrayList;
- public class buffertest4 {
- public static void main(String[] args) throws IOException {
- ArrayList
al = new ArrayList(); - al.add("Hello");
- al.add("Java");
- al.add("in");
- al.add("2022");
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("test3.txt"));
- for(String s:al){
- bw.write(s);
- bw.newLine();
- }
- bw.close();
- }
- }
程序创建了一个存放String的ArrayList集合,随后遍历集合,将每个内容写入文件。
程序无输出,test3.txt的内容是:
Hello
Java
in
2022
- import java.io.*;
- import java.util.ArrayList;
- public class buffertest5 {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new FileReader("D:\\Program Files\\Java Project\\JavaProject5\\test3.txt"));
- ArrayList
al = new ArrayList(); - String s;
- while((s = br.readLine()) != null){
- al.add(s);
- }
- br.close();
- for(String str :al){
- System.out.println(str);
- }
- }
- }
程序创建了一个存放String的ArrayList集合al,随后从文件获取内容,只要有内容就加入al集合。最后对al进行遍历输出。
程序的输出是:
Hello
Java
in
2022
- import java.io.*;
- import java.util.ArrayList;
- import java.util.Random;
- public class selectname {
- public static void main(String[] args) throws IOException {
- Random r = new Random();
- ArrayList
name = new ArrayList(); - BufferedReader br = new BufferedReader(new FileReader("test3.txt"));
- String s;
- while((s = br.readLine()) != null){
- name.add(s);
- }
- br.close();
-
- int length = name.size();
- int i = r.nextInt(length);
- System.out.println(name.get(i));
- }
- }
程序创建了一个ArrayList集合name,存储学生姓名。随后遍历文件test3.txt,将获取的姓名加入集合name。最后出一个随机数,范围是0到集合的大小,将此随机数为索引的元素输出。
提前在test3.txt中存放了十二个人名,依次是:
Pamela
Peter
Paine
Paula
Pilar
Philippe
Paul
Patty
Priscilla
Pablo
Polo
Paulette
下面是程序的一次运行结果:
Paul
- public class student1 {
- private String num;
- private String name;
- private int age;
-
- public student1(){
- }
- public student1(String num,String name,int age){
- this.num = num;
- this.name = name;
- this.age = age;
- }
-
- public void setnum(String num){
- this.num = num;
- }
- public String getnum(){
- return num;
- }
-
- public void setname(String name){
- this.name = name;
- }
- public String getname(){
- return name;
- }
-
- public void setage(int age){
- this.age = age;
- }
- public int getage(){
- return age;
- }
- }
这是学生类。
- import java.io.*;
- import java.util.ArrayList;
- public class studenttest1 {
- public static void main(String[] args) throws IOException {
- ArrayList
ar = new ArrayList(); - student1 s1 = new student1("2021EPAC","Pamela",21);
- student1 s2 = new student1("2003ATL","Peter",18);
- student1 s3 = new student1("1986EPAC","Paine",16);
- student1 s4 = new student1("2010ATL","Paula",22);
- student1 s5 = new student1("1987EPAC","Pilar",15);
- student1 s6 = new student1("2011ATL","Phillippe",21);
- ar.add(s1);
- ar.add(s2);
- ar.add(s3);
- ar.add(s4);
- ar.add(s5);
- ar.add(s6);
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("test1.txt"));
- for(student1 s:ar){
- StringBuilder sb = new StringBuilder();
- sb.append(s.getnum());
- sb.append(',');
- sb.append(s.getname());
- sb.append(',');
- sb.append(s.getage());
- String str = sb.toString();
- bw.write(str);
- bw.newLine();
- }
- bw.close();
- }
- }
程序创建了一个ArrayList结合ar,存放了六个student1类对象。随后遍历ar,将获取的学生类按照一定规则转换为字符串后写入文件。
程序无输出。test1.txt的内容是:
2021EPAC,Pamela,21
2003ATL,Peter,18
1986EPAC,Paine,16
2010ATL,Paula,22
1987EPAC,Pilar,15
2011ATL,Phillippe,21
- public class student2 {
- private String num;
- private String name;
- private int age;
-
- public student2(){
- }
- public student2(String num,String name,int age){
- this.num = num;
- this.name = name;
- this.age = age;
- }
-
- public void setnum(String num){
- this.num = num;
- }
- public String getnum(){
- return num;
- }
-
- public void setname(String name){
- this.name = name;
- }
- public String getname(){
- return name;
- }
-
- public void setage(int age){
- this.age = age;
- }
- public int getage(){
- return age;
- }
- }
这是学生类。
- import java.io.*;
- import java.util.ArrayList;
- public class studenttest2 {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new FileReader("test2.txt"));
- ArrayList
al = new ArrayList(); -
- String s;
- while((s = bf.readLine()) != null){
- String[] str = s.split(",");
- student2 s2 = new student2();
- s2.setnum(str[0]);
- s2.setname(str[1]);
- s2.setage(Integer.parseInt(str[2]));
- al.add(s2);
- }
-
- for(student2 s1:al){
- System.out.print("The num is " + s1.getnum());
- System.out.print(" and the name is " + s1.getname());
- System.out.println(" and the age is " + s1.getage());
- }
- bf.close();
- }
- }
程序首先以循环的方式从文件中获取字符串,然后每次得到一个字符串,就按照规则碎开,然后再构建成一个学生类,构建完毕就加入集合。最后遍历集合。
test2.txt中事先写了一些内容,内容如下:
2012EPAC,Paul,20
2012ATL,Patty,20
1983EPAC,Priscilla,19
2019ATL,Pablo,23
1984EPAC,Polo,18
2020ATL,Paulette,22
程序的输出是:
The num is 2012EPAC and the name is Paul and the age is 20
The num is 2012ATL and the name is Patty and the age is 20
The num is 1983EPAC and the name is Priscilla and the age is 19
The num is 2019ATL and the name is Pablo and the age is 23
The num is 1984EPAC and the name is Polo and the age is 18
The num is 2020ATL and the name is Paulette and the age is 22
- public class student3 {
- private String name;
- private int chinese;
- private int math;
- private int english;
-
- public student3(){}
- public student3(String name,int chinese,int math,int english){
- this.name = name;
- this.chinese = chinese;
- this.math = math;
- this.english = english;
- }
-
- public void setname(String name){
- this.name = name;
- }
- public String getname(){
- return name;
- }
-
- public void setchinese(int score){
- chinese = score;
- }
- public int getchinese(){
- return chinese;
- }
- public void setmath(int score){
- math = score;
- }
- public int getmath(){
- return math;
- }
-
- public void setenglish(int score){
- english = score;
- }
- public int getenglish(){
- return english;
- }
-
- public int gettotal(){
- return chinese + math + english;
- }
- }
成员变量chinese math english表示语文数学英语成绩。gettotal返回三科成绩综合。
- import java.io.*;
- import java.util.Comparator;
- import java.util.TreeSet;
- import java.util.Scanner;
- public class studenttest3 {
- public static void main(String[] args) throws IOException {
- Scanner sc = new Scanner(System.in);
- TreeSet
ts = new TreeSet( - new Comparator
(){ - public int compare(student3 s1,student3 s2){
- if(s1.gettotal() != s2.gettotal()){
- return s2.gettotal() - s1.gettotal();
- }else{
- return s2.getname().compareTo(s1.getname());
- }
- }
- }
- );
-
- int i;
- for(i = 0; i < 5;i += 1){
- student3 s = new student3();
- String name;
- System.out.println("Please enter the name: ");
- name = sc.nextLine();
-
- int chinese;
- System.out.println("Please enter the Chinese score: ");
- chinese = sc.nextInt();
- int math;
- System.out.println("Please enter the Math score: ");
- math = sc.nextInt();
-
- int english;
- System.out.println("Please enter the English score: ");
- english = sc.nextInt();
- sc.nextLine();
- s.setname(name);
- s.setchinese(chinese);
- s.setmath(math);
- s.setenglish(english);
- ts.add(s);
- }
-
- BufferedWriter bw = new BufferedWriter(new FileWriter("test3.txt"));
- for(student3 s3:ts){
- StringBuilder sb = new StringBuilder();
- sb.append(s3.getname());
- sb.append(',');
- sb.append(s3.getchinese());
- sb.append(',');
- sb.append(s3.getmath());
- sb.append(',');
- sb.append(s3.getenglish());
- sb.append(',');
- sb.append(s3.gettotal());
- String str = sb.toString();
- bw.write(str);
- bw.newLine();
- }
- bw.close();
- }
- }
程序以TreeSet集合ts存储学生类,按照成绩降序排列,成绩相同的按姓名降序排序。随后要求用户输入数据,建立了五个学生类加入ts。随个逐一拼接成字符串写入文件。
程序的执行语句很长,这里放执行完毕后文件的内容:
Seymour,90,96,84,270
Sandra,85,90,88,263
Sergio,84,81,88,253
Sam,83,95,75,253
Simon,80,75,84,239
- import java.io.*;
- public class filecopy1 {
- public static void main(String[] args) throws IOException {
- File f1 = new File("D:\\Program Files\\Java Project\\JavaProject1\\src");
- File f2 = new File("D:\\Program Files\\Java Project\\JavaProject6\\src");
- f2.mkdir();
-
- File[] f1list = f1.listFiles();
- for(File fi1:f1list){
- String s = fi1.getName();
- File fi1temp = new File(f1,s);
- File fi2temp = new File(f2,s);
- fi2temp.createNewFile();
-
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi1temp));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fi2temp));
-
- byte[] byts = new byte[1024];
- int len;
- while((len = bis.read(byts)) != -1){
- bos.write(byts,0,len);
- }
- bis.close();
- bos.close();
- }
- }
- }
程序创建了文件对象f1和f2,是两个目录,并创建f2目录。随后得到f1的子目录的集合f1list,然后遍历f1list,对于每个对象,都创建在新目录中对应的文件,然后进行内容的复制。本例只考虑单级目录的情况。程序无输出结果,进行了复制。
- import java.io.*;
- public class filecopy2 {
- public static void main(String[] args) throws IOException {
- File f1 = new File("D:\\Program Files\\Java Project\\JavaProject1");
- File f2 = new File("D:\\Program Files\\Java Project\\JavaProject6");
- f2.mkdir();
- copy(f1,f2);
- }
- public static void copy(File f1, File f2) throws IOException {
- if(f1.isDirectory() == true){
- File[] flist = f1.listFiles();
- for(File fi1:flist) {
- String s = fi1.getName();
- File fi1temp = new File(f1, s);
- File fi2temp = new File(f2, s);
- if (fi1temp.isDirectory() == true) {
- fi2temp.mkdir();
- copy(fi1temp, fi2temp);
- }else{
- fi2temp.createNewFile();
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fi1temp));
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fi2temp));
-
- byte[] byts = new byte[1024];
- int len;
- while ((len = bis.read(byts)) != -1) {
- bos.write(byts, 0, len);
- }
- bis.close();
- bos.close();
- }
- }
- }
-
- }
- }
程序创建了两个文件对象f1和f2,作为参数传递给copy方法。copy方法使用递归。首先判断是否为目录,不是目录就什么也不执行。是目录,就先得到子目录的集合flist,然后遍历此集合,对于每一个对象都创建新目录中对应对象。如果此对象是目录,就进行递归,否则进行内容的复制粘贴。
程序无输出,进行了复制。