android 重定向 参数无6,Android应用开发----异常处理、文件操作及重定向

一.异常处理

二.文件操作、流处理、对象序列化与随机读写

三.重定向

一.异常处理

程序难免会在运行时出现各种问题,出现问题时,用户无法自己解决或无需自己解决时,这时可以使用程序中的一些异常处理机制帮助解决或忽视异常。

Exception类是所有异常类的父类。

b8a8934a7068

image.png

捕获异常

语法如下:

try{

待测语句;

}catch(出现异常类型){

出现该异常后的解决方法;

}

1.try-catch支持嵌套,但不建议使用嵌套;要捕获多个异常时,使用如下语法:

try{

待测语句一;

待测语句二;

待测语句三;

}

catch(语句一会出现的异常类型){

出现异常后的解决办法;

}catch(语句二会出现的异常类型){

出现异常后的解决办法;

}catch(语句三会出现的异常类型){

出现异常后的解决办法;

}

2.捕获异常有顺序,try里面先执行的出现异常先被捕获,若先执行的语句的异常是后执行的语句的异常的父类,两句都出现异常时,只捕获父类出现的异常,反之两者都捕获。

try{

异常语句;

}catch(Exception e){

e.printStackTrace();

System.out.println("异常");

}catch(RuntimeException e){

e.prinStackTrace();

System.out.println("运行异常");

}

异常语句出现异常时,将直接打印“异常”,不会打印“运行异常”。

3.若try里面先执行的语句有异常,后执行的语句无异常,catch里只捕获有异常的语句,异常出现时,无异常的语句将不会执行

try{

会出现异常的语句一;

不会出现异常的语句二;

}catch(一出现异常的类型){

异常处理;

}

这种情况下,语句二不会被执行。

4.不管有没有异常都要执行的语句,使用finally关键字

try{

待测语句;

}catch(异常类型){

处理语句;

}finally{

不管有没有异常都会执行的语句;

}

5.不关心具体异常是什么时,catch里面用所有异常的父类Exception类

try{

待测语句;

}catch(Excption e){

e.printStackTrace();//打印异常出现的栈

异常处理;

}

6.某个语句执行出现异常时,这种异常需要由调用者自己处理时,使用throws抛出异常

public class Test{

public void show() throws Exception{

...

...

}

}

二.文件操作与流处理

文件操作

1.创建文件

使用File类创建一个文件对象,File()构造器中,参数可以是文件路径与文件名在一起的字符串,也可以将文件路径与文件名分开两个字符串,还可以是文件对象和字符串类型的文件名

//createfiel()创建一个文件

public void createfile() {

String filename = "D:\\Dreamcatcher\\桌面\\test.txt";

File file = new File(filename);//使用File定义一个文件指针,filename是文件所在路径

//或者:

//File file = new File("D:\\Dreamcatcher\\桌面","test.txt")

if(!file.exists()){

try {

file.createNewFile();//当路径下不存在该文件时,就创建文件

System.out.println("文件创建成功!");

}catch (IOException e){

e.printStackTrace();

}

}else{

System.out.println("文件存在!");

return;

}

}

2.创建文件夹

public void createDir() {

String filename = "D:\\Dreamcatcher\\桌面\\java学习";

File file = new File(filename);

if(!file.exists()){

try {

file.mkdir();

System.out.println("目录创建成功!");

}catch (NullPointerException e){

e.printStackTrace();

}

}else{

System.out.println("文件存在!");

return;

}

}

b8a8934a7068

image.png

3.File类的一些常用方法:

isDirectory(),判断是否是文件夹;

isFile(),判断是否是文件;

getAbsolutePath(),获取绝对路径

list(),(无参数)查看文件夹里面的文件名或文件夹名,返还字符串数组;

list(),(有参数,参数为含过滤的对象)

delete(),删除文件或文件夹

流处理

字符输入流与字符输出流

这类流是Reader流类和Writer流类的子类,用该类实例化的对象是以字符形式处理文件(不能,用于处理文本文件)

文件字符输入流和文件字符输出流

//文件字符输出流

