java 找出姓“张”的学生,将他们的信息写入第二个文件

一、需求

编写一个Java源程序,其中包含:

编写一个包含主方法main的公共类(访问权限为public的类),主方法main中完成的任务是:

(1)从键盘上输入两个文件的路径名及其文件名。

(2)从键盘上输入若干学生的信息(姓名,数学成绩,计算机成绩,各项之间用空格隔开),以finish结束,将这些学生的信息写入第一个文件。

(3)将第一个文件作为输入流的源,找出姓“张”的学生,将他们的信息写入第二个文件。

提示:将输入的每一行学生的信息作为一个字符串来处理。


二、使用类

1、File类
  • 构造方法
2、Scanner类
  • String nextLine()
    读入一行数据(可接收回车、空格)

  • String next()
    查找并返回来自此扫描器的下一个完整标记。(不可接收回车、空格)

3、FileOutputStream类
  • 构造方法 FileOutputStream(File file)
    创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

  • 构造方法 FileOutputStream(File file, boolean append)
    创建一个向指定 File 对象表示的文件中写入数据的文件输出流。

  • void write(byte[] b)
    将 b.length 个字节从指定 byte 数组写入此文件输出流中。

  • void write(byte[] b, int off, int len)
    将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。

  • void write(int b)
    将指定字节写入此文件输出流。

  • void close()
    关闭此文件输入流并释放与此流有关的所有系统资源。

4、String类
  • boolean contains(CharSequence s)
    当且仅当此字符串包含指定的 char 值序列时,返回 true。

  • boolean equals(Object anObject)
    将此字符串与指定的对象比较。

5、Iterator 接口
  • boolean hasNext()
    如果仍有元素可以迭代,则返回 true。

  • E next()
    返回迭代的下一个元素。

6、Collection 接口
  • ArrayList类

  • LinkedLi类

7、缓冲流
  • BufferedReader类

  • BufferedWriter类


三、代码

public class Main
{
    public static void main(String[] args) throws IOException 
    {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入第一个文件的路径与文件名:");
        File file1 = new File(in.next());
        System.out.println("请输入第二个文件的路径与文件名:");
        File file2 = new File(in.next());
        write(file1);
        read_write(file1,file2);
    }
    public static void write(File file1) throws IOException
    {
        LinkedList<String> student = new LinkedList<String>();
        Scanner in = new Scanner(System.in);
        System.out.println("第一个文件存放学生的姓名,数学成绩和计算机成绩,各项之间用空格隔开。");
        System.out.println("\n请向第一个文件输入内容,以finish结束:");
        while(true)
        {
            String content = in.nextLine();
            if(!"finish".equals(content))
            {
                student.add(content);
            }
            else
            {
                break;
            }
        }
        Iterator<String> it = student.iterator();
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        while(it.hasNext())
        {
            byte[] b = it.next().getBytes();
            fileOutputStream.write(b);      
            fileOutputStream.write((int)'\n');
        }
        fileOutputStream.close();
    }
    public static void read_write(File file1,File file2) throws IOException
    {
        ArrayList<String> list = new ArrayList<String>();
        FileReader fileReader = new FileReader(file1);
        FileWriter fileWriter = new FileWriter(file2);
        BufferedReader bufferedReader = new BufferedReader(fileReader);     
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        String content = new String();
        while((content = bufferedReader.readLine())!=null)
        {
            if(content.startsWith("张"))
            {
                list.add(content);
            }
        }
        Iterator<String> it = list.iterator();
        while(it.hasNext())
        {
            bufferedWriter.write(it.next());
            bufferedWriter.write('\n');
        }
        bufferedWriter.close();
        bufferedReader.close();
        System.out.println("\n第一个文件中姓“张”的学生的信息已经存放到第二个文件,请到其存放路径查阅!");
    }
}

四、运行截图

这里写图片描述

查看文件:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值