IO是什么?
I:input 输入流
O:output 输出流
输入输出是相对内存而言,向内存中去是输入,从内存出来是输出
字节流:有的流按照字节读取,每次取1byte,相当于8个二进制位,可以称为万能流,可以读文本、图片、声音、视频。
字符流:有的流按照字符读取方便txt文本读取,一次一个字符,只能读取文本文件,不能读取word文件
Stream结尾是字节流,Read/Writer结尾的是字符流 都在java.IO.*包下
重点掌握java.io.FileInputStream java.io.FileOutputStream ,其他的非常相似
输出文件内容
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamText {
public static void main(String[] args) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("src/IO/a.txt");
byte[] bytes = new byte[1024];
int count=0;
while (( count = inputStream.read(bytes)) != -1){
System.out.print(new String(bytes,0,count));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream == null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制文件
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream("src/IO/a.txt");
fos=new FileOutputStream("src/IO/b.txt");
byte[] bytes =new byte[1024];
int count =0 ;
while (( count= fis.read(bytes)) != -1){
fos.write(bytes,0,count);
}
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fis == null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos == null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferReader
package IO;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Stream;
public class BufferReader {
public static void main(String[] args) {
FileReader fileReader = null;
BufferedReader bufferedReader=null;
try {
fileReader=new FileReader("src/IO/a.txt");
bufferedReader = new BufferedReader(fileReader);
String s=null;
while ( (s = bufferedReader.readLine()) != null){
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader == null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
InputStreamReaderTest 字节流转字符流
package IO;
import java.io.*;
import java.util.stream.Stream;
public class InputStreamReaderTest {
public static void main(String[] args) {
try {
//字节流
FileInputStream fileInputStream = new FileInputStream("src/IO/a.txt");
//字节流转字符流
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
// 字符流
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lines = bufferedReader.readLine();
System.out.println(lines);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}