Java 异常处理与输入输出
一、异常

1.1
package exception;
import java.util.Scanner;
public class ArrayIndex {
public static void main(String[] args) {
int[] a=new int[10];
Scanner in=new Scanner(System.in);
int index=in.nextInt();
try
{
a[index]=25;
System.out.println("right");
}
catch(ArrayIndexOutOfBoundsException b)
{
System.out.println("there is a wrong");
}
in.close();
}
}


出现异常就会到catch。
1.2 调用的函数中出现异常
package exception;
import java.util.Scanner;
public class ArrayIndex {
public static void f()
{
int[] a=new int[10];
a[10]=15;
System.out.println("right");
}
public static void main(String[] args) {
try
{
f();
}
catch(ArrayIndexOutOfBoundsException b)
{
System.out.println("there is a wrong");
}
System.out.println("end");
}
}

1.3
package exception;
import java.util.Scanner;
public class ArrayIndex {
public static void f()
{
int[] a=new int[10];
a[10]=15;
System.out.println("hello");
}
public static void g()
{
f();
}
public static void h()
{
int i=10;
if(i<100)
g();
}
public static void k()
{
try {
h();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("k()");
}
}
public static void main(String[] args) {
try
{
k();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("there is a wrong");
}
System.out.println("end");
}
}

try ...catch 里出现嵌套时,只会输出里层嵌套的内容。
catch的存在就是为了处理出现的异常。
相当于在k()函数中发现了异常,并且利用catch已经将异常解决了。所以在main()中便默认k()没有了异常,所以不会输出“there is a wrong”。
1.4
package exception;
import java.util.Scanner;
public class ArrayIndex {
public static void f()
{
int[] a=new int[10];
a[10]=15;
System.out.println("hello");
}
public static void g()
{
f();
}
public static void h()
{
int i=10;
if(i<100)
g();
}
public static void k()
{
try {
h();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("k()");
throw e;//抛出异常
}
}
public static void main(String[] args) {
try
{
k();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("there is a wrong");
System.out.println(e.getMessage());//输出e的值,10,这是指数组a[10]处赋值出现问题
System.out.println(e);
e.printStackTrace();//显示调用路径
}
System.out.println("end");
}
}
throw e:相当于将处理的异常又重新添加加进来,这样“there is a wrong ”也便会输出。
或者说对于异常catch没有处理完全。所以还需要再度输出。

1.5
二、流


