java入门10- IO基础

来源:互联网 发布:岁寒然后知松柏下一句 编辑:程序博客网 时间:2024/06/10 20:17

1、流的概念

流:数据在文件和程序(内存)之间经历的路径
输入流:数据从文件到程序(内存)的路径
输出流:数据从程序(内存)到文件的路径
按流的方向不同
输入流、输出流
按处理数据的单位不同
字节流、字符流
按功能不同
节点流、处理流

2、文件操作

java.io包中的File类

1.File 变量名 = new File(String pathname);

String rootPath="C:\\Windows\\debug";File f = new File(rootPath);

路径中用”\”表示,要有两个;
路径中用”/”表示,要有一个;
2、File 变量名 = new File(URI uri);

3、File 变量名 = new File(String parent, String child);
File f3 = new File(“d:/temp”, ”abc.txt”);

两种路径写法都能执行。
4、File 变量名 = new File(File parent, String child);

根据parent抽象路径名和child路径名字符串创建一个新File实例

File f = new File(“d:/temp”);File f4 = new File (f, ”abc.txt”);

3、File类的常用方法

方法 含义 String getName() 得到一个文件的名称(不包括路径) Sting getPath() 得到一个文件的相对路径名 String getAbsolutePath() 得到一个文件的绝对路径名 boolean exists() 判断指定的文件或目录是否存在 boolean mkdir() 创建文件对象指定的目录(单层目录) boolean mkdirs() 创建文件对象指定的目录(多层目录) boolean isDirectory() 判断指定的文件是否为目录 boolean isFile() 判断指定的文件是否为文件 long length() 获取文件的长度,以字节为单位 File[] listFiles() 返回目录中所有文件 String[] list() 返回目录中所有文件名字符串,包含文件目录 boolean createNewFile() 创建文件对象指定的文件

4、字节流

InputStream和OutputStream分别是字节输入流和字节输出流的超类
InputStream和OutputStream是抽象类
InputStream 继承关系
  -FileInputStream
  -ObjectInputStream
  -FilterInputStream
    -BufferedInputStream
    -DataInputStream
    
int read() //读一个字节返回
int read(byte[]) //将数据读入byte[], 返回读的字节数,返回-1表示读完。
int read(byte[], int offset, int length)
void close() //关闭此输入流并释放与该流关联的所有系统资源

FileInputStream的构造方法

1.public FileInputStream(String name) throws FileNotFoundException2.public FileInputStream(File file) throws FileNotFoundException

OutputStream 继承关系
  -FileOutputStream
  -FilterOutputStream
   -BufferedOutputStream
   -DataOutputStream
   
void write(int) //写一个字节
void write(byte[]) //写一个字节数组
void write(byte[], int offset, int length)
void close() //关闭此输出流并释放与此流有关的所有系统资源
void flush() //刷新此输出流并强制写出所有缓冲的输出字节

FileOutputStream的构造方法是:

  1. public FileOutputStream(String name) throws FileNotFoundException
  2. public FileOutputStream(String.name,Boolean append) throws FileNotFoundException
  3. public FileOutputStream(File file) throws FileNotFoundException

字节文件输入输出流
1. 创建文件输入输出流的对象
2. 打开文件
3. 用文件读写方法读写数据
4. 关闭数据流。

        // 创建文件对象        File f = new File("D:\\demo\\test1.txt");        // 创建输入文件流对象        FileInputStream is = null;        is = new FileInputStream(f);        byte[] bytes = new byte[1024];        int n;        while ((n = is.read(bytes)) != -1) {            String str = new String(bytes, 0, n);            System.out.println(str);            }
方法 含义 BufferedInputStream(InputStream in) 创建一个带32字节缓冲区的缓冲输入流 BufferedInputStream(InputStream in, int size) 创建一个带size大小缓冲区的缓冲输入流 BufferedOutputStream(OutputStream out) 创建一个带32字节缓冲区的缓冲输出流 BufferedOutputStream(OutputStream out, int size) 创建一个带size大小缓冲区的缓冲输出流

5、字符流

Reader和Writer是字符输入输出流的抽象超类,
Reader 
 -BufferedReader
 -InputStreamReader
  -FileReader
int read(); //读单个字符
int read(char cbuf[]); //读字符放入数组中,返回-1表示读取结束
int read(char cbuf[], int offset, int length); //读字符放入数组的指定位置
void close(); //关闭此Reader并释放与其关联的所有系统资源

public FileReader(String filename)
public FileReader(File file)

Writer
 -OutputStreamReader
  -FileWriter
 -BufferedWriter
int write(int c) ; // 写单个字符
int write(char cbuf[]) ;// 写字符数组
int write(char cbuf[], int offset, int length) ;

public FlieWriter(String filename)
public Filewriter(File file)

字符文件输入输出流

  1. 先创建对象打开文件
  2. 然后用读写方法从文件中读取数据或将数据写入文件
  3. 最后关闭数据流
        FileReader fr = null;        fr = new FileReader("D:\\demo\\test1.txt");        char[] c = new char[1024];        int n;        while ((n = fr.read(c)) != -1) {            String str = new String(c, 0, n);            System.out.println(str);            }
方法 含义 BufferedReader (Reader in) 创建一个默认大小缓冲区的缓冲输入流 BufferedReader (Reader in, int size) 创建一个带size大小缓冲区的缓冲输入流 BufferedWriter(Writer out) 创建一个默认大小缓冲区的缓冲输出流 BufferedWriter(Writer out, int size) 创建一个带size大小缓冲区的缓冲输出流

BufferedReader提供了readLine方法用于读取一行字符串(以\r或\n分隔)
BufferedWriter提供了newLine方法用于写入一个行分隔符
ag.

BufferedReader reader = new BufferedReader(new FileReader(inFileName));BufferedWriter writer = new BufferedWriter(new FileWriter(outFileName));
原创粉丝点击