Day22练习

A:简答题

1、请把我们讲解过的所有类中的方法在API中找到,并使用自己的话进行描述

PrintWriter
PrintWriter(OutputStream out, boolean autoFlush) 
创建从现有的OutputStream新PrintWriter实现自动刷新和换行
PrintWriter(Writer out, boolean autoFlush) 
创建一个新的PrintWriter 实现自动刷新和换行
public void print(String s) 
打印一个字符串
public void println(String x)
打印一个字符串 然后终止行
 
ObjectOutputStream
public final void writeObject(Object obj)
写入指定的对象的对象

ObjectInputStream
public final Object readObject()
从对象输入流对象

Properties
public Object setProperty(String key, String value) 
调用方法 put Hashtable
public String getProperty(String key)	
在这个属性列表中搜索指定的键的属性
public Set<String> stringPropertyNames()
在这个属性列表中返回一组键,其中键和它的对应值是字符串,包括在默认属性列表中的不同键,如果同一个名称的一个键没有从主要属性列表中找到
public void load(Reader reader) 
从一个简单的行导向格式中读取输入字符流中的属性列表(键和元素对)
public void load(InputStream inStream) 
从输入字节流中读取属性列表(键和元素对)
public void store(OutputStream out,String comments) 
写这个属性列表(关键元素对)在这Properties表格式来合适的输出流加载到一个Properties表使用load(InputStream)方法。
public void store(Writer writer,String comments) 
写这个属性列表(关键元素对在这 Properties表格式来合适的输出字符流使用load(Reader)方法

2、自己整理一份总结集合、IO部分知识点文档

Properties作为Map集合的使用
Properties父类是Hashtable
Properties不能指定泛型
属性列表中每个键及其对应值都是一个字符串

3、请简述打印流(PrintStream、PrintWriter)的特点?

字节打印流(PrintStream)
字符打印流(PrintWriter
1.都属于输出流 分别针对输出字节和字符
2.都提供了重载的print()、println()方法用于多种数据类型的输出
3.都不会抛出异常 用户通过检测错误状态获取错误信息
4.都有自动flush功能。

B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。

1、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因

class Test {
    public static void main(String[] args) throws IOException {
        write();
        write();
        read();
    }

    public static void write() throws FileNotFoundException {
        PrintWriter pw = new PrintWriter(new FileOutputStream("d:\\file.txt", true));
        pw.println("你好");
        pw.close();
    }

    public static void read() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("d:\\file.txt"));
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}
/*
在d:创建文件file.txt 并写入你好 你好(两行输出)
在屏幕输出:
你好
你好
*/

2、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因

class Person implements Serializable {
    private String name;
    private transient int age;

    public Person(){}
    public Person (String name, int age) {
        this.name = name;
        this.age = age;
    }
    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;
    }
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        write();
        read();
    }

    public static void write() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.txt"));
        oos.writeObject(new Person("Tom", 20));
        oos.close();
    }

    public static void read() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.txt"));
        Object obj = ois.readObject();
        System.out.println(obj);
        ois.close();
    }
}
/*
在本目录下生成file.txt 并写入对象信息
使用transient关键字声明不需要序列化的成员变量
age不需要序列化
在屏幕输出对象信息
Person [name=Tom, age=0]
*/

3、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因

class Test {
    public static void main(String[] args) throws IOException {
        write();
        read();
    }

    public static void write() throws IOException {
        Properties prop = new Properties();
        prop.setProperty("name", "Tom");
        prop.setProperty("age", "20");
        prop.store(new FileWriter("file.txt"), "Person Info");
    }

    public static void read() throws IOException {
        Properties prop = new Properties();
        prop.load(new FileReader("file.txt"));
        Set<String> keys = prop.stringPropertyNames();
        for (String key : keys) {
            System.out.println(prop.getProperty(key));
        }
    }
}
/*
在本目录下生成file.txt 并写入键值对
在屏幕输出对象信息
20
Tom
*/

C:编程题

1、编写程序,通过打印流完成复制文本文件

public class Homework01 {
    public static void main(String[] args) throws IOException {
        //打印流完成复制文本文件
        BufferedReader in = new BufferedReader(new FileReader("E:\\copy\\a.txt"));
        PrintWriter out = new PrintWriter(new FileWriter("E:\\copy\\b.txt"));
        String line;
        while ((line = in.readLine()) != null) {
            out.println(line);
        }
        in.close();
        out.close();
    }
}

