在java.io包中还有许多其他的流,主要是为了提高性能和使用方便。C/C++只能提供字节流。Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种所以一共四个):InputStream,OutputStream,Reader,Writer。Java中其他多种多样变化的流均是由它们派生出来的.
字符流和字节流是根据处理数据的不同来区分的。字节流按照8位传输,字节流是最基本的,所有文件的储存是都是字节(byte)的储存,在磁盘上保留的并不是文件的字符而是先把字符编码成字节,再储存这些字节到磁盘。
1.字节流可用于任何类型的对象,包括二进制对象,而字符流只能处理字符或者字符串;
2. 字节流提供了处理任何类型的IO操作的功能,但它不能直接处理Unicode字符,而字符流就可以。
读文本的时候用字符流,例如txt文件。读非文本文件的时候用字节流,例如mp3。理论上任何文件都能够用字节流读取,但当读取的是文本数据时,为了能还原成文本你必须再经过一个转换的工序,相对来说字符流就省了这个麻烦,可以有方法直接读取。
字符流处理的单元为2个字节的Unicode字符,分别操作字符、字符数组或字符串,而字节流处理单元为1个字节, 操作字节和字节数组。所以字符流是由Java虚拟机将字节转化为2个字节的Unicode字符为单位的字符而成的,所以它对多国语言支持性比较好!
1.字节流:继承于InputStream / OutputStream。
OutputStream提供的方法:
void write(int b):写入一个字节的数据
void write(byte[] buffer):将数组buffer的数据写入流
void write(byte[] buffer,int offset,int len):从buffer[offset]开始,写入len个字节的数据
void flush():强制将buffer内的数据写入流
void close():关闭流
InputStream提供的方法:
int read():读出一个字节的数据,如果已达文件的末端,返回值为-1
int read(byte[] buffer):读出buffer大小的数据,返回值为实际所读出的字节数
int read(byte[] buffer,int offset,int len)
int available():返回流内可供读取的字节数目
long skip(long n):跳过n个字节的数据,返回值为实际所跳过的数据数
void close():关闭流
2.字符流,继承于InputStreamReader / OutputStreamWriter。
字符流的类:1),BufferedReader是一种过滤器(filter)(extends FilterReader)。过滤
器用来将流的数据加以处理再输出。构造函数为:
BufferedReader(Reader in):生成一个缓冲的字符输入流,in为一个读取器
BufferedReader(Reader in,int size):生成一个缓冲的字符输入流,并指定缓冲区的大小为size
public class IOStreamDemo {
public void samples() throws IOException { //1. 这是从键盘读入一行数据,返回的是一个字符串
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
//2. 这是从文件中逐行读入数据
BufferedReader in = new BufferedReader(new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "/n";
in.close();
//3. 这是从一个字符串中逐个读入字节
StringReader in1 = new StringReader(s2);
int c;
while((c = in1.read()) != -1)
System.out.print((char)c);
//4. 这是将一个字符串写入文件
try {
BufferedReader in2 = new BufferedReader(new StringReader(s2));
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in2.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
} }
对于上面的例子,需要说明的有以下几点:
1. InputStreamReader是InputStream和Reader之间的桥梁,由于System.in是字节流,需要用它来包装之后变为字符流供给BufferedReader使用。
3. PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out")));
这句话体现了Java输入输出系统的一个特点,为了达到某个目的,需要包装好几层。首先,输出目的地是文件IODemo.out,所以最内层包装的是FileWriter,建立一个输出文件流,接下来,我们希望这个流是缓冲的,所以用BufferedWriter来包装它以达到目的,最后,我们需要格式化输出结果,于是将PrintWriter包在最外层。
Java流有着另一个重要的用途,那就是利用对象流对对象进行序列化。
在一个程序运行的时候,其中的变量数据是保存在内存中的,一旦程序结束这些数据将不会被保存,一种解决的办法是将数据写入文件,而Java中提供了一种机制,它可以将程序中的对象写入文件,之后再从文件中把对象读出来重新建立。这就是所谓的对象序列化。Java中引入它主要是为了RMI(Remote Method Invocation)和Java Bean所用,不过在平时应用中,它也是很有用的一种技术。
例子: int array[]={4,7,9,7,4,5} 将其下入文件temp.dat中再读出了,进行升序排列,并打印到控制台
public class outputFile
{
public static void writeFile(int intArr[])
{
OutputStreamWriter ow = null;
PrintWriter witer = null;
ByteArrayOutputStream bos = null;
try
{
bos = new ByteArrayOutputStream();
ow = new OutputStreamWriter(bos);
witer = new PrintWriter(new BufferedWriter(ow));
for (int i = 0; i < intArr.length; i++)
{
witer.print(String.valueOf(intArr[i]));
witer.print(",");
}
witer.flush();
witer.close();
OutputStream os = new FileOutputStream(getPath());
bos.writeTo(os);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (ow != null)
{
ow.close();
ow = null;
}
if (witer != null)
{
witer.close();
witer = null;
}
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public static String readFile()
{
FileInputStream fs = null;
BufferedReader br = null;
StringBuffer buf = new StringBuffer();
try
{
fs = new FileInputStream(getPath());
br = new BufferedReader(new InputStreamReader(fs));
String line;
while ((line = br.readLine()) != null)
{
buf.append(line);
}
fs.close();
br.close();
}
catch (Exception e)
{
try
{
if (fs != null)
{
fs.close();
fs = null;
}
if (br != null)
{
br.close();
br = null;
}
}
catch (Exception e1)
{
e1.printStackTrace();
}
e.printStackTrace();
}
return buf.toString();
}
public static String getPath()
{
String path = "c://aa.txt";
return path;
}
public static int[] sort(String str)
{
String[] strArr = str.split(",");
int len = strArr.length;
int[] intArr = new int[len];
for (int i = 0; i < len; i++)
{
intArr[i] = Integer.valueOf(strArr[i]).intValue();
}
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (intArr[i] < intArr[j])
{
int temp = intArr[i];
intArr[i] = intArr[j];
intArr[j] = temp;
}
}
}
return intArr;
}
public static void display(int[] a)
{
int len = a.length;
for (int i = 0; i < len; i++)
{
System.out.print(a[i]);
System.out.print(",");
}
}
public static void main(String[] ags)
{
// int intArr[] = { 1, 4, 5, 2, 9, 3 };
// //write
// writeFile(intArr);
// //read
// String context = readFile();
// //sort
// int[] a = sort(context);
// //display
// display(a);
boolean b = true;
System.out.println(String.valueOf(b));
}
}