您的位置:首页 > 其它

IO流学习内容记录

2017-02-04 15:52 204 查看


输入字节流:

| InputStream 所有输入字节流的基类 抽象类 

————| FileInputStream 读取文件数据的输入字节流 
使用FileInputStream读取文件数据的步骤: 

1. 找到目标文件 

2. 建立数据的输入通道。 

3. 读取文件中的数据。 

4. 关闭 资源.

注意点:read()方法返回的值讲解:
1、返回值为原本数据数据:
read() 读取一个字节的数据,把读取的数据返回。如:int content = fileInputStream.read();
2、返回值为字节数据的个数
如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,
而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
System.out.print(new String(buf,0,length));
}


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

}

private static void importDemo1() throws FileNotFoundException, IOException {
//找到目标文件
File file = new File("/Users/liyunxiang/Desktop/1.txt");
System.out.println(file.exists()); //判断文件是否存在
//建立数据输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组配合循环读取文件的数据。
int length = 0; //保存每次读取到的字节个数。
//存储读取到的数据    缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。  理论上缓冲数组越大,效率越高
byte[] buf = new byte[1024];

// 如果使用read读取数据传入字节数组,那么数据是存储到字节数组中的,
//而这时候read方法的返回值是表示的是本次读取了几个字节数据到字节数组中。
while((length = fileInputStream.read(buf))!=-1){ // read方法如果读取到了文件的末尾,那么会返回-1表示。
System.out.print(new String(buf,0,length));
}

//关闭资源
fileInputStream.close();
}
}


问题: 读取完一个文件的数据的时候,我不关闭资源有什么影响? 答案: 资源文件一旦 

使用完毕应该马上释放,否则其他的程序无法对该资源文件进行其他 的操作。


输出字节流:

——–| OutputStream 是所有输出字节流 的父类。 抽象类 

———–| FileOutStream 向文件输出数据的输出字节流。
FileOutputStream如何使用呢? 

1. 找到目标文件 

2. 建立数据的输出通道。 

3. 把数据转换成字节数组写出。 

4. 关闭资源

FileOutputStream要注意的细节:
1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是
把低八位的二进制数据写出,其他二十四位数据全部丢弃。
00000000-000000000-00000001-11111111   511
11111111---> -1


public class OutPutDemo {
public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("/Users/liyunxiang/Desktop/1.txt");
//建立数据输出通道
FileOutputStream fileOutputStream = new FileOutputStream(file);
//每次只能写一个字节的数据出去。
fileOutputStream.write('h');
//使用字节数组把数据写出。
String data = "hello world";
fileOutputStream.write(data.getBytes());
//使用字节数组把数据写出。,0 从字节数组的指定索引值开始写, 2:写出两个字节。
byte[] buf = data.getBytes();
fileOutputStream.write(buf,0,3);
//关闭资源
fileOutputStream.close();
}
}



需求: 利用输入输出流拷贝一张图片

public class CopyImage {

public static void main(String[] args) throws IOException {
//找到目标文件
File inFile = new File("F:\\美女\\1.jpg");
File destFile = new File("E:\\1.jpg");
//建立数据的输入输出通道
FileInputStream fileInputStream = new  FileInputStream(inFile);
FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....

//每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
//建立缓冲数据,边读边写
byte[] buf = new byte[1024];
int length = 0 ;
while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
}
//关闭资源 原则: 先开后关,后开先关。
fileOutputStream.close();
fileInputStream.close();
}

}



缓冲输入字节流

我们清楚读取文件数据使用缓冲数组读取效率更高,sun也知道使用缓冲数组读取效率更高,那么 

这时候sun给我们提供了一个——缓冲输入字节流对象,让我们可以更高效率读取文件。
输入字节流体系:  

—-| InputStream 输入字节流的基类。 抽象 

———-| FileInputStream 读取文件数据的输入字节流 

———-| BufferedInputStream 缓冲输入字节流 缓冲输入字节流的出现主要是为了提高读取文件数据的效率。 

其实该类内部只不过是维护了一个8kb的字节数组而已。 
注意: 凡是缓冲流都不具备读写文件的能力。
使用BufferedInputStream的步骤 : 

1. 找到目标文件。 