2.1 二进制数据
package Hello; import java.io.IOException;
import java.util.Scanner; public class test { public static void main(String[] args) {
System.out.println("Hello world");
byte[] buffer=new byte[1024];//定义了字节数组
try {
int len=System.in.read(buffer);//往buffer中写入数据,并返回长度
String s=new String(buffer, 0, len);//从buffer中0的位置开始,获取len长度的字节
System.out.println("读到了"+len+"字节");
System.out.println(s);
System.out.println("s的长度是:"+s.length());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}




回车和换行算两个字符。

2.2
package Hello; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner; public class test { public static void main(String[] args) {
System.out.println("Hello world");
byte[] buffer=new byte[5];
for(int i=0;i<buffer.length;i++)
{
buffer[i]='a';
}
try {
FileOutputStream out=new FileOutputStream("a.txt");
out.write(buffer);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


2.3 过滤器

package Hello; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner; import javax.imageio.stream.FileImageOutputStream; public class test { public static void main(String[] args) {
System.out.println("Hello world");
byte[] buffer=new byte[10];
for(int i=0;i<buffer.length;i++)
{
buffer[i]=(byte) i;
}
try {
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("b.dat")));//只能处理字节
int i=123454;
out.writeInt(i);
out.close(); DataInputStream in=new DataInputStream(
new BufferedInputStream(
new FileInputStream("b.dat")));
int j=in.readInt();
System.out.println(j); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以16进制存储。




2.4 文本


package Hello; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner; import javax.imageio.stream.FileImageOutputStream; public class test { public static void main(String[] args) {
System.out.println("Hello world");
byte[] buffer=new byte[10];
for(int i=0;i<buffer.length;i++)
{
buffer[i]=(byte) i;
}
try {
PrintWriter out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("c.txt"))));//
int i=12345;
out.println(i);
out.close(); BufferedReader in=new BufferedReader(
new InputStreamReader(
new FileInputStream("c.txt")));
String line;
while((line=in.readLine())!=null)
System.out.println(line); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}





2.5编码格式
package Hello; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner; import javax.imageio.stream.FileImageOutputStream; public class test { public static void main(String[] args) {
System.out.println("Hello world");
byte[] buffer=new byte[10];
for(int i=0;i<buffer.length;i++)
{
buffer[i]=(byte) i;
}
try {
PrintWriter out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("c.txt"))));//
int i=12345;
out.println(i);
out.close(); BufferedReader in=new BufferedReader(
new InputStreamReader(
new FileInputStream("a.txt"),"GBk"));//设置编码格式
String line;
while((line=in.readLine())!=null)
System.out.println(line); } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GBK编码时,正确读出

utf8时

2.6 格式化输出



三、流应用
package Hello; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket; public class hello { public static void main(String[] args) {
try {
Socket socket=new Socket(InetAddress.getByName("localhost"),12345);
PrintWriter out=new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())));//
out.println("hello"); BufferedReader in=new BufferedReader(
new InputStreamReader(
socket.getInputStream()));//设置编码格式
String line;
line=in.readLine();
System.out.println(line);
out.close();
socket.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
四、对象串行化
package Hello; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket; class Student implements Serializable{ //可串行化的类
private String name;
private int age;
private int grade; public Student(String name,int age,int grade)
{
this.name=name;
this.age=age;
this.grade=grade;
} public String toString()
{
return name+" "+age+" "+grade;
}
} public class hello { public static void main(String[] args) {
try {
Student s1=new Student("John",18,5);
System.out.println(s1);
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream("obj.dat"));
out.writeObject(s1);
out.close();
ObjectInputStream in=new ObjectInputStream(
new FileInputStream("obj.dat"));
Student s2=(Student)in.readObject();
System.out.println(s2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
保存在obj.dat中的值


Java 异常处理与输入输出的更多相关文章
- Java—异常处理总结
异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制异常痛苦,同一个异常或者错误如果多个地方出 ...
- [ Java学习基础 ] Java异常处理
一.异常概述 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用Sys ...
- 2017.4.7 java异常处理总结
目录 1.java异常处理的几种错误做法 2.异常处理示例 3.常用异常 4.异常类的继承关系 5.异常处理机制 6.Throw和Throws的区别 7.e.toString(), e.getCaus ...
- Java异常处理总结Exception\Error
Java异常处理总结Exception\Error 2012-12-28 08:17:17| 分类: JAVA | 标签:java |举报|字号 订阅 Java异常处理总结 ...
- 这几种Java异常处理方法,你会吗?
摘要:我们在软件开发的过程中,任何语言的开发过程中都离不开异常处理. 本文分享自华为云社区<Java异常处理学习总结>,作者: zekelove . 我们在软件开发的过程中,任何语言的开发 ...
- 札记:Java异常处理
异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...
- java异常处理(父子异常的处理)
我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...
- Java 异常处理
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...
- 《转载》Java异常处理的10个最佳实践
本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...
随机推荐
- 前端每日实战:92# 视频演示如何用纯 CSS 创作一颗逼真的土星
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/EpbaQX 可交互视频 此视频是可 ...
- 必备技能六、Vue框架引入JS库的正确姿势
在Vue.js应用中,可能需要引入Lodash,Moment,Axios,Async等非常好用的JavaScript库.当项目变得复杂庞大,通常会将代码进行模块化拆分.可能还需要跑在不同的环境下,比如 ...
- VUE四 axios详解
axios的中文文档写的已经很详细 https://www.kancloud.cn/yunye/axios/234845
- ZXingObjC直接引用第三方工程使用方法
1.下载ZXingObjc压缩包,解压缩. 2.将文件拷贝到项目工程目录下 3.到工程目录中ZXingObjc文件夹中将ZXing的执行文件拖拽到项目中. 4.点击项目targets ——>Bu ...
- 【原创】(求锤得锤的故事)Redis锁从面试连环炮聊到神仙打架。
这是why技术的第38篇原创文章 又到了一周一次的分享时间啦,老规矩,还是先荒腔走板的聊聊生活. 有上面的图是读大学的时候,一次自行车骑行途中队友抓拍的我的照片.拍照的地方,名字叫做牛背山,一个名字很 ...
- DataFrame简介(一)
1. DataFrame 本片将介绍Spark RDD的限制以及DataFrame(DF)如何克服这些限制,从如何创建DataFrame,到DF的各种特性,以及如何优化执行计划.最后还会介绍DF有哪些 ...
- 关于BitmapImage EndInit()时报值不在范围内的异常
值不在预期的范围内.ArgumentException 在 System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorConte ...
- Java并发编程之验证volatile不能保证原子性
Java并发编程之验证volatile不能保证原子性 通过系列文章的学习,凯哥已经介绍了volatile的三大特性.1:保证可见性 2:不保证原子性 3:保证顺序.那么怎么来验证可见性呢?本文凯哥(凯 ...
- 2018 巅峰极客CTF misc writeup
flows 拿到一个pcap包,用wireshark打开,发现是USB协议,尝试在kali下使用tshark提取,提取失败,发现异常.回到wireshark分析数据.在其中一个数据包中发现了tip 把 ...
- Mass Spectrometry-Compatible Subcellular Fractionation for Proteomics 质谱兼容的蛋白质组学的亚细胞分离(解读人:王茹凯)
文献名:Mass Spectrometry-Compatible Subcellular Fractionation for Proteomics(质谱兼容的蛋白质组学的亚细胞分离) 期刊名:Jpor ...