2、编写程序,通过序列化流与反序列化流完成从file.txt文件存取对象的操作

public class Homework02 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //通过序列化流与反序列化流完成从file.txt文件存取对象的操作
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.txt"));
        out.writeObject(new Student("jack", 15));
        out.writeObject(new Student("tom", 16));
        out.close();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("file.txt"));
        Student s1 = (Student) in.readObject();
        System.out.println(s1);
        Student s2 = (Student) in.readObject();
        System.out.println(s2);
        in.close();
    }
}

class Student implements Serializable {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、编写程序,判断文件中是否有指定的键如果有就修改值

需求:我有一个文本文件file.txt,我知道数据是键值对形式的,但是不知道内容是什么。

请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"

file.txt文件内容如下:
zhangsan = 90
lisi = 80
wangwu = 85
public class Homework03 {
    public static void main(String[] args) throws IOException {
        Properties p = new Properties();
        p.load(new FileReader("file.txt"));
        p.forEach(new BiConsumer<Object, Object>() {
            @Override
            public void accept(Object o, Object o2) {
                String key = (String) o;
                if ("lisi".equals(key)) {
                    p.setProperty(key, "100");
                }
            }
        });
        p.store(new FileWriter("file.txt"), null);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将之前day2练习生成的音乐添加混响效果并进行实时处理和显示的代码: ```matlab clc; clear; close all; % 生成音乐 fs = 44100; t = 0:1/fs:5; f1 = 440; f2 = 880; f3 = 1320; y = sin(2*pi*f1*t) + sin(2*pi*f2*t) + sin(2*pi*f3*t); % 创建音频播放器和图形窗口 sigPlayer = audioplayer(y, fs); myFigure = figure('Name', '实时混响'); waveAxes = subplot(2, 1, 1); axis(waveAxes, [0 numel(y)/fs -3 3]); waveAxes.NextPlot = 'replacechildren'; fftAxes = subplot(2, 1, 2); axis(fftAxes, [0 fs/2 0 100]); fftAxes.NextPlot = 'replacechildren'; % 设置音频播放器的参数 windLength = fs / 10; sigPlayer.TimerPeriod = windLength / fs; sigPlayer.TimerFcn = {@realtimeReverb, waveAxes, fftAxes, y, fs}; % 添加混响效果 reverbObject = reverberator('PreDelay', 0.5, 'WetDryMix', 0.5); % 创建实时波形和频谱图对象 realtimeWavePlot = animatedline(waveAxes); realtimeFFTPlot = bar(fftAxes, [], []); % 播放音乐 play(sigPlayer); function realtimeReverb(plr, ~, axwave, axfft, sigData, fs) persistent reverbObject; persistent ind; persistent indbase; if isempty(reverbObject) reverbObject = reverberator('PreDelay', 0.5, 'WetDryMix', 0.5); end if isempty(indbase) indbase = 1:numel(sigData); end windLength = plr.TimerPeriod * fs; ind = fix((plr.CurrentSample) / windLength) * windLength + indbase; segData = sigData(ind); % 混响处理 reverbAudio = reverb(reverbObject, segData); ydata_fft = fft(reverbAudio); ydata_abs = abs(ydata_fft(1:numel(segData)/2)); % 更新实时波形和频谱图的数据 addpoints(realtimeWavePlot, (1:numel(segData))/fs, segData); set(realtimeFFTPlot, 'ydata', log(ydata_abs)); drawnow; end ``` 这段代码首先生成了一个简单的音乐信号 `y`,然后创建了一个音频播放器对象 `sigPlayer` 和一个图形窗口 `myFigure`。在图形窗口中,创建了两个子图 `waveAxes` 和 `fftAxes`,用于显示实时波形图和频谱图。 接下来,设置音频播放器的参数,包括定时器周期和定时器函数。定时器函数 `realtimeReverb` 用于实时处理音频数据并更新图形。在定时器函数中,使用 `reverberator` 函数创建了混响效果对象 `reverbObject`,并对音频数据进行混响处理。然后,通过 FFT 算法计算音频数据的频谱,并将数据更新到实时波形图和频谱图中。 最后,通过调用 `play` 函数开始播放音乐,并启动定时器以实现实时处理和显示。 请注意,在运行代码之前,您需要确保已经安装了 Matlab,并具有 `reverberator` 函数和 Day 2 练习生成的音乐信号 `y`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值