JAVA exception异常处理+I/O操作读写文件 笔记

来源:互联网 发布:手机淘宝7.1.1版本 编辑:程序博客网 时间:2024/04/28 16:23

JAVA  异常处理

 JAVA的异常5个关键字try...catch...finally..throw..throws


捕获异常声明异常抛出异常try执行可能产生异常的代码throwsthrow  catch捕获异常声明方法可能要抛出的的各种异常手动抛出异常finally无论是否发生异常代码总能执行


所有异常对象都包含了如下几个常用方法:
getMessage():返回该异常的详细描述字符串。
printStackTrace():将该异常的跟踪栈信息输出到标准错误输出。
printStackTrace(PrintStream s):将该异常的跟踪栈信息输出到指定输出流。
getStackTrace():返回该异常的跟踪栈信息。







try
{
需要检测的代码;
}
catch(异常类  变量)
{
异常处理代码;
}
finally
{
一定会执行的代码;
}
Finally代码块只有一种情况不会被执行。就是在之前执行了System.exit(0)。



异常处理的嵌套

异常处理流程代码可以放在任何能放可执行性代码的地方,因此完整的异常处理流程既可放在try块里,也可放在catch块里,也可放在finally块里。
异常处理嵌套的深度没有很明确的限制,但通常没有必要使用超过两层的嵌套异常处理,层次太深的嵌套异常处理没有太大不要,而且导致程序可读性降低。
 






I/O操作读写文件

import java.io.File;
……
File file = new File(args[0]);


System.out.println("文件或目录是否存在:" +  file.exists());
System.out.println("是文件吗:" +  file.isFile());
System.out.println("是目录吗:" +  file.isDirectory());
System.out.println("名称:" +  file .getName());
System.out.println("路径: " + file.getPath());
System.out.println("绝对路径: " + file.getAbsolutePath());
System.out.println("最后修改时间:" + file.lastModified());    
System.out.println(“文件大小:” + file.length()+ “ 字节”);
……

Java的IO流是实现输入/输出的基础,它可以方便地实现数据的输入/输出操作,Java中把不同的输入/输出源(键盘、文件、网络连接等)抽象表述为“流”(stream),通过流的方式允许Java程序使用相同的方式来访问不同的输入/输出源。stream是从起源(source)到接收(sink)的有序数据。
Java把所有传统的个流类型(类或抽象类)都放在java.io包中,用以实现输入/输出功能。

按照流的流向来分:可以分为输入流和输出流。
输入流:只能从中读取数据,而不能向其写出数据。
输出流:只能向其写出数据,而不能从中读取数据。
字节流和字符流
按照流的角色分,可以分为节点流和处理流。

文本文件的读写
用FileInputStream读文件
用FileOutputStream写文件
用BufferedReader读文本文件
用BufferedWriter写文本文件
二进制文件的读写
DataOutputStream
DataInputStream

IO流的四个基类

Java把所有设备里的有序数据抽象成流模型简化了输入/输出的处理。
Java的IO流共涉及到40多个类,这些类看上去芜杂而凌乱,但实际上是非常规则、而且彼此之间存在非常紧密的联系。

Java的IO流的40多个类都是从4个抽象基类派生出来的:

InputStream/Reader:所有输入流的基类,前者是输入字节流,后者是输入字符流。
OutputStream/Writer:所有输出流的基类,前者是输出字节流,后者是输出字符流。

输入流

InputStream和Reader是所有输入流的基类,它们都是两个抽象类,本身并不能创建实例来执行输入,但它们将所谓所有输入流的模板,所以它们的方法是所有输入流都可使用的方法。它们包含如下三个方法:
int read():从输入流中读取单个字节(相当于从图15.5所示水管中取出一滴水),返回所读取的字节数据(字节数据可直接转换为int类型)。
int read(byte[]/char[] b):从输入流中读取最多b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数。
int read(byte[]/char[] b, int off, int len):从输入流中读取最多len字节的数据,并将其存储在数组 b 中,放入b数组中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数。 

输出流

OutputStream和Writer也非常相似,它们采用如图15.6所示的模型来执行输出,两个流都提供了如下三个方法:
void write(int c):将指定的字节/字符输出到输出流中,其中c既可以代表字节,也可以代表字符。
void write(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定输出流中。
void write(char[] cbuf, int off, int len):将字节数组/字符数组中从off位置开始,长度为len的字节/字符输出到输出流中。

package ch07;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.file.FileSystemException;public class FileInputStreamTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileInputStream fis=null;try { fis=new FileInputStream("E:\\新建文件夹\\12.txt");StringBuffer sbff=new StringBuffer();int next=0;while((next=fis.read())!=-1){sbff.append((char)next);}System.out.println(sbff);} catch (FileSystemException e) {// TODO: handle exception} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

package ch07;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class FileInputStreamTest2 {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileInputStream fis=null;try {fis=new FileInputStream("E:\\新建文件夹\\12.txt");byte[] b=new byte[30];StringBuffer sbff=new StringBuffer();while(fis.read(b)!=-1){sbff.append(new String(b));}System.out.println(sbff);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

读某文件夹里内容




在文件里写内容

package ch07;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class FilOutputStreamTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileOutputStream fos = null;String name = "E:\\新建文件夹\\1234.txt";try {fos = new FileOutputStream(name);   //清空后加(name)//追加(name,true)String str = "dhahashdsd中国";         fos.write(str.getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fos.flush();fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

生气复制文字生气

package ch07;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class InputOuput {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileInputStream fis=null;FileOutputStream fos = null;String inputname = "E:\\新建文件夹\\1234.txt";String ouputname = "E:\\新建文件夹\\123455.txt";try {fis=new FileInputStream(inputname);fos = new FileOutputStream(ouputname); byte[] b=new byte[3000];StringBuffer sbff=new StringBuffer();while(fis.read(b)!=-1){sbff.append(new String(b));}String str = sbff.toString();         fos.write(str.getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fis.close();fos.flush();fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


生气复制图片生气

package ch07;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class inputouputtupian {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileInputStream fis=null;FileOutputStream fos = null;String inputname = "E:\\新建文件夹\\123.jpg";String ouputname = "E:\\新建文件夹\\123455.jpg";try {fis=new FileInputStream(inputname);fos = new FileOutputStream(ouputname); //byte[] b=new byte[3000000];//StringBuffer sbff=new StringBuffer();int next=0;while((next=fis.read())!=-1){ fos.write(next);}//String str = sbff.toString();//         fos.write(str.getBytes());} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fis.close();fos.flush();fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}


作者:冲天之峰       20160603



3 0
原创粉丝点击