public static void writeToFile(String des) {

String text = "这是一段文本";//待写入字符串

FileWriter fw = null;//选择以字符流写入数据

try {

fw = new FileWriter(des);

fw.write(text);//写入字符串

} catch (IOException e) {

e.printStackTrace();

}finally {//关闭保存已写入文本

try {

fw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

//文件字符输入输出流

public static void copyTxtFile(String src,String des){

try (FileReader fr = new FileReader(src);

FileWriter fw = new FileWriter(des);){

char[] buffer = new char[1024];

int ch;

while (true){

ch = fr.read(buffer);

if(ch == -1){

break;

}

fw.write(buffer);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

缓冲字符输入流与缓冲字符输出流

public static void buffered_io_ch(String src,String des){

try(FileInputStream fis = new FileInputStream(src);

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);//用缓冲字符输入流实例化对象

FileOutputStream fos = new FileOutputStream(des);

OutputStreamWriter osw = new OutputStreamWriter(fos);

BufferedWriter bw = new BufferedWriter(osw)){

int b = 0;

while ( (b = br.read()) != -1){

bw.write(b);

}

bw.flush();

}catch (Exception e){

e.printStackTrace();

}

}

字节输入流与字节输出流

这类流是InputStream流类和OutputStream流类的子类,用该类实例化的对象将以二进制的形式处理文件(可以处理二进制文件,也可以处理文本文件)

文件字节输入流与文件字节输出流

该类读写文件时可以是一次读一个字节,写一个字节,也可以是一次读多个字节,写多个字节

//文件字节输入输出流

public static void copyImage(String src,String des){

try(FileInputStream fis = new FileInputStream(src);

FileOutputStream fos = new FileOutputStream(des)){//当同时打开的文件数目较多,关闭文件麻烦时可以用这种语法实现自动关闭

int b = 0;

while(true){

b = fis.read();//一个字节一个字节的读写,速度较慢

if(b == -1){

break;

}

fos.write(b);

}

System.out.println();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

//文件字节输入输出流,批量读写

public static void copymove_buf(String src,String des){

try(FileInputStream fis = new FileInputStream(src);

FileOutputStream fos = new FileOutputStream(des);

){

byte[] buffer = new byte[1024];

int len = 0;

while(true){

len = fis.read(buffer);//多个字节以数组形式读写,速度相对较快

if(len == -1){

break;

}

fos.write(buffer);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

缓冲字节输入流与缓冲字节输出流

缓冲字节输入流相对于文件字节输入流较快,它是将文件一次性读入缓冲区,再由缓冲区一次性写入文件

public static void buffered_io_by(String src,String des){

//字节缓冲输出输出流

try(FileInputStream fis = new FileInputStream(src);

BufferedInputStream bis = new BufferedInputStream(fis);//用缓冲字节输入流类实例化对象

FileOutputStream fos = new FileOutputStream(des);

BufferedOutputStream bos = new BufferedOutputStream(fos);

){

byte[] buff = new byte[1024];

int ch = 0;

while ((ch = bis.read(buff)) != -1){

bos.write(buff);

}

bos.flush();

}catch (Exception e){

e.printStackTrace();

}

}

对象序列化

当我们需要将一个对象以文件形式保存时(方便传输和储存),就要将对象序列化后写进文件。能实现对象序列化的类必须实现Serializable接口,且属性都是可序列化的或用transient声明。

public class Person implements Serializable {

String name;

int age;

public Person(String name,int age){

this.name = name;

this.age = age;

}

@Override

public String toString() {

return "Person{" +

"name='" + name + '\'' +

", age=" + age +

'}';

}

}

将对象序列化,写进文本

public static void saveObject(String des){

try (FileOutputStream fos = new FileOutputStream(des);

ObjectOutputStream oos = new ObjectOutputStream(fos)){

Person p = new Person("jake",23);

oos.writeObject(p);

} catch (IOException e) {

e.printStackTrace();

}

}

将文本反序列化,恢复为对象

public static void readObject(String src){

try (FileInputStream fis = new FileInputStream(src);

ObjectInputStream ois = new ObjectInputStream(fis)){

Person p = (Person) ois.readObject();//反序列化的对象为Object类,需强转

System.out.println(p);

} catch (Exception e) {

e.printStackTrace();

}

}

随机读写

用RandomAccessFile类实例化对象,构造方法中应该传入读写文件地址和打开文件模式

使用seek方法设置文件指针位置,从设置位置开始读写,写入时文件将被覆盖。

public static void randomAccess(String src){

try{

RandomAccessFile raf = new RandomAccessFile(src,"rw");//“rw”为打开模式,为可读可写

raf.seek(1);

raf.writeChars("hello");

}catch(Exception e){

e.printStackTrace();

}

}

三.重定向

将流中的结果输入或输出到指定的位置。

//输入重定向

public static void redirect_in(String src){

try {

FileInputStream fis = new FileInputStream(src);

Scanner scanner = new Scanner(fis);

while(scanner.hasNext()){

System.out.println(scanner.next());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

//输出重定向

public static void redirect_out(String des){

try (FileOutputStream fos = new FileOutputStream(des);

PrintStream ps = new PrintStream(fos);){

System.setOut(ps);

System.out.println("HEllO");//重定向输出的内容,输出内容需在setOut()方法后,且内容不再打印到屏幕上。

} catch (Exception e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值