I/O--3(字节流)

字节流:

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


public class ByteStreamDemo {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {



demo_read();
}


public static void demo_read() throws IOException {

//1,创建一个读取流对象。和指定文件关联。
FileInputStream fis = new FileInputStream("bytedemo.txt");

// System.out.println(fis.available());
// byte[] buf = new byte[fis.available()];//available()获取文件大小,多少字节。慎用,万一一下2g内存的电影,内存就爆了。
// fis.read(buf);
// System.out.println(new String(buf));/'/数组变成字符串一打印。


//建议使用这种读取数据的方式
// byte[] buf = new byte[1024];
// int len = 0;
//
// while((len=fis.read(buf))!=-1){
// System.out.println(new String(buf,0,len));
// }


// int ch = 0;
// while((ch=fis.read())!=-1){
// System.out.println((char)ch);
// }

//一次读取一个字节。一次读不出来一个文字,因为一个字符两个字节。
// int ch = fis.read();
// System.out.println(ch);

fis.close();

}


public static void demo_write() throws IOException {

//1,创建字节输出流对象。用于操作文件.
FileOutputStream fos = new FileOutputStream("bytedemo.txt");

//2,写数据。直接写入到了目的地中。  字节流的文件是不需要临时存储缓冲的,他会直接将数据写入目的地中,因为写的都是源码,所以不用flush()。
fos.write("abcdefg".getBytes());

// fos.flush();
fos.close();//关闭资源动作要完成。 
}


}

===========================================

CopyMp3Test:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


public class CopyMp3Test {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


copy_4();

}
// 千万不要用,效率没有!
public static void copy_4() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\4.mp3");


int ch = 0;

while((ch =fis.read())!=-1){
fos.write(ch);
}

fos.close();
fis.close();
}


//不建议。 
public static void copy_3() throws IOException {
FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\3.mp3");

byte[] buf = new byte[fis.available()];//创建一个刚刚好的 数组。
fis.read(buf);
fos.write(buf);
fos.close();
fis.close();
}


public static void copy_2() throws IOException {//封装类的缓冲区。

FileInputStream fis = new FileInputStream("c:\\0.mp3");
BufferedInputStream bufis = new BufferedInputStream(fis); //利用缓冲区 ,提高效率

FileOutputStream fos = new FileOutputStream("c:\\2.mp3");
BufferedOutputStream bufos = new BufferedOutputStream(fos);//利用缓冲区 , 提高效率



int ch = 0;

while((ch=bufis.read())!=-1){//从缓冲区中一个一个取。
bufos.write(ch);
}

bufos.close();//关闭的时候应该是缓冲区。
bufis.close(); //关闭的时候应该是缓冲区。
}


public static void copy_1() throws IOException {//自定义缓冲区

FileInputStream fis = new FileInputStream("c:\\0.mp3");
FileOutputStream fos = new FileOutputStream("c:\\1.mp3");

byte[] buf = new byte[1024];

int len = 0;

while((len=fis.read(buf))!=-1){
fos.write(buf,0,len);
}

fos.close();
fis.close(); 
}


}

=======================================

键盘录入:

import java.io.IOException;
import java.io.InputStream;


/*
 * 读取一个键盘录入的数据,并打印在控制台上。
 * 
 * 键盘本身就是一个标准的输入设备。
 * 对于java而言,对于这种输入设备都有对应的对象。
 * 
 * 
 */
public class ReadKey {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


// readKey();
// System.out.println((int)'\r');
// System.out.println((int)'\n');

readKey2();

}


public static void readKey2() throws IOException {

/*
* 获取用户键盘录入的数据,
* 并将数据变成大写显示在控制台上,
* 如果用户输入的是over,结束键盘录入。

* 思路:
* 1,因为键盘录入只读取一个字节,要判断是否是over,需要将读取到的字节拼成字符串。
* 2,那就需要一个容器。StringBuilder.
* 3,在用户回车之前将录入的数据变成字符串判断即可。 

*/

//1,创建容器。
StringBuilder sb = new StringBuilder();

//2,获取键盘读取流。
InputStream in = System.in;//读取的字节流

//3,定义变量记录读取到的字节,并循环获取。
int ch = 0;

while((ch=in.read())!=-1){

// 在存储之前需要判断是否是换行标记 ,因为换行标记不存储。 
if(ch=='\r')
continue;
if(ch=='\n'){
String temp = sb.toString();
if("over".equals(temp))
break;
System.out.println(temp.toUpperCase());
sb.delete(0, sb.length());
}
else
//将读取到的字节存储到StringBuilder中。
sb.append((char)ch);

// System.out.println(ch);
}

}


