I/O分类:
从不同的角度分类:
按数据流的方向不同可以分为 输入流 和 输出流。
按处理数据单位不同可以分为 字节流 和 字符流。
按照功能不同可以分为 节点流 和 处理流
所有的流类型位于java.io内都分别继承以下四种抽象类;
字节流 字符流
输入流 :InputStream Reader
输出流 :OutputStream Writer
I/O类中以Stream结尾的都是字节流,Reader/Writer结尾的都是字符流。
访问文件:FileInputStream与FileOutputStream
FileInputStream和FileOutputStream分别继承自InputStream和OutputStream用于向文件中输入和输出字节。
构造方法:
FileInputStream(String name)
throws
FileNotFountException
FileInputStream(File file) throws FileNotFountException
FileOutputStream(String name) throws FileNotFountException
FileOutputStream(File file) throws FileNotFountException
FileOutputStream(File file, boolean append) throws FileNotFountException
FileInputStream(File file) throws FileNotFountException
FileOutputStream(String name) throws FileNotFountException
FileOutputStream(File file) throws FileNotFountException
FileOutputStream(File file, boolean append) throws FileNotFountException
FileInputStream和FileOutputStream类支持其父类InputStream和OutputStream所提供的数据读写方法。
在实例化FileInputStream和FileOutputStream流时要用try-catch语句处理其可能抛出的FileNotFoundException,
在读写数据时也要用途try-catch语句处理可能抛出的IOException。
FileInputStream类的使用实例:
public
class
TestFileInputStream{
public static void main(String[] args){
int b = 0 ;
FileInputStream in = null ;
try {
in = new FileInputStream( " c:/TestFileInputStream.java " );
} catch (FileNotFoundException e){
System.out.println( " 找不到指定文件 " );
System.exit( - 1 );
}
try {
long num = 0 ;
while ((b = in.read()) !=- 1 ){ // read()方法读取一个字节,返回类型是int
System.out.print(( char )b); // read()方法读取中文字符时是乱码,因为中文是两个字节的。
num ++ ;
}
in.close();
System.out.println();
System.out.println( " 共读取了 " + num + " 个字节 " );
} catch (IOException e1){
System.out.println( " 文件读取错误 " );
System.exit( - 1 );
}
}
}
public static void main(String[] args){
int b = 0 ;
FileInputStream in = null ;
try {
in = new FileInputStream( " c:/TestFileInputStream.java " );
} catch (FileNotFoundException e){
System.out.println( " 找不到指定文件 " );
System.exit( - 1 );
}
try {
long num = 0 ;
while ((b = in.read()) !=- 1 ){ // read()方法读取一个字节,返回类型是int
System.out.print(( char )b); // read()方法读取中文字符时是乱码,因为中文是两个字节的。
num ++ ;
}
in.close();
System.out.println();
System.out.println( " 共读取了 " + num + " 个字节 " );
} catch (IOException e1){
System.out.println( " 文件读取错误 " );
System.exit( - 1 );
}
}
}
FileOutputStream使用实例:
import
java.io.
*
;
public class TestFileOutputStream{
public static void main(String[] args){
int b = 0 ;
FileInputStream in = null ;
FileOutputStream out = null ;
try {
in = new FileInputStream( " c:/TestFileInputStream.java " );
out = new FileOutputStream( " c:/test.txt " ); // 没有的时候自动创建文件
while ((b = in.read()) != - 1 ){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2){
System.out.println( " 找不到指定文件 " );
System.exit( - 1 );
} catch (IOException e1){
System.out.println( " 文件复制错误 " );
System.exit( - 1 );
}
System.out.println( " 文件已复制 " );
}
}
public class TestFileOutputStream{
public static void main(String[] args){
int b = 0 ;
FileInputStream in = null ;
FileOutputStream out = null ;
try {
in = new FileInputStream( " c:/TestFileInputStream.java " );
out = new FileOutputStream( " c:/test.txt " ); // 没有的时候自动创建文件
while ((b = in.read()) != - 1 ){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2){
System.out.println( " 找不到指定文件 " );
System.exit( - 1 );
} catch (IOException e1){
System.out.println( " 文件复制错误 " );
System.exit( - 1 );
}
System.out.println( " 文件已复制 " );
}
}
FileReader和FileWriter
FileReader、FileWriter与上面两个类的方法基本相同,只是在后两者里,读取和写入的是字符,即两字节。
FileReader例子:
import
java.io.
*
;
public class TestFileReader{
public static void main(String[] args){
FileReader r = null ;
int c;
try {
r = new FileReader( " c:/TestFileReader.java " );
while ((c = r.read()) != - 1 ){ // read()方法读取字符返回int类型
System.out.print(( char )c);
}
r.close();
} catch (FileNotFoundException e1){
System.out.println( " 找不到指定文件 " ); // 中文不会出现乱码
} catch (IOException e2){
System.out.println( " 文件读取错误 " );
}
}
}
public class TestFileReader{
public static void main(String[] args){
FileReader r = null ;
int c;
try {
r = new FileReader( " c:/TestFileReader.java " );
while ((c = r.read()) != - 1 ){ // read()方法读取字符返回int类型
System.out.print(( char )c);
}
r.close();
} catch (FileNotFoundException e1){
System.out.println( " 找不到指定文件 " ); // 中文不会出现乱码
} catch (IOException e2){
System.out.println( " 文件读取错误 " );
}
}
}
FileWriter例子:
import java.io.*;
public
class
TestFileWriter{
public static void main(String[] args){
FileWriter fw = null ;
try {
fw = new FileWriter( " c:/unicode.txt " );
for ( int i = 0 ;i < 5000 ;i ++ ){
fw.write(i); // 写字符,两个字节
}
fw.close();
} catch (IOException e1){
e1.printStackTrace();
System.out.println( " 文件写入错误 " );
System.exit( - 1 );
}
System.out.println( " 文件写入成功 " );
}
public static void main(String[] args){
FileWriter fw = null ;
try {
fw = new FileWriter( " c:/unicode.txt " );
for ( int i = 0 ;i < 5000 ;i ++ ){
fw.write(i); // 写字符,两个字节
}
fw.close();
} catch (IOException e1){
e1.printStackTrace();
System.out.println( " 文件写入错误 " );
System.exit( - 1 );
}
System.out.println( " 文件写入成功 " );
}
}