《Java程序设计》第9周学习总结


教材学习内容总结

因未熟练掌握第十章 输入、输出流的内容,继续学习第十章。

  • 第十章要点:
    • 要点1:File 类;
    • 要点2:文件字节输入流、输出流、;
    • 要点3:缓冲流、随机流、数组流、数据流、对象流;
    • 要点4:序列化与对象克隆;
    • 要点5:使用 Scanner 解析文件;
    • 要点6:文件对话框;
    • 要点7:带进度条的输入流;
    • 要点8:文件锁、应用举例。

教材学习中的问题和解决过程

1. 输出文件时过多输出。

import java.io.*;
public class test {
    public static void main(String args[]) {
        int n=-1;
        byte [] a=new byte[100];
        try{  File f=new File("D:\javagc\test\src\test.java");
            InputStream in = new FileInputStream(f);
            while((n=in.read(a,0,100))!=-1) {
                String s=new String (a,0,100);
                System.out.print(s);
            }
            in.close();
        }
        catch(IOException e) {
            System.out.println("File read Error"+e);
        }
    }
}

会在输出 test.java 后再多输出数行:

......
        catch(IOException e) {
            System.out.println("File read Error"+e);
        }
    }
}


ch(IOException e) {
            System.out.
Process finished with exit code 0
  • 问题原因分析:
    错误应该出现在第 8 行的 while 循环里,会多输出的原因是:n 是会随时变化的,而 100 是不变的。
    在第八行中 n=in.read(a,0,100) 是每循环一次都往后移 100 单位的长度,由 length() 算出方法长度为 550 。所以最后 100-50=50 的单位长度都由上次赋值的 50 至 99 位决定(未覆盖)。

  • 问题解决方案:
    第 9 行中的 String s=new String (a,0,100); 改成 String s=new String (a,0,n); 即可。

import java.io.*;
public class test {
    public static void main(String args[]) {
        int n=-1;
        byte [] a=new byte[100];
        try{  File f=new File("D:\javagc\test\src\test.java");
            InputStream in = new FileInputStream(f);
            while((n=in.read(a,0,100))!=-1) {
                String s=new String (a,0,n);
                System.out.print(s);
            }
            in.close();
        }
        catch(IOException e) {
            System.out.println("File read Error"+e);
        }
    }
}

代码调试中的问题和解决过程

1. 问题:费马素性检验程序。

  • 问题2解决方案:

代码如下:

import java.util.Scanner;

public class Master {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("输入奇整数 n(n>=3):");
        int number = reader.nextInt();
        System.out.println("输入安全参数 t:");
        int t = reader.nextInt();
        int max=number-1;
        int min=2;
        int r=0;
        for (int j=0,count=0; count<t; j++,count++) {
            int b = (int)(1+Math.random()*(max-min+1));
            int moddle = b;
            for (int i=0; i<number-2; i++) {
                moddle = moddle*b;
                r = moddle%number;
            }

            if (r!=1) {
                System.out.println("n为合数。");
                j=-2;
                return;
            }
            else if (r==1) {
                System.out.println("n为素数。");
            }
            else {
                ;
            }
        }
    }
}

[代码托管]

  • 代码提交过程截图:

  • 代码量截图:

image.png
image.png


学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第一周 322/322 1/1 23/23
第二周 520/842 3/4 25/48
第三周 458/1300 2/6 16/64
第三周 914/2214 2/8 21/85
第四周 685/2899 1/9 18/103
第五周 663/3562 2/11 20/103
第六周 746/3562 1/12 16/103
第七周 1139/4701 2/14 21/124
第八周 548/5249 2/16 12/134
  • 计划学习时间:20小时

  • 实际学习时间:12小时


参考资料

原文地址:https://www.cnblogs.com/Yogile/p/10786162.html