《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 {
;
}
}
}
}
[代码托管]
-
代码提交过程截图:
-
代码量截图:


学习进度条
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | 重要成长 | |
---|---|---|---|---|
目标 | 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小时
参考资料
作者:Yogile
出处:https://www.cnblogs.com/Yogile/p/10786162.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构