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 ...
随机推荐
- Java 集合、数组排序
在平时开发的过程中,经常会遇到需要对数组.集合中元素按规则进行排序,本文记录在开发过程中可能遇到的情况以及相关的完整代码示例. 知识点 Comparable<T>接口 实现了该接口的对象, ...
- 【新功能】MaxCompoute禁止Full Scan功能开放
摘要: 2018年1月10日,MaxCompute禁止Full Scan功能开放.对于新创建的project默认情况下执行sql时,针对该project里的分区表不允许全表扫描,必须有分区条件指定需要 ...
- [红日安全]Web安全Day9 - 文件下载漏洞实战攻防
本文由红日安全成员: Once 编写,如有不当,还望斧正. 大家好,我们是红日安全-Web安全攻防小组.此项目是关于Web安全的系列文章分享,还包含一个HTB靶场供大家练习,我们给这个项目起了一个名字 ...
- webpack进阶(二)
1)webpack动态打包所有依赖项,避免打包未使用的模块. 2)转换css的loader有:css-loader,style-loader,加载图片或文件的loader是:file-loader,如 ...
- Hadoop fs 基础命令
操作hdfs的基本命令 在hdfs中,路径需要用绝对路径 1. 查看根目录 hadoop fs -ls / 2. 递归查看所有文件和文件夹 -lsr等同于-ls -R hadoop fs -lsr / ...
- HashMap 速描
HashMap 速描 之前简单的整理了Java集合的相关知识,发现HashMap并不是三言两语能够讲明白的,所以专门整理一下HashMap的相关知识. HashMap 存储结构 HashMap是一个哈 ...
- php里面的一些面试经典的函数
<?php /* 这是一个多线程的读取解决的函数 @param1 $fle 传入要读取的文件名 */ function filelock($fle){ $fp=fopen($fls,'w+'); ...
- 结题报告--洛谷P3915
题目:点此. 我处理这种多组数据的方法被我叫做“mains法”,就是先假设只有一组数据,写一个代码,然后把那个main函数改成mains,最后写一个真正的main函数. 这个“真正的”main函数一般 ...
- activiti工作流解决历史批注中文乱码
/** * 根据流程实例查询流程的批注信息 * * @param processInstanceId * @return */ private List<Comment> findComm ...
- javaScript 基础知识汇总(九)
1.Rest 参数 与 Spread 操作符 当我们在代码中遇到“..."时,它不是Rest参数就是Spread操作符 区分方法: 若...出现在函数的参数列表,那它表示的就是Rest参数, ...