一、异常

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 异常处理与输入输出的更多相关文章

  1. Java—异常处理总结

    异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制异常痛苦,同一个异常或者错误如果多个地方出 ...

  2. [ Java学习基础 ] Java异常处理

    一.异常概述 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的.比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用Sys ...

  3. 2017.4.7 java异常处理总结

    目录 1.java异常处理的几种错误做法 2.异常处理示例 3.常用异常 4.异常类的继承关系 5.异常处理机制 6.Throw和Throws的区别 7.e.toString(), e.getCaus ...

  4. Java异常处理总结Exception\Error

    Java异常处理总结Exception\Error 2012-12-28 08:17:17|  分类: JAVA |  标签:java  |举报|字号 订阅   Java异常处理总结          ...

  5. 这几种Java异常处理方法,你会吗?

    摘要:我们在软件开发的过程中,任何语言的开发过程中都离不开异常处理. 本文分享自华为云社区<Java异常处理学习总结>,作者: zekelove . 我们在软件开发的过程中,任何语言的开发 ...

  6. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

  7. java异常处理(父子异常的处理)

    我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...

  8. Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...

  9. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

随机推荐

  1. vue+element 表单封成组件(1)

    作为一名刚接触vue不到一个月的菜鸟,思想还没有从操作DOM转变为数据驱动,看vue的代码处处别扭.组里为了让我熟悉vue交给了我一个将element 表单封装成组件的练手任务.由于开发过程中遇到的表 ...

  2. Flask架构管理及特点(重要)

    正文 程序包结构 ——————————————————————————————————flask文件夹结构 其中:app为程序包,Flask程序保存在这个包中migrations文件夹包含数据库迁移脚 ...

  3. 移动webApp必备技能一、WebApp 里Meta标签大全,webappmeta标签大全

    1.先说说mate标签里的viewport: viewport即可视区域,对于桌面浏览器而言,viewport指的就是除去所有工具栏.状态栏.滚动条等等之后用于看网页的区域.对于传统WEB页面来说,9 ...

  4. java算法--普通队列

    数据结构队列 首先明确一下队列的概念. 队列是一种有序列表,使用数组的结构来存储队列的数据. 队列是一种先进先出的算法.由前端加入,由后端输出. 如下图: ​ 第一个图 第二个图 第三个图 这就是队列 ...

  5. .net core WebAPI+EF 动态接收前台json,并动态修改数据库

    用API开发的人都知道,常用的后台接收参数就是建个DTO,然后前台把这个DTO传过来.后台再更新,例如如下例子: public async Task<IActionResult> PutM ...

  6. AX2012 form displays unusually because of native resolution issues(由于本机高分辨率问题导致AX2012界面显示异常)

    Please tick the 'Disable display scaling on high DPI settings' and re-logiin AX,it will be OK. 当你遇到本 ...

  7. 使用.Net Core编写命令行工具(CLI)

    命令行工具(CLI) 命令行工具(CLI)是在图形用户界面得到普及之前使用最为广泛的用户界面,它通常不支持鼠标,用户通过键盘输入指令,计算机接收到指令后,予以执行. 通常认为,命令行工具(CLI)没有 ...

  8. Jmeter——如何使得token在各线程组间引用的游刃有余

    在以前的博文中,有介绍过,jmeter基本的关联,关联就是将参数在各接口中动态传参,使得接口脚本变得灵活使用,非一次性脚本.今天再来介绍一种jmeter全局变量的设置与使用,可以让脚本运用更丰富,场景 ...

  9. 浅谈ASP.NET Core中的DI

    DI的一些事 传送门马丁大叔的文章 什么是依赖注入(DI: Dependency Injection)?     依赖注入(DI)是一种面向对象的软件设计模式,主要是帮助开发人员开发出松耦合的应用程序 ...

  10. 扩展gRPC支持consul服务发现和Polly策略

    gRPC由于需要用工具生成代码实现,可开发性不是很高,在扩展这方面不是很友好 最近研究了下,进行了扩展,不需要额外的工具生成,直接使用默认Grpc.Tools生成的代理类即可 相关源码在文章底部 客户 ...