一、IO流:
IO流用来处理设备之间的数据传输(上传和下载文件)
二、分类:
1、 按照数据流向:
输入流:读入数据
输出流:写出数据
2、 按照数据类型:
(1)字节流
字节输入流 读入数据 InputStream
字节输出流 写出数据 OutputStream
(2)字符流
字符输入流 读取数据 Reader
字符输出流 写出数据 Writer
举个栗子:在文本文件中输出“hello world”
字节输出流操作步骤:
(1)创建字节输出流对象
(2)写数据
(3)释放资源
package Demo;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("f.txt");
fos.write("hello world".getBytes());
//释放资源
fos.close();
}
}
3、换行符号:
Windows:\r\n
Linux:\n
Mac:\r
4、追加写入:
FileOutputStream(String name, boolean append) :append为真则为追加内容
改进后的标准异常代码:
package Demo;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo2 {
public static void main(String[] args) {
FileOutputStream fs = null;
try {
fs = new FileOutputStream("f1.txt");
fs.write("hello io".getBytes());
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字节流读取数据:
package Demo;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("f.txt");
int b = 0;
while((b = fis.read()) != -1) {
System.out.print((char)b);
}
fis.close();
}
}
一次读取一个字节数组:
package Demo;
import java.io.FileInputStream;
import java.io.IOException;
public class InputDemo {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("f2.txt");
byte[] b = new byte[1024];
int len = 0;
while((len = fis.read(b)) != -1) {
System.out.print(new String(b , 0 , len));
}
fis.close();
}
}
字节流复制文本文件:
package Demo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyDemo {
public static void main(String[] args) throws IOException {
//封装数据源
FileInputStream fis = new FileInputStream("f.txt");
//封装目的地
FileOutputStream fos = new FileOutputStream("f2.txt");
int b = 0;
while((b = fis.read()) != -1) {
fos.write(b);
}
//释放资源
fis.close();
fos.close();
}
}
5、Java也考虑到效率的问题,就提供了带缓冲区的字节类,成为缓冲区类
写数据:BufferedOutputStrem
读数据:BufferedInputStream
写数据:
package Demo;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedOutput {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt"));
bos.write("hello java".getBytes());
bos.close();
}
}
读数据:
package Demo;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferedInput {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("bos.txt"));
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1) {
System.out.print(new String(b , 0, len));
}
bis.close();
}
}