读取文件数据
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("aaa.txt");
int b;
while((b = fis.read()) != -1) {
System.out.println(b);
}
fis.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("aaa.txt");
int x = fis.read();
System.out.println(x);
int y = fis.read();
System.out.println(y);
int z = fis.read();
System.out.println(z);
int a = fis.read();
System.out.println(a);
int b = fis.read();
System.out.println(b);
fis.close();
}
文件输出
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("bbb.txt");
fos.write(98);
fos.write(99);
fos.close();
}
将所有数据读入数组中
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("致青春.mp3");
FileOutputStream fos = new FileOutputStream("copy.mp3");
byte[] arr = new byte[fis.available()];
fis.read(arr);
fos.write(arr);
fis.close();
fos.close();
}
缓冲区
public static void demo1() throws FileNotFoundException, IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("致青春.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp3"));
int b;
while((b = bis.read()) != -1) {
bos.write(b);
}
bis.close();
bos.close();
}
中文处理
package cn.itcast.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo8_Chinese {
/**
* @param args
* @throws IOException
* 字节流只读中文是有弊端的,有可能会读到半个中文
* 解决方法有2
* 1,用字符流读(编码表+字节流)
* 2,将文件上的所有字节一次读到内存中,在内存中将所有字节转换成对应的字符串
* ByteArrayOutputStream
*
* 字节流写中文
* 在只写中文的时候必须转换成字节数组写出去
*
* 字节流可以拷贝任意类型的数据,因为所有的数据都是一字节的形式存在的
*/
public static void main(String[] args) throws IOException {
demo2();
}
public static void demo3() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
fis.close();
fos.close();
}
public static void demo2() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("bbb.txt");
fos.write("你好".getBytes());
fos.write("\r\n".getBytes());
fos.write("你好".getBytes());
fos.close();
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("bbb.txt");
byte[] arr = new byte[3];
int len;
while((len = fis.read(arr)) != -1) {
System.out.println(new String(arr,0,len));
}
fis.close();
}
}
AutoCloseable
package cn.itcast.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo9_IOException {
/**
* @param args
* @throws IOException
* 在try()中创建的流对象必须实现了AutoCloseable这个接口,如果实现了,在try后面的{}(读写代码)执行后就会自动调用
* 流对象的close方法将流关掉
*/
public static void main(String[] args) throws IOException {
try(
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
MyClose mc = new MyClose();
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
}
public static void demo1() throws FileNotFoundException, IOException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("aaa.txt");
fos = new FileOutputStream("bbb.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
} finally {
try {
if(fis != null)
fis.close();
}finally {
if(fos != null)
fos.close();
}
}
}
}
class MyClose implements AutoCloseable {
public void close() {
System.out.println("我关了");
}
}