JAVA_IO流总结一
流的概念划分:
按照流的方向划分:输入流和输出流;
按照处理数据单元划分:字节流和字符流。
字节流和字符流的区别:
字节流读取的时候,读到一个字节就返回一个字节;字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在 UTF-8码表中是 3 个字节)时。先去查指定的编码表,将查到的字符返回。
字节流可以处理所有类型数据,如图片、声音、影音等;字符流只能处理字符数据。
(只要是处理纯文本数据,就要优先考虑使用字符流。除此之外都用字节流。)
以下是java io中常用的流:
一、文件操作File类
1、文件基本操作:
publicstaticvoidcreateFile()throwsIOException{
StringfileName = "F:"+File.separator+"test.txt";//File.separator运行结果为 '\'
Filef =newFile(fileName);
f.mkdir();//只能在已经存在的目录中创建创建文件夹
f.mkdirs();//可以在不存在的目录中创建文件夹
f.createNewFile();//只能在已经存在的目录下创建文件,否则报错
f.delete();//删除此抽象路径名表示的文件或目录
System.out.println("文件名 "+f.getName()); // 返回由此抽象路径名表示的文件或目录的名称。
System.out.println("文件父目录字符串 "+f.getParent());//返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null
}
注:mkdir创建文件夹,createNewFile创建文件。
2、列出指定目录所有文件夹(包括隐藏文件):
publicstaticvoidshowFile(){
StringfileName = "F:"+File.separator;
Filef = newFile(fileName);
File[]file = f.listFiles();
for (int i = 0; i < file.length; i++) {
System.out.println(file[i]);
}
}
3、判断指定文件是否为目录(isDirectory()):
publicstaticvoidisFile(){
StringfileName = "F:"+File.separator;
Filef = newFile(fileName);
if(f.isDirectory()){
System.out.println("true");
}else{
System.out.println("false");
}
}
注:
isFile():判断是否文件,也许可能是文件或者目录
exists():判断是否存在,可能不存在
二、字节流
1、输入流OutputStream类:
1.1、向指定文件写入字符串:
publicstaticvoid writeStr()throws IOException{
String fileName = "F:"+File.separator+"test.txt";
File f = new File(fileName);
if(f.exists()){//判断该文件是否存在
OutputStream os = new FileOutputStream(f);
String str = "你好";
byte[] bt = str.getBytes();
os.write(bt);
os.flush();
os.close();
}else{
System.out.println("该文件不存在");
}
}
1.2、向文件追加新内容:
publicstaticvoid appendStr()throws IOException{
String fileName = "F:"+File.separator+"test.txt";
File f = new File(fileName);
if(f.exists()){//判断该文件是否存在
OutputStream os = new FileOutputStream(f,true);//参数为 true 表示将字节写到末尾处
String str = "JAVA";
byte[] bt = str.getBytes();
os.write(bt);
os.flush();
os.close();
}else{
System.out.println("该文件不存在");
}
}
注:如果该目录不存在则报错,如果该文件不存在则自动创建
2、输出流InputStream类:
publicstaticvoid readStr()throws IOException{
String fileName = "F:"+File.separator+"test.txt";
File f = new File(fileName);
if(f.exists()){//判断该文件是否存在
InputStream is = new FileInputStream(f);
byte[] bt =newbyte[(int) f.length()];//为了避免浪费空间或者空间不足问题,在这里取 'f'的长度
int count = 0;
int temp = 0;
while ((temp=is.read()) != (-1)) {//如果不知道文件的长度,此时需要判断是否已经读到文件的末尾
bt[count++] = (byte) temp;
} is.close();
System.out.println(new String(bt));
}else{
System.out.println("该文件不存在");
}
}
输出结果为:你好JAVA
三、字符流
1、输入流Writer类:
1.1写入数据:
publicstaticvoidreadStrs()throwsIOException{
StringfileName = "F:"+File.separator+"test.txt";
Filef = newFile(fileName);
if(f.exists()){
Writerwot = newFileWriter(f);
Stringstr = "你好";
wot.write(str);
wot.flush();
wot.close();
}else{
System.out.println("该文件不存在");
}
}
1.2追加数据:
publicstaticvoidwriterStrs()throwsIOException{
StringfileName = "F:"+File.separator+"test.txt";
Filef = newFile(fileName);
if(f.exists()){
Writerwot = newFileWriter(f,true);
Stringstr = "JAVA";
wot.write(str);
wot.flush();
wot.close();
}else{
System.out.println("该文件不存在");
}
}
2、输出流Reader
publicstaticvoidreadStrs() throws IOException{
StringfileName = "F:"+File.separator+"test.txt";
Filef = newFile(fileName);
if(f.exists()){
Readerred = newFileReader(f);
char[] cr =newchar[(int) f.length()];
int count = 0;
int temp = 0;
while ((temp=red.read()) !=(-1)) {
cr[count++]= (char)temp;
}
System.out.println(new String(cr));
}else{
System.out.println("该文件不存在");
}
}
输出结果为:你好JAVA