一、Java IO
1.1字节流、字符流
- 其中,字节流也有两个抽象类:InputStream OutputStream
其对应子类有FileInputStream和FileOutputStream实现文件读写
BufferedInputStream和BufferedOutputStream提供缓冲区功能 - 同样,字符流有两个抽象类:Writer Reader 其对应子类FileWriter和FileReader可实现文件的读写操作 BufferedWriter和BufferedReader能够提供缓冲区功能,用以提高效率
FileInputStream Demo
其中新建了一个byte数组用来保存每次读取出来的数据,而c用来保存read方法的返回值,返回值为放入byte数组中字节的个数。
public static void main(String[] args) throws IOException{
File file = new File("./src/resourses/file.txt");
System.out.println(file.exists());
FileInputStream in = new FileInputStream("./src/resourses/file.txt");
int c;
byte[] b = new byte[1024];
while ((c = in.read(b,0,b.length))!=-1){ // 将流中的数据读入放在byte数组的第off个位置先后的len个位置中
// 返回值为放入字节的个数。
System.out.println(new String(b,0,c));
}
in.close();
}
FileOutputStream Demo
public static void main(String[] args) {
try{
FileInputStream is = new FileInputStream("./src/resourses/file.txt");
FileOutputStream os = new FileOutputStream("./src/resourses/write.txt");
byte[] b =