IO流与异常

首先我们先来看一下,怎样用JAVA编写一个简单的编辑器,代码如下:

import java.io.*;

import java.util.Scanner;

 

public class TestFileWriter {

public static void main(String[] args) throws Exception {

Scanner sca = new Scanner(System.in);

System.out.println("请输入文件名:");

String filename = sca.next();

File file = new File("D:/",filename);

if(file.exists()){

file.delete();

}

file.createNewFile();

//编写文件内容

System.out.println("请输入文件内容:");

FileWriter fw = new FileWriter("d:/"+filename);

String str;

str = sca.nextLine();

while(!str.equals("Q")){

fw.write(str,0,str.length());

fw.write("\n",0,1);

str = sca.nextLine();

}

fw.close();

/*

FileReader fr = new FileReader("d:/HelloWorld.java");

FileWriter fw = new FileWriter("d:/HelloWorld.bak");

int b;

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

fw.write(b);

}

fr.close();

fw.close();

System.out.println("操作已完成!");*/

}

}

 

 

实现效果:D盘创建了一个Helloworld.java文件。

请输入文件名:

HelloWorld.java

请输入文件内容:

public class HelloWorld{

public static void main(String[] args){

System.out.println("HelloWorld!");

}

}

Q

IO流根据操作类型不同可分为输入流和输出流,还可以按照流处理数据的单位不同分为字节流和字符流两种,但两种分类并不是独立存在的,输入流可分为:字符输入流和字节输入流;输出流可分为:字符输出流和字节输出流。

1、字节输入流的抽象类是InputStream类,实现所有字节输入流的类都继承自InputStream类。用法如下:

public abstact int read() throws IOException

public int read(byte b[]) throws IOException

public int read(byte b[],int offset,int length) throws IOException

2、字节输出流的抽象类是OnputStream类,JAVA语言中所有的字节输出流的实现类均继承自OnputStream类。用法如下:

public abstract void write() throws IOException

public void write(byte b[]) throws IOException

public void write(byte b[],int offset,int length) throws IOException

 

3、字符输入流的抽象类是Reader类,常用于读取文本类型的数据或字符串流的操作。它和字节输入流的区别只是计量单位的不同:字符流一次可以读取1个字符,专门用于操作字符、字符数字或字符串。由此可见Reader在处理字符串时简化了编程,所有实现所有字符输入流的类都继承自Reader类。用法如下:

public  int read() throws IOException

public int read(char c[]) throws IOException

public int read(char c[],int offset,int length) throws IOException

4、字符输出流的抽象类是Writer类,用法如下:

public void write(int c) throws IOException

public void write(char c[]) throws IOException

public void write(char c[],int offset,int length) throws IOException

下面我们来看一下有关字符流的相关小实验:

1字符流的文件读取

/*D盘目录下创建HelloWorld.java

使用字符流的方式进行文件的读取并打印至控制台

计算所读取到的字符数,并在控制台打印*/

import java.io.*

public class sy601 {

public static void main(String[] args) throws IOException {

File sourcefile=new File("D:\\HelloWorld.java");

FileReader  fr= new FileReader(sourcefile);

int i;

while((i=fr.read())!=-1){

System.out.print((char)i);

System.out.println("\n字符数为:"+sourcefile.length());

}

}

2、使用字符流的方式实现文件的复制

/*D盘中的HelloWorld.java文件使用字符流的方式复制到D盘下HW.java文件*/

import java.io.*;

public class shiyan602 {

public static void main(String[] args) throws IOException{

FileReader fr=new FileReader("D:/HelloWorld.java");

FileWriter fw=new FileWriter("D:/HW.java");

int i;

while((i=fr.read())!=-1){

fw.write(i);

}

fr.close();

fw.close();

FileReader fi=new FileReader("D:/HW.java");

System.out.println("文件HW中的内容为:");

while((i=fi.read())!=-1){

System.out.print((char)i);

}

fi.close();

}

}

3、三种方式读文件

/*使用FileReader类、BufferedReader类、FileInputStream类三种方式

D盘下HelloWorld.java文件中读取全部信息并打印到控制台

异常处理使用try---catch关键字进行捕获并打印相关信息*/

import java.io.*;

public class shiyan603 {

public static void main(String[] args) {

//使用FileReader

System.out.print("使用FileReader:   ");

FileReader fr = null;

try {

fr = new FileReader("D:/HelloWorld.java");

} catch (FileNotFoundException e) {

System.out.println("未找到文件!");

}

int i;

try {

while((i=fr.read())!=-1){

System.out.print((char)i);

}

} catch (IOException e) {

System.out.println("不能输出!");

}

try {

fr.close();

} catch (IOException e) {

System.out.println("不能关闭!");

}

System.out.println();

//BufferedReader

System.out.print("使用BufferedReader:   ");

File file=new File("D:/HelloWorld.java");

FileReader fr1 = null;

try {

fr1 = new FileReader(file);

} catch (FileNotFoundException e) {

System.out.println("未找到文件!");

}

BufferedReader br=new BufferedReader(fr1);

String line;

try {

while((line=br.readLine())!=null){

System.out.println(line);

}

} catch (IOException e) {

System.out.println("错误!");

}

try {

br.close();

} catch (IOException e1) {

System.out.println("不能关闭!");

 

}

//FileInputStream

System.out.print("使用FileInputStream:   ");

FileInputStream fis = null;

try {

fis = new FileInputStream("D:/HelloWorld.java");

} catch (FileNotFoundException e) {

System.out.println("未找到文件!");

}

int h;

try {

while((h=fis.read())!=-1){

System.out.print((char)h);

}

} catch (IOException e) {

System.out.println("输出错误!");

}

try {

fis.close();

} catch (IOException e) {

System.out.println("不能关闭!");

}

}

}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值