OutputStream:字节输入
InputStream:字节读取
InputStream的三种读取方式如下
int available():此方法返回int类型,表示读取字节的长度。
import java.io.*;
class FileOutputStreamDemo
{
public static void main(String[] args) throws IOException
{
readFile_03();
}
public static void readFile_03() throws IOException
{
FileInputStream fis = new FileInputStream("fos.txt");
byte[] buf = new byte[fis.available()]; //定义一个刚刚好的缓冲区。不用再循环了。
fis.read(buf);
System.out.println(new String(buf));
fis.close();
}
public static void readFile_02() throws IOException
{
FileInputStream fis = new FileInputStream("fos.txt");
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf)) != -1)
{
System.out.println(new String(buf,0,len));
}
fis.close();
}
public static void readFile_01() throws IOException
{
FileInputStream fis = new FileInputStream("fos.txt");
int ch = 0;
while((ch = fis.read()) != -1)
{
System.out.println((char)ch);
}
fis.close();
}
public static void writeFile() throws IOException
{
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write("abcde".getBytes());
fos.close();
}
}