2. 建立数据 的输入通道 

3. 建立缓冲 输入字节流流 

4. 关闭资源

public class Demo1 {

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

public static void readTest2() throws IOException{
//找到目标文件
File file = new File("F:\\a.txt");

FileInputStream fileInputStream= new FileInputStream(file);
BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
bufferedInputStream.read();

FileOutputStream fileOutputStream= new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream= new BufferedOutputStream(fileOutputStream);
fileOutputStream.write(null);

//读取文件数据
int content = 0 ;
//疑问二:BufferedInputStream出现 的目的是了提高读取文件的效率,但是BufferedInputStream的read方法每次读取一个字节的数据
//而FileInputStreram每次也是读取一个字节的数据,那么BufferedInputStream效率高从何而来?
while((content = fileInputStream.read())!=-1){
System.out.print((char)content);
}

//关闭资源
bufferedInputStream.close();//调用BufferedInputStream的close方法实际上关闭的是FileinputStream.
}

//读取文件的时候我们都是使用缓冲数组读取。效率会更加高
public static void readTest() throws IOException{
File file = new File("/Users/liyunxiang/Desktop/1.txt");
//建立数组通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓冲数组读取数据
byte[] buf = new byte[1024*8];
int length = 0;
while((length = fileInputStream.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
//关闭资源
fileInputStream.close();
}
}



输出字节流

——–| OutputStream 所有输出字节流的基类 抽象类 

————| FileOutputStream 向文件 输出数据 的输出字节流 

————| Bufferedoutputstream 缓冲输出字节流 BufferedOutputStream出现的目的是为了提高写数据的效率。  

内部也是维护了一个8kb的字节数组而已。
使用BufferedOutputStream的步骤: 

1. 找到目标文件 

2. 建立数据的输出通道

BufferedOutputStream 要注意的细节 

1. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中。 

2. 使用BufferedOutStream写数据的时候,它的write方法是是先把数据写到它内部维护的字节数组中,如果需要把数据真正的写到硬盘上面,需要 

调用flush方法或者是close方法、 或者是内部维护的字节数组已经填满数据的时候。

public class Demo2 {

public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("/Users/liyunxiang/Desktop/1.txt");
//建立数据的输出通道
FileOutputStream  fileOutputStream = new FileOutputStream(file);
//建立缓冲输出字节流对象
BufferedOutputStream bufferedOutputStream  = new BufferedOutputStream(fileOutputStream);
//把数据写出
bufferedOutputStream.write("hello world".getBytes());
//把缓冲数组中内部的数据写到硬盘上面。
//bufferedOutputStream.flush();
bufferedOutputStream.close();
}

}



使用缓冲输入输出字节流拷贝一个图片

public class CopyImage {

public static void main(String[] args) throws IOException {
//找到目标文件
File  inFile = new File("F:\\美女\\1.jpg");
File outFile = new File("E:\\1.jpg");
//建立数据输入输出通道
FileInputStream fileInputStream = new FileInputStream(inFile);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
//建立缓冲输入输出流
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//边读边写
int content = 0;
// int length = bufferedInputStream.read(buf);   如果传入了缓冲数组,内容是存储到缓冲数组中,返回值是存储到缓冲数组中的字节个数。
while((content = bufferedInputStream.read())!=-1){ // 如果使用read方法没有传入缓冲数组,那么返回值是读取到的内容。
bufferedOutputStream.write(content);
//          bufferedOutputStream.flush();
}
//关闭资源
bufferedInputStream.close();
bufferedOutputStream.close();

}

}



IO异常 的处理

package cn.itcast.exception;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.management.RuntimeErrorException;

/*
IO异常 的处理

*/
public class Demo1 {

public static void main(String[] args) {
//  readTest();

copyImage();
}

// 拷贝图片
public static void copyImage() {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;

try {
// 找到目标文件
File inFile = new File("F:\\美女\\1.jpg");
File outFile = new File("E:\\1.jpg");
// 建立输入输出通道
fileInputStream = new FileInputStream(inFile);
fileOutputStream = new FileOutputStream(outFile);
// 建立缓冲数组,边读边写
byte[] buf = new byte[1024];
int length = 0;
while ((length = fileInputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, length);
}
} catch (IOException e) {
System.out.println("拷贝图片出错...");
throw new RuntimeException(e);
} finally {
// 关闭资源
try {
if (fileOutputStream != null) {
fileOutputStream.close();
System.out.println("关闭输出流对象成功...");
}
} catch (IOException e) {
System.out.println("关闭输出流资源失败...");
throw new RuntimeException(e);
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
System.out.println("关闭输入流对象成功...");
} catch (IOException e) {
System.out.println("关闭输入流对象失败...");
throw new RuntimeException(e);
}
}

}
}
}

