您的位置:首页 > 职场人生

黑马程序员—IO概述

2015-10-06 21:53 429 查看
——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

IO概述

IO流用来处理设备之间的数据传输。

Java对数据的操作是通过流的方式。

Java用于操作流的对象都在IO包中。

IO分类:

流按操作数据分为两种:字节流与字符流。

注意:其实计算机里的所有数据都是以字节的形式体现的,计算机可以直接识别字节。但是作为人类的程序员更擅长操作字符,所以后期产生了字符流 。

流按流向分为:输入流(input),输出流(output)。

相对路径与绝对路径:

绝对路径:从盘符开始指定的物理路径。是真正文件在硬盘上的位置。

相对路径:相对于某个指定位置的路径。

如c:/a/b/c.txt文件。

相对于c:的路径为a/b/c.txt

c.txt相对于c:/a/b的路径为直接的c .txt

代码中Windows中路径分隔可使用/或者\。

InputStream:此抽象类是表示字节输入流的所有类的超类。

FileInputStream: InputStream的子类 从文件系统中的某个文件中获得输入字节。

构造:

public FileInputStream(String name) throws FileNotFoundException 通过文件字符串的名称,从一个文件中读

public FileInputStream(File file) throws FileNotFoundException 通过文件对象,从一个文件中读

普通方法:

public int read() throws IOException 一次读取一个字节 返回为读取到的那个字节 如果读取到文件末位,返回-1

public int read(byte[] b)throws IOException 一次读取一个字节组

byte[] b:最终读取到的内容存放的

返回值:本次读到的字节个数

代码实例

public class Demo02_InputStream {

public static void main(String[] args) throws IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//读入或者写出

/*
*
一次读取一个字节
//System.out.println(fis.read());
//定义变量,用于临时存储读取到的字节
//      int b;
//
//      while((b=fis.read())!=-1) {
//          System.out.println((char)b);
//      }
*
*/
//定义变量,用于临时存储读取到的字节
byte[] bytes = new byte[3];
//定义变量,记录本次读取到的字节个数,用于判断循环是否结束
int len;

while((len=fis.read(bytes))!=-1) {

System.out.println("本次读取到"+len+"个字节");
System.out.println("本次读取到的内容是:"+Arrays.toString(bytes));

String s = new String(bytes,0,len);
System.out.println(s);
}

//关闭流
fis.close();

}

}


代码实例 把一个文件复制到另一个文件中去

public class Demo03_copyFile {

public static void main(String[] args) throws IOException {

method();
method2();
}
//一次复制一个字节数组
public static void method2() throws IOException{

//本次采取一次一个字节数组的方式

//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");

//先读
//定义变量,用于临时存储读到的字节
byte[] bytes = new byte[3];
//定义变量,用于临时存储本次读取到的字节个数
int len;

while((len=fis.read(bytes))!=-1) {
//后写
fos.write(bytes,0,len);
}

//关闭流
fos.close();
fis.close();
}

//一次复制一个字节
public static void method() throws IOException{

//本次采取一次一个字节的方式

//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("b.txt");

//先读
//定义变量,用于临时存储读到的字节
int b;
while((b=fis.read())!=-1) {
//后写
fos.write(b);
}

//关闭流
fos.close();
fis.close();
}
}


OutputStream:此抽象类是表示输出字节流的所有类的超类。

FileOutputStream: OutputStream的子类 文件输出流是用于将数据写入 File的输出流。

构造:

public FileOutputStream(String name) throws FileNotFoundException 通过文件字符串的名称,写出到一个文件中

public FileOutputStream(File file)throws FileNotFoundException 通过文件对象,写出到一个文件中

普通方法:

public void write(int b) throws IOException 一次输出一个字节

public void write(byte[] b) throws IOException 一次输出一个字节数组

public void write(byte[] b, int off,int len) throws IOException 一次输出一个字节数组的一部分

public void close() throws IOException 关闭流

代码实例

public class Demo01_OutputStream {

public static void main(String[]
4000
args) throws IOException {

//创建流对象
FileOutputStream fos = null;
fos = new FileOutputStream("a.txt");

//      File file = new File("a.txt");
//      FileOutputStream fos2 = new FileOutputStream(file);
//写出或读取数据
fos.write(97);

String s = "i love java";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));

fos.write(bytes);

fos.write(bytes,2,2);
fos.close();
}
}


BufferedOutputStream: 高效字节输出流 缓冲字节输出流

BufferedInputStream: 高效字节输入流 缓冲字节输入流

public void flush() throws IOException 刷新缓冲区

使用带缓冲区的流是写出到缓冲区中,如果想写到文件中,需要将缓冲区的内容刷新到文件中

close方法在真正关闭之前,先刷新缓冲区

构造方法:

public BufferedOutputStream(OutputStream out)

public BufferedInputStream(InputStream in)

高效流的构造方法,不直接关联文件,而是使用了对应的普通流对象

代码实例

public class Demo02_Buffer {

public static void main(String[] args) throws IOException {

//高效输出流
//创建流对象
OutputStream os = new FileOutputStream("a/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);

//写出
//      os.write('a');
bos.write("我虔诚地热爱着java".getBytes());
//      bos.flush();

//关闭流
bos.close();

//高效输入流
InputStream is = new FileInputStream("a/a.txt");
BufferedInputStream bis = new BufferedInputStream(is);

//      //一次一个字节
//      int b;
//      while((b=bis.read())!=-1) {
//          System.out.println((char)b);
//      }

//一次一个字节数组
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1) {
System.out.println(new String(bytes,0,len));
}

bis.close();
}

}


Writer:字符输出流根类

FileWriter:用来写入字符文件的便捷类。

普通方法:

public void write(int c) throws IOException 一次写出一个字符

public void write(char[] cbuf) throws IOException 一次写出一个字符数组

public void write(char[] cbuf,int index,int len) throws IOException 一次写出一个字符数组的一部分

public void write(String str) throws IOException  一次写出一个字符串


代码实例

public class Demo03_Writer {

public static void main(String[] args) throws IOException {

//创建流对象
FileWriter fw = new FileWriter("c.txt");
//写出数据
fw.write('中');
fw.write('a');
fw.write(97);

char[] chars = new char[]{'a','b','c'};
fw.write(chars);
fw.write(chars, 0, 2);

fw.write("我爱Java");
//      fw.flush();
//关闭流
fw.close();
}

}


Reader:字符输入流根类

FileReader:用来读入字符文件的便捷类。

普通方法:

public int read() throws IOException 一次读取一个字符

public int read(char[] cbuf) throws IOException 一次读取一个字符数组

代码实例

public class Demo04_Reader {

public static void main(String[] args) throws IOException {

//创建IO流对象
FileReader fr = new FileReader("c.txt");
//读取
//      //一次读取一个字符
//      int c;
//      while((c=fr.read())!=-1) {
//          System.out.println((char)c);
//      }

//一次读取一个字符数组
char[] chars = new char[1024];
int len;
while((len=fr.read(chars))!=-1) {
System.out.println(chars);
System.out.println(new String(chars, 0, len));
}

//关闭流
fr.close();
}

}


转换流

InputStreamReader isr = new InputStreamReader(System.in);

InputStreamReader isr = new InputStreamReader(new FileInputStream(…));

OutputStreamWriter osw = new OutputStreamWriter(System.out);

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(…));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java