Java教程

Java实验(7)

本文主要是介绍Java实验(7),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前言:

henu大二上Java实验(7)


前言:

一:实验目的

二:实验内容


一:实验目的

1、理解字节流和字符流的含义,掌握IO流的分类;

2、掌握File类的使用;

3、掌握文件字节流FileInputStream、FileOutputStream和文件字符流FileReader、FileWriter的使用;

4、掌握字符缓冲流BufferedReader、BufferedWriter的使用;

5、了解转换流OutputStreamWriter和InputStreamReader的使用;

6、能够灵活使用IO流完成数据的处理。

二:实验内容

1、判断E盘指定目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称。

源代码:

import java.io.File;
import java.io.FilenameFilter;
public class S7_1 {
    public static void main(String[] args) throws Exception{
        File file=new File("F:/java.text");
        FilenameFilter fileter=new FilenameFilter() {
            @Override
            public boolean accept(File dir,String name) {
                File currFile=new File(dir,name);
                if(currFile.isFile()&& name.endsWith(".jpg")){
                    return true;
                }
                else{
                    return false;
                }
            }
        };
        if(file.exists()){
            String[] lists=file.list(fileter);
            for(String name:lists){
                System.out.println(name);
            }
        }
    }
}

测试数据与实验结果:

2、分别使用字节流和字节缓冲流的两种读取方式实现对图片文件的复制操作。

源代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class S7_2 {
    public static void main(String[] args) throws IOException{
        Stream1();//字节流
        Stream2();//缓冲流
    }
        public static void Stream1() throws IOException{
            FileInputStream fileInputStream=new FileInputStream("F:/java.text/2.jpg");
            FileOutputStream fileOutputStream=new FileOutputStream("F:/java.text/5.jpg");
            int num=0;
            while((num=fileInputStream.read())!=-1){
                fileOutputStream.write(num);
            }
            fileInputStream.close();
            fileOutputStream.close();
        }
        public static void Stream2() throws IOException{
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("F:/java.text/2.jpg"));
            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("F:/java.text/8.jpg"));
            int num = 0;
            while ((num = bufferedInputStream.read()) != -1) {
                bufferedOutputStream.write(num);
            }

            bufferedInputStream.close();
            bufferedOutputStream.close();
        }
 }

 测试数据和实验结果:字节流:5.jpg   缓冲流:8.jpg

字节流:

缓冲流:

 

3、编写一个程序,分别使用转换流、字符流和缓冲字符流拷贝一个文本文件。要求:

•    使用InputStreamReader、OutputStreamWriter类和FileReader、FileWriter类分别用两种方式(字符和字符数组)进行拷贝。

•    使用BufferedReader、BufferedWriter类的特殊方法进行拷贝。

源代码:

import java.io.*;
public class S7_3 {
    public static void main(String[] args)throws Exception {
        InputStream in = new FileInputStream("F:/java.text/新建文本档.txt");
        OutputStream out=new FileOutputStream("F:/java.text/新建文本档(2).txt");
        byte[] buffer=new byte[1024];
        int len;
        while((len=in.read(buffer))!=-1){
            out.write(buffer,0,len);
        }
        in.close();
        out.close();


        BufferedReader br=new BufferedReader(new FileReader("F:/java.text/新建文本档.txt"));
        BufferedWriter bw=new BufferedWriter(new FileWriter("F:/java.text/新建文本档(3).txt"));
        String str;
        while((str=br.readLine())!=null){
            bw.write(str);
            bw.newLine();
        }
        br.close();
        bw.close();
    }
}

测试数据和实验结果:

4、编程序实现下列功能:

•    向指定的txt文件中写入键盘输入的内容,然后再重新读取该文件的内容,显示到控制台上。

•    键盘录入5个学生信息(姓名, 成绩),按照成绩从高到低存入文本文件。

源代码:

import java.io.*;
import java.util.Scanner;
import java.util.*;
public class S7_4 {
    public static void main(String[] args)throws IOException {
        FileOutputStream fos;
        FileInputStream fis;
        InputStreamReader isr;
        Scanner sc = new Scanner(System.in);
        try{
            fos = new FileOutputStream("F:\\java.text\\a.txt");  //在e盘创建txt文件,用于写入内容
            System.out.print("please inter your String:");
            String s = sc.next();
            byte[] b = s.getBytes();
            fos.write(b);  //将字符串s的内容输入到txt文件中
            System.out.print("The string was written successfully,see it in a.txt.");
            fos.close();  //关闭文件输出流

            fis = new FileInputStream("F:\\java.text\\a.txt");
            isr = new InputStreamReader(fis,"utf-8");  //字符集utf-8,避免中文出现乱码情况
            int data = 0;
            System.out.println("The output is as follows:");
            while((data = isr.read()) != -1){
                System.out.print((char)data);  //读取txt文件的内容并将其输出到控制台
            }
            fis.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e1){
            e1.printStackTrace();
        }
        //
         class Student_20 {
            private String name;
            private int score;
            //无参构造方法
            public Student_20(){
                super();
            }
            //有参构造方法
            public Student_20(String name, int score){
                this.name = name;
                this.score = score;
            }

            public String getName() {
                return name;
            } //得到学生姓名

            public int getScore() {
                return score;
            } //得到学生分数

            public void setName(String name) {
                this.name = name;
            }

            public void setScore(int score) {
                this.score = score;
            }

            @Override
            public String toString() {
                return "Student{" +
                        "name='" + name + '\'' +
                        ", score=" + score +
                        '}';
            }
        }
        TreeSet<Student_20> set = new TreeSet<>(new Comparator<Student_20>() {
            @Override
            public int compare(Student_20 s1, Student_20 s2) {
                int cmp1 =   s2.getScore() - s1.getScore();
                int cmp2 = cmp1 == 0 ? s2.getName().compareTo(s1.getName()) : cmp1;
                return cmp2;
            }
        });
        // 输入学生信息

        System.out.print("请输入总学生个数:");
        int Student_number = sc.nextInt();
        for(int i=0;i<Student_number;i++){
            System.out.println("请输入第"+(i+1)+"个学生的信息:");
            System.out.print("name:");
            String name = sc.next();
            System.out.print("score:");
            int score = sc.nextInt();
            //创建学生对象并输入信息
            Student_20 s = new Student_20();
            s.setName(name);
            s.setScore(score);
            set.add(s);  //将学生信息添加到集合中
        }
        System.out.println("信息录入完成!");
        BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\java.text\\student.txt"));
        bw.write("由高到低排序后:");
        bw.write("\n");
        for(Student_20 s:set){
            StringBuilder Sb = new StringBuilder("name----"+s.getName()+",score----"+s.getScore());
            bw.write(Sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();

    }
}

测试数据与实验结果:

 

 

 

 

 

这篇关于Java实验(7)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!