public static void readTest() {
FileInputStream fileInputStream = null;
try {
// 找到目标文件
File file = new File("F:\\aaaaa.txt");
// 建立数据输入通道
fileInputStream = new FileInputStream(file);
// 建立缓冲数组读取数据
byte[] buf = new byte[1024];
int length = 0;
while ((length = fileInputStream.read(buf)) != -1) {
System.out.print(new String(buf, 0, length));
}
} catch (IOException e) {
/*
* //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
* RuntimeException(e);
* //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
* 为了让调用者使用变得更加灵活。
*/
System.out.println("读取文件资源出错....");
throw new RuntimeException(e);
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
System.out.println("关闭资源成功...");
}
} catch (IOException e) {
System.out.println("关闭资源失败...");
throw new RuntimeException(e);
}
}
}

}



输入字符流

字节流:字节流读取的是文件中的二进制数据,读到的数据并不会帮你转换成你看得懂的字符。 
字符流: 字符流会把读取到的二进制的数据进行对应 的编码与解码工作。 字符流 = 字节流 + 编码(解码)
输入字符流: 

———-| Reader 输入字符流的基类 抽象类 

————-| FileReader 读取文件的输入字符流。
FileReader的用法: 

1. 找到目标文件 

2. 建立数据的输入通道 

3. 读取数据 

4. 关闭资源

public class Demo2 {

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

//使用缓冲字符数组读取文件。
public static void readTest2() throws IOException{
//找到目标文件
File file = new File(“/Users/liyunxiang/Desktop/1.txt“);
// 建立数据的输入通道
FileReader fileReader = new FileReader(file);
//建立缓冲字符数组读取文件数据
char[] buf = new char[1024];
int length = 0 ;
while((length = fileReader.read(buf))!=-1){
System.out.print(new String(buf,0,length));
}
}

public static void readTest1() throws IOException{
//找到目标文件
File file = new File("/Users/liyunxiang/Desktop/1.txt");
//建立数据的输入通道
FileReader fileReader = new FileReader(file);
int content = 0 ;
while((content = fileReader.read())!=-1){ //每次只会读取一个字符,效率低。
System.out.print((char)content);
}
//关闭资源
fileReader.close();
}

}



输出字符流

输出字符流:
——| Writer 输出字符流的基类。 抽象类 

———–| FileWriter 向文件数据数据的输出字符流
FileWriter的使用步骤: 

1. 找到目标文件。 

2. 建立数据输出通道 

3. 写出数据。 

4. 关闭资源

FileWriter要注意的事项: 

1. 使用FileWriter写数据的时候,FileWriter内部是维护了一个1024个字符数组的,写数据的时候会先写入到它内部维护的字符数组中,如果需要 

把数据真正写到硬盘上,需要调用flush或者是close方法或者是填满了内部的字符数组。 

2. 使用FileWriter的时候,如果目标文件不存在,那么会自动创建目标文件。 

3.使用FileWriter的时候, 如果目标文件已经存在了,那么默认情况会先情况文件中的数据,然后再写入数据 , 如果需要在原来的基础上追加数据, 需要使用“new FileWriter(File , boolean)”的构造方法,第二参数为true。

public class Demo1 {

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

public static void  writeTest1() throws IOException{
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据输出通道
FileWriter fileWriter = new FileWriter(file,true);
//准备数据,把数据写出
String data = "今天天气非常好!!";
fileWriter.write(data);  //字符流具备解码的功能。
//刷新字符流
//      fileWriter.flush();
//关闭资源
fileWriter.close();

}
}