public static void readKey() throws IOException {

InputStream in = System.in;

int ch = in.read();//阻塞式方法。没数据就就一直等,在控制台上等,这就是阻塞式。
System.out.println(ch);
int ch1 = in.read();//阻塞式方法。
System.out.println(ch1);
int ch2 = in.read();//阻塞式方法。
System.out.println(ch2);

// in.close();

// InputStream in2 = System.in;//流关了之后再new个对象就抛异常了,从系统过来这个流就一个,关了就没了,随着系统的关闭而关闭随着系统的出现而出现。所以这个流不需要关的,默认的输入和输出的流都不用关。
// int ch3 = in2.read();



}



}

\r\n的ARC码被读出来。


====================================================================================

字符流Reader的由来就是字节流加编码表:

字节流加编码表(解码)就是转换流:OuputStreamReader,InputStreamReader,

转换流:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;


public class TransStreamDemo {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


//字节流。 
InputStream in = System.in;
// int ch = in.read();
// System.out.println(ch);
// int ch1 = in.read();
// System.out.println(ch1);

//将字节转成字符的桥梁。装换流。 
InputStreamReader isr = new InputStreamReader(in);

// int ch = isr.read();
// System.out.println((char)ch);

//字符流。
BufferedReader bufr = new BufferedReader(isr);

OutputStream out = System.out;

OutputStreamWriter osw = new OutputStreamWriter(out);

BufferedWriter  bufw = new BufferedWriter(osw);



String line = null;

while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
// System.out.println(line.toUpperCase());
// osw.write(line.toUpperCase()+"\r\n");
// osw.flush();

bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}


}


}

转换流的过程图:


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;


public class TransStreamDemo2 {

/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


/*
* 1,需求:将键盘录入的数据写入到一个文件中。 



* 2,需求:将一个文本文件内容显示在控制台上。



* 3,需求:将一个文件文件中的内容复制到的另一个文件中。
*/

BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));

BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("b.txt")));


String line = null;

while((line=bufr.readLine())!=null){
if("over".equals(line))
break;

bufw.write(line.toUpperCase());
bufw.newLine();
bufw.flush();
}

}

}

================================================================


转换流:
InputStreamReader :字节到字符的桥梁。解码。
OutputStreamWriter:字符到字节的桥梁。编码。


流的操作规律:
之所以要弄清楚这个规律,是因为流对象太多,开发时不知道用哪个对象合适。


想要知道开发时用到哪些对象。只要通过四个明确即可。


1,明确源和目的(汇)
源:InputStream  Reader
目的:OutputStream  Writer


2,明确数据是否是纯文本数据。
源:是纯文本:Reader
否:InputStream
目的:是纯文本 Writer
否:OutputStream

到这里,就可以明确需求中具体要使用哪个体系。

3,明确具体的设备。
源设备:
硬盘:File
键盘:System.in
内存:数组
网络:Socket流

目的设备:
硬盘:File
控制台:System.out
内存:数组
网络:Socket流


4,是否需要其他额外功能。
1,是否需要高效(缓冲区);
是,就加上buffer.
2,转换。





需求1:复制一个文本文件。
1,明确源和目的。
源:InputStream Reader
目的:OutputStream  Writer
2,是否是纯文本?
是!
源:Reader
目的:Writer

3,明确具体设备。
源:
硬盘:File
目的:
硬盘:File

FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");

4,需要额外功能吗?
需要,需要高效。
BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));

================================================


需求2:读取键盘录入信息,并写入到一个文件中。

1,明确源和目的。
源:InputStream Reader
目的:OutputStream  Writer
2,是否是纯文本呢?
是,
源:Reader
目的:Writer
3,明确设备
源:
键盘。System.in
目的:
硬盘。File

