IO流分类
- 基于字节操作的I/O流:InputStream和OutputStream
- 基于字符操作的I/O流:Reader和Writer
- 基于磁盘操作的I/O流:File
- 基于网络操作的I/O流:Socket
一般我们前两种比较常用
File基本操作
public static void main(String[] args) throws IOException{
File file = new File("d:/python");
file.mkdir(); //文件夹不存在就建立该文件夹
/*
mkdir()与mkdirs()区别是前者建立不存在的文件夹必须要求
父路径存在,比如“d:/java/app”要建立app文件夹前提是jav
a必须存在,否则报错;而mkdirs()方法不用。*/
if(file.exists()&&file.isDirectory()){
System.out.println("python存在");
File file1 = new File("d:/python/spider.txt");
File file2 = new File("d:/python/user.txt");
file1.createNewFile();
file2.createNewFile();
File[] files = file.listFiles();
for(File f:files){
System.out.println("python目录下的文件名:"+f.get
Name());
System.out.println("python目录下文件的绝对路径
:"+f.getAbsolutePath());
}
else{
System.out.println("python目录不存在");
}
}
字节流基本操作
public static void main(Strin[] args)throws Exception{
//必须处理IO流的异常,不然报错!
FileOutputStream wos = new FileOutputStream("D:/abc.txt");
//如果没有找到文件,会自动创建
String content = "I love java";
byte[] array = content.getBytes();
//字符流以字节为单位写入,将String转换为byte型
try{
wos.write(array);
}
catch(IOException e){
System.out.println(e);
}
FileInputStream ios = new FileInputStream("D:/abc.txt");
try{
int len = 0;
byte[] array1 = new byte[2];
//每次读取两个字节
while((len=ios.read(array1)!=-1){
System.out.println(array1);
//依次输出"[B@15db9742"
}
}
catch(IOException e){
System.out.println(e);
}
}
编码:
计算机只能识别二进制数据,以字节为单位;文本数据与二进制数据的转换就是编码和解码的过程;编码表是由字符和其相应的数值组成的,所以字符流=字节流+编码表
String s = "你好";
//String -- byte[]
byte[] bys = s.getBytes(); // [-60, -29, -70, -61]
byte[] bys = s.getBytes("GBK");// [-60, -29, -70, -61]
byte[] bys = s.getBytes("UTF-8");// [-28, -67, -96, -27, -91, -67]
字符流基本操作
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
public class io {
public static void main(String args[]) throws IOException{
OutputStreamWriter ows = new OutputStreamWriter(new
FileOutputStream("D:/a.txt"));
String s = "Troye Sivan";
ows.write(s);
ows.close();
//操作同一文件必须先关闭输出流
InputStreamReader iws = new InputStreamReader(new
FileInputStream("D:/a.txt"));
char[] r = new char[1];
int len=0;
while((len=iws.read(r))!=-1) {
System.out.println(r);
}
iws.close();
}
}
简化写法
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class io1 {
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("D:/a.txt");
FileWriter fw = new FileWriter("D:/abc.txt");
char[] ch = new char[2];
int len=0;
while((len=fr.read(ch))!=-1) {
fw.write(ch,0,len);
fw.flush();
}
fr.close();
fw.close();
}
}
缓冲流
提高读写效率
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
public class io_buffer {
public static void main(String[] args) throws IOException{
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/abc.txt"));
//BufferedWriter bw = new BufferedWriter(new
// OutputStreamWriter(new FileOutputStream("D:/abc.txt")));
bw.write("ZAYN");
bw.write("MALIK");
bw.write("LET ME");
bw.flush();
bw.close();
BufferedReader br = new BufferedReader(new FileReader("D:/abc.txt"));
//BufferedReader br = new BufferedReader(new
// InputStreamReader(new FileInputStream("D:/abc.txt"));
char[] ch = new char[2];
int len=0;
while((len=br.read(ch))!=-1) {
System.out.println(new String(ch,0,len));
}
br.close();
}
}
总结:
-
java规定IO流必须要处理异常,否则会报错
-
要从文件中读写数据,就要使用File类或者它的子类