//使用字符流拷贝文件
public class Copy {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\Test.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("E:\\Test.exe"));
String line=null;
while((line = bufferedReader.readLine())!=null){
bufferedWriter.write(line);
}
bufferedWriter.close();
bufferedReader.close();
}

}



缓冲输入字符流

输入字符流: 

——-| Reader 所有输入字符流的基类。 抽象类 

———-| FileReader 读取文件字符串的输入字符流。 

———-| BufferedReader 缓冲输入字符流 。 缓冲 输入字符流出现的目的是为了提高读取文件 的效率和拓展了FileReader的功能。 其实该类内部也是维护了一个字符数组
记住:缓冲流都不具备读写文件的能力。
BufferedReader的使用步骤: 

1.找到目标文件 

2 .建立数据的输入通道。

public static void readTest1() throws IOException{
//找到目标文件
File file  = new File("F:\\a.txt");
//建立数据的输入通道。
FileReader fileReader = new FileReader(file);
//建立缓冲输入字符流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//读取数据
/*int content = bufferedReader.read();  //读到了一个字符。 读取到的字符肯定也是从Bufferedreader内部的字符数组中获取的到。所以效率高。
System.out.println((char)content);*/
//使用BUfferedReader拓展的功能,readLine()  一次读取一行文本,如果读到了文件的末尾返回null表示。
String line =  null;
while((line = bufferedReader.readLine())!=null){ // 虽然readLine每次读取一行数据,但是但会的line是不包含\r\n的、
System.out.println(Arrays.toString("aaa".getBytes()));
}
//关闭资源
bufferedReader.close();

}

}



缓冲输出字符流

输出字符流 

———-| Writer 所有输出字符流的基类, 抽象类。 

————— | FileWriter 向文件输出字符数据的输出字符流。  

—————-| BufferedWriter 缓冲输出字符流 缓冲输出字符流作用: 提高FileWriter的写数据效率与拓展FileWriter的功能。 

BufferedWriter内部只不过是提供了一个8192长度的字符数组作为缓冲区而已,拓展了FileWriter的功能。
BufferedWriter如何使用? 

1. 找到目标文件 

2. 建立数据的输出通道

public class Demo2 {

public static void main(String[] args) throws IOException {
//找到目标文件
File file = new File("F:\\a.txt");
//建立数据的输出通道
FileWriter fileWriter = new FileWriter(file,true);
//建立缓冲输出流对象
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//写出数据
//      bufferedWriter.newLine(); //newLine() 换行。 实际上就是想文件输出\r\n.
bufferedWriter.write("\r\n");
bufferedWriter.write("前两天李克强来萝岗!!");
//关闭资源
bufferedWriter.flush();
//      bufferedWriter.close();

}

}



练习: 缓冲输入输出字符流用户登陆注册…

public class Login {

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) throws IOException {
while(true){
System.out.println("请选择功能: A(注册)   B(登陆)");
String option =  scanner.next();
if("a".equalsIgnoreCase(option)){
//注册
reg();

}else if("b".equalsIgnoreCase(option)){
//登陆
login();

}else{
System.out.println("你的输入有误,请重新输入...");
}
}
}

//登陆
public static void login() throws IOException{
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请 输入密码:");
String password = scanner.next();
String info = userName+" "+ password;
//读取文件的信息,查看是否有该用户的信息存在,如果存在则登陆成功。
//建立数据的输入通道
//建立缓冲输入字符流
BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\users.txt"));
String line = null;

boolean isLogin = false; // 用于记录是否登陆成功的标识, 默认是登陆失败的。
//不断的读取文件的内容
while((line = bufferedReader.readLine())!=null){
if(info.equals(line)){
isLogin = true;
break;
}
}

if(isLogin){
System.out.println("欢迎"+userName+"登陆成功...");
}else{
System.out.println("不存在该用户信息,请注册!!");
}

}

//注册
public static void reg() throws IOException{
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请 输入密码:");
String password = scanner.next();
String info = userName+" "+ password;
//把用户的注册的信息写到文件上
File file = new File("F:\\users.txt");
FileWriter fileWriter = new FileWriter(file,true);
//建立缓冲输出字符流
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
//把用户信息写出

bufferedWriter.write(info);
bufferedWriter.newLine();
//关闭资源
bufferedWriter.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: