目录


教材学习内容总结

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

  • 第十章要点:

    • 要点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 {
;
}
}
}
}

[代码托管]

  • 代码提交过程截图:

  • 代码量截图:




学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 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小时


参考资料

最新文章

  1. nodejs+easyui(抽奖活动后台)增删改查
  2. 兼容可控硅调光的一款LED驱动电路记录
  3. spark MySQL jar 包
  4. 单向和双向tvs管
  5. hdu 4198 Quick out of the Harbour
  6. Javascript上下文
  7. UITableView section header 不固定
  8. ArrayList 、Vector、 LinkList
  9. css文件和js文件后面带一个问号
  10. FtpClient的使用
  11. 图片拉伸iOS
  12. FPGA与Deep Learning
  13. sql servel 报错:将 expression 转换为数据类型 int 时出现算术溢出错误。
  14. 【转载】SDL2.0在mfc窗口中显示yuv的一种方法
  15. win32多线程学习笔记
  16. Python开发【第十篇】:RabbitMQ队列
  17. javaScript事件(八)事件类型之变动事件
  18. ABAP-表中数据的横向累加
  19. centos7.6删除重新安装python和yum
  20. Java 管道PipedInputStream PipedOutStream PipedReader PipedWriter

热门文章

  1. Ubuntu - Start - 必要软件安装
  2. 性能测试系列-java gc调优
  3. LeetCode--026--删除排序数组中的重复项(java)
  4. 20190321xlVBA_汇总表按模板生成明细表
  5. SQLyog 连接主机的时候出现错误
  6. restore和recover的区别
  7. 调用 CURL 使用正则抓取信息
  8. 世界各国GDP动态排名可视化实现(基于d3.js)
  9. 安全基线自动化扫描、生成报告、加固的实现(以Tomcat为例)
  10. Oracle单机Rman笔记[4]---RMAN联机备份