InputStream in = System.in;
FileWriter fw = new FileWriter("b.txt");
这样做可以完成,但是麻烦。将读取的字节数据转成字符串。再由字符流操作。
4,需要额外功能吗?
需要。转换。 将字节流转成字符流。因为名确的源是Reader,这样操作文本数据做便捷。
所以要将已有的字节流转成字符流。使用字节-->字符 。InputStreamReader
InputStreamReader isr = new InputStreamReader(System.in);
FileWriter fw = new FileWriter("b.txt");

还需要功能吗?
需要:想高效。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new FileWriter("b.txt"));



===================================================

需求3:将一个文本文件数据显示在控制台上。
1,明确源和目的。
源:InputStream Reader
目的:OutputStream  Writer
2,是否是纯文本呢?
是,
源:Reader
目的:Writer
3,明确具体设备
源:
硬盘:File
目的:
控制台:System.out

FileReader fr = new FileReader("a.txt");
OutputStream out = System.out;//PrintStream
4,需要额外功能吗?
需要,转换。
FileReader fr= new FileReader("a.txt");
OutputStreamWriter osw = new OutputStreamWriter(System.out);
需要,高效。 
BufferedReader bufr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));

================================================================


需求4:读取键盘录入数据,显示在控制台上。
1,明确源和目的。
源:InputStream Reader
目的:OutputStream  Writer
2,是否是纯文本呢?
是,
源:Reader
目的:Writer
3,明确设备。
源:
键盘:System.in
目的:
控制台:System.out

InputStream in = System.in;
OutputStream out = System.out;

4,明确额外功能?
需要转换,因为都是字节流,但是操作的却是文本数据。
所以使用字符流操作起来更为便捷。
InputStreamReader isr = new InputStreamReader(System.in);
OutputStreamWriter osw = new OutputStreamWriter(System.out);

为了将其高效。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));


============================================================


5,将一个中文字符串数据按照指定的编码表写入到一个文本文件中.

1,目的。OutputStream,Writer
2,是纯文本,Writer。
3,设备:硬盘File 
FileWriter fw = new FileWriter("a.txt");
fw.write("你好"); 

注意:既然需求中已经明确了指定编码表的动作。
那就不可以使用FileWriter,因为FileWriter内部是使用默认的本地码表。
只能使用其父类。OutputStreamWriter.
OutputStreamWriter接收一个字节输出流对象,既然是操作文件,那么该对象应该是FileOutputStream

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName);

需要高效吗?
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt"),charsetName));


什么时候使用转换流呢?


1,源或者目的对应的设备是字节流,但是操作的却是文本数据,可以使用转换作为桥梁。
提高对文本操作的便捷。
2,一旦操作文本涉及到具体的指定编码表时,必须使用转换流 。

====================================================================

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


public class TransStreamDemo3 {


/**
* @param args
* @throws IOException 
*/
public static void main(String[] args) throws IOException {


readText_2();
}


public static void readText_2() throws IOException, FileNotFoundException {


InputStreamReader isr = new InputStreamReader(new FileInputStream("gbk_1.txt"),"utf-8");
char[] buf = new char[10];
int len = isr.read(buf);
String str = new String(buf,0,len);
System.out.println(str);

isr.close();
}


public static void readText_1() throws IOException {

FileReader fr = new FileReader("gbk_1.txt");

char[] buf = new char[10];
int len = fr.read(buf);
String str = new String(buf,0,len);
System.out.println(str);

fr.close();


}

public static void writeText_3() throws IOException {

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("u8_1.txt"),"UTF-8");

osw.write("你好");
osw.close();

}


public static void writeText_2() throws IOException {

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK");

// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk_3.txt"),"GBK");
// FileWriter fw = new FileWriter("gbk_1.txt");

/*
* 这两句代码的功能是等同的。 
* FileWriter:其实就是转换流指定了本机默认码表的体现。而且这个转换流的子类对象,可以方便操作文本文件。
*             简单说:操作文件的字节流+本机默认的编码表。
* 这是按照默认码表来操作文件的便捷类。

* 如果操作文本文件需要明确具体的编码。FileWriter就不行了。必须用转换流。 

*/


osw.write("你好");

osw.close();


}
public static void writeText_1() throws IOException {

FileWriter fw = new FileWriter("gbk_1.txt");

fw.write("你好");

fw.close();
}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值