对于可检测异常,若是发生异常的方法不捕获和处理这个异常,则必须在该方法定义的声明头中包含throws子句;php
System.out.in
:标准输入流;System.out.out
:标准输出流;System.out.err
:标准错误流(输出错误信息);html
public class test { public static void main(String[] args) { StringTooLongException a = new StringTooLongException(""); System.out.println(a); } }
在这里就能够看见不少异常的方法,因此就能够说明是一个对象,包含一个对象所具备一些性质;java
(2)由于咱们是到其实类都是object类的子类,因此在书里有这样一句话:git
Java预约义了一组程序执行中可能发生的异常和错误;web
因此咱们应当明白,其实也就能够理解为它是一个对象所拥有的一个性质。编程
问题2解决方案:既然是程序里的一部分,因此我在想,假如程序运行,他会被抛出,会不会是程序就是输出的一部分,或者就是一部分语句块,想了不少中状况,由于在后面讲的捕捉异常,我感受它应该是是一个输出的结果,或者就是一个检测程序错误的部分。数组
问题3解决方案:
书里是这样解释的:缓存
一个异常是一个定义非正常状况或错误的对象;安全
错误相似异常,不一样之处是错误表明不可恢复的问题而且必须捕获处理。数据结构
感受这个概念书里讲的挺模糊的,就上网查了查:
问题4解决方案:
我目前尚未找到答案,感受这个问题本身太较真了,或者再后面仍是能够利用自定义异常来编写,因此感受问的这个问题价值不是很高,可是由于这个问题我仍是进行了思考的,因此仍是记录下来。
System.err
和System.out
的具体用法?public class Test2 { static{ System.out.println("1"); } public static void main(String[] args) { System.err.println("2"); new Test2(); } public Test2() { System.out.println("3"); } }
实验结果:
1,3的位置相对不变,2的位置随机出现
import java.io.*; public class FileTest { public static void main(String[] args) throws IOException { //(1)文件建立(文件类实例化) File file = new File("C:\\Users\\besti\\Desktop\\FileTest","HelloWorld.txt"); if (!file.exists()){ file.createNewFile(); } //(2)文件读写 //第一种:字节流读写,先写后读 OutputStream outputStream1 = new FileOutputStream(file); byte[] hello = {'H','e','l','l','o',',','W','o','r','l','d','!'}; outputStream1.write(hello); InputStream inputStream1 = new FileInputStream(file); while (inputStream1.available()> 0){ System.out.print((char) inputStream1.read()+" "); } inputStream1.close(); //============================BufferedInputStream==================================== byte[] buffer = new byte[1024]; String content = ""; int flag = 0; InputStream inputStream2 = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream2); while ((flag =bufferedInputStream.read(buffer))!=-1){ content += new String(buffer,0,flag); } System.out.println(content); bufferedInputStream.close(); //====================================BufferedOutputstream================================================ OutputStream outputStream2 = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream2 = new BufferedOutputStream(outputStream2); String content2 = "写入文件的缓冲区内容"; bufferedOutputStream2.write(content2.getBytes(),0,content2.getBytes().length); bufferedOutputStream2.flush(); bufferedOutputStream2.close(); //第二种:字符流读写,先写后读(两种读) Writer writer2 = new FileWriter(file); writer2.write("Hello, I/O Operataion!"); writer2.flush(); writer2.append("Hello,World"); writer2.flush(); BufferedWriter bufferedWriter = new BufferedWriter(writer2); String content3 = "使用bufferedWriter写入"; bufferedWriter.write(content3,0,content3.length()); bufferedWriter.flush(); bufferedWriter.close(); Reader reader2 = new FileReader(file); System.out.println(); while(reader2.ready()){ System.out.print((char) reader2.read()+ " "); } BufferedReader bufferedReader = new BufferedReader(reader2); while ((content =bufferedReader.readLine())!= null){ System.out.println(content); } } }
问题1:在编写一次课上做业的时候,第一次我是这样编的,当时觉得对了,在最后一秒发现,这样的编法没有用递归...
问题1解决方案:在周围同窗的忽然一点下,知道本身在后面的静态方法里没有用递归,因此赶忙作出了改变:
ps:红圈是第一次与第二次不一样的地方,也是错误的地方。
import java.io.*; import java.util.Arrays; import java.util.Scanner; public class homework1 { public static void main(String[] args) throws IOException{ File file = new File("D:\\test","Test.txt"); if (!file.exists()){ file.createNewFile();} String a; Scanner scan = new Scanner(System.in); System.out.println("输入几个数字: "); a=scan.nextLine(); Writer writer = new FileWriter(file); writer.write(a); writer.flush(); Reader reader = new FileReader(file); System.out.println(); while (reader.ready()){ System.out.print((char)reader.read()+ ""); } String str = a.replaceAll(" ", ""); char[] b=new char[str.length()]; int in = 0; while (in<=str.length()-1) { b[in]=str.charAt(in); in++; } System.out.println(); insertionSort(b); System.out.println("排序后:" + Arrays.toString(b)); FileWriter fw = new FileWriter("D:\\test\\Test.txt"); String q = ""; for(int i=0;i<b.length;i++){ q += b[i] + ","; } fw.write(q); fw.flush(); fw.close(); } private static void insertionSort(char[] arr) { for (int i = 1; i < arr.length; i++) { int temp = arr[i]; int j = i - 1; while (j >= 0 && temp < arr[j]) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = (char) temp; } } }
输出结果后,是这个样子的:
就出现问题了;
- 我虽然分开输入了不一样位数的数字,可是最后程序把它们分红了一个个数字,也就是这个程序只能排列0~9之间的数字;
因而我就想着能不能把它们先分开,再将一个个总体写入数组里,就开始写:
一、首先我用了这个语句String[] sourceStrArray = a.split(" ");
将其以空格做为分界,把一个个分开之后存进数组;
二、后来就发现,一个是int类型的数组,一个是String型的数组,不匹配,因此就想着用Integer.parseInt
,可是有个问题,这个貌似不能用在数组里,因此用了很长时间研究,用了这样一段代码实现了转换:
int[] t = new int[b.length]; for (int i = 0; i < b.length; i++) { t[i] = Integer.parseInt(b[i]); }
而后输出:
就ok啦!!
错题1
Character streams manage
A. byte-sized data
B. binary data
C. Unicode characters
D. ASCII characters
E. compressed data
正确答案: C 个人答案: B
解析:字符流用于管理16位Unicode字符。这与字节流不一样,后者用于管理任何种类的字节大小的数据,包括ASCII字符和其余类型的二进制数据。
错题2
A processing stream is a data stream that
A. can manage both input streams and output streams at the same time
B. performs some manipulation or process on the data
C. can only manage input streams
D. operates on input and output devices but not files
E. can manage byte streams and character streams at the same time
正确答案: B 个人答案: E
解析:数据流表示特定的源或目标流,用于输入或输出。处理流就像数据流,其中一些额外的处理被添加到输入或输出。例如,处理字节流可能会输入文件中的全部项目,并删除任何不是数字的ASCII字符,以便预期为数字的输入不会引起NumberFormatException。
错题3
Assume infield is a Buffered Reader for a textile and that the textile is empty. What is returned from the message infile.readLine ( );
A. 0
B. null
C. a special character known as the End-of-file marker (EOF)
D. none of the above, the message causes a NullPointerException to be thrown
E. none of the above, the message causes a EndOfFileException to be thrown
正确答案: B 个人答案: D
解析:readLine()方法返回一个等于文件中下一个文本项的String。若是文件为空,则返回null。
错题4
Print Writer is a better output stream class that Print Stream because Print Writer
A. has both print and println methods and PrintStream only has print
B. can output both byte and character streams and PrintStream can only output byte streams
C. has error checking mechanisms as part of the class and PrintStream does not
D. will not throw checked exceptions and PrintStream will
E. all of the above
正确答案: C 个人答案: E
解析:PrintWriter类是一个Writer类,而PrintStream类是一个Stream类。主要区别在于PrintWriter专门用于文件,所以具备不属于PrintStream的错误检查机制。
代码调试中的问题和解决过程, 一个问题加1分
代码行数(新增/累积) | 博客量(新增/累积) | 学习时间(新增/累积) | |
---|---|---|---|
目标 | 5000行 | 30篇 | 400小时 |
第一周 | 156/156 | 1/1 | 15/15 |
第二周 | 217/371 | 1/2 | 20/35 |
第三周 | 233/604 | 2/4 | 20/55 |
第四周 | 1382/1986 | 1/5 | 35/90 |
第五周 | 146/2196 | 1/6 | 25/115 |
第六周 | 462/2658 | 1/7 | 15/130 |
第七周 | 856/3514 | 1/8 | 20/150 |
第八周 | 1877/5391 | 3/11 | 20/170 |
第九周 | 1747/7138 | 1/12 | 20/190 |
Java程序设计
蓝墨云
java基础(System.err和System.out)详解
java的异常和错误有什么区别
java中error和exception的区别