IO流
1.定义:
- IO流(input/output):对文件的输入和输出流,从内存写入到磁盘就是输出流,从磁盘的文件写入到内存中就是输入流((以程序为参照物)。
2.File类的操作
import java.io.File;
import java.io.IOException;
public class FileTest {
public static void main(String[] args) {
File file = new File("D:\\wu.txt");
FileTest t = new FileTest();
t.createFile(file);
t.showFileInfo(file);
}
private void showFileInfo(File file) {
System.out.println("得到绝对路径:"+file.getAbsolutePath());
System.out.println("相对路径:"+file.getPath());
System.out.println("文件大小:"+file.length());
System.out.println("文件上级目录:"+file.getParent());
}
private void createFile(File file) {
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
System.out.println("该文件已存在!");
}
}
}
创建文件目录
import java.io.File;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) {
File path = new File("D:/wuming/like");
File file = new File("D:\\wuming\\like\\wu.txt");
path.mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.IO流分类:
一种是字节流,另外一种的字符流;字节流就是以字节(系统最小的存储单位)为单位输入输出流.比如文件、音频、视频等都可以做输入输出,字符流是以字符为单位输入输出,比如文字、中英文等,并且字符流有缓存区可以一行一行的读取字符,效率也要比字节流更高。
体系图:
4.字节流
4.1 字节输出流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 字节输出流 以字节为单位
* @author Administrator
*
*/
public class Test3 {
public static void main(String[] args) {
OutputStream os = null;
try {
//字节输出流
os = new FileOutputStream("D:/wu.txt");
String str = "我是全球技术最好的程序员!";
//字节输出流,需要将字符串打散成字节数组,以字节为单位写入到磁盘文件中
byte[] words = str.getBytes();
os.write(words);
System.out.println("写入成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
try {
//关闭流,销毁资源
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.2 字节输入流
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 字节输入流
* @author Administrator
*
*/
public class FileInputStreamTest {
public static void main(String[] args) {
//File file = new File("D:/wu.txt");
InputStream is = null;
try {
//InputStream is = new FileInputStream(file);
//创建字节输入流
is = new FileInputStream("D:/wu.txt");
int data;
//is.read()取到每一个字节通过Ascll转换到的数是0-255之间的整数,没有值了就返回-1
while((data=is.read())!=-1) {
//(char)data;把10进制的数据转换成对应的字符
//因为中文的字符可能是一个字节、两个字节甚至多个字节
//无法判断该字符有几个字节,所以以字节流去读取中文字符会出现乱码(要用字符流)
System.out.print((char)data);
}
System.out.println((char)data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.3 通过字节输入输出流实现文件的复制
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class FileINputAndOutputStream {
public static void main(String[] args) {
File file1 = new File("D:\\wu.txt");
File file2 = new File("D:\\wu2.txt");
InputStream is = null;
OutputStream os = null;
try {
//创建字节输入流
is = new FileInputStream(file1);
//创建字节输出流
os = new FileOutputStream(file2);
byte[] words = new byte[1024];
while((is.read(words))!=-1){
//os.write(words);
//截取指定长度的字节文件
os.write(words,0,words.length);
}
System.out.println("复制成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(os!=null) {
os.close();
}
if(is!=null) {
is.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.4 通过字节流实现二进制流
-
二进制流以二进制单位实现流,一般可以用于音频、视频等
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 二进制流一般用于图片、音频、视频等文件操作流 * @author Administrator * */ public class DataInputStreamTest { public static void main(String[] args) { InputStream is=null; OutputStream os=null; DataInputStream dis=null; DataOutputStream dos=null; try { is = new FileInputStream("D:\\collecton.png"); os = new FileOutputStream("D:\\1.png"); //借助字节输入流创建二进制输入流 dis = new DataInputStream(is); //借助字节输出流创建二进制输出流 dos = new DataOutputStream(os); int data; while((data=dis.read())!=-1) { //写入到文件里面去 dos.write(data); } System.out.println("复制成功!"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { //关闭资源 if(is!=null) { is.close(); } if(dis!=null) { dis.close(); } if(os!=null) { os.close(); } if(dos!=null) { dos.close(); } } catch (IOException e) { e.printStackTrace(); } } }
5.字符流
- 因为字节流是以字节为单位进行读写,中文无法判断该字符有几个字节,所以以字节流去读取中文字符会出现乱码,所以读写中文的时候建议用字符流,并且字符流可以一行一行的去读取,效率也比字节流高。
5.1 字符输入流
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class FileReaderTest {
public static void main(String[] args) {
Reader fr=null;
try {
//创建字符输入流
fr = new FileReader("D:\\wu.txt");
char words[] = new char[20];
//可变长度字符串
StringBuffer sb = new StringBuffer();
//ͨ通过fr.read(words)读取到20个字符存储到words数组
while((fr.read(words))!=-1) {
//将取到的字符拼接到StringBuffer字符串
sb.append(words);
fr.read(words);
}
System.out.println("文件中的数据:"+sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5.2 通过字符流设置缓存区提高获取效率
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class BefferReaderTest {
public static void main(String[] args) {
Reader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("D:\\wu2.txt");
//创建字符缓存区,把字符一个一个取到后,先放到缓存区够一行后再去写入到程序里面,再去取下一行
br = new BufferedReader(fr);
//通过缓存区取到一行数据
String line = br.readLine();
while(line!=null) {
System.out.println(line);
line=br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(br!=null) {
br.close();
}
if(fr!=null) {
fr.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.3 字符输出流
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class FileWriterTest {
public static void main(String[] args) {
Writer fw = null;
try {
//字符输出流
fw = new FileWriter("D:\\wu.txt");
fw.write("我是张三,我是程序员");
fw.write("我今年123岁");
fw.flush();//刷新流(将内存中的数据写到文件中)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(fw!=null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5.4 通过字符输出流设置缓存区输出到文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 通过字符输出流设置缓存区输出到文件
* @author Administrator
*
*/
public class BufferedWriterTest {
public static void main(String[] args) {
Writer fw =null;
BufferedWriter bw =null;
try {
fw = new FileWriter(new File("D:\\wu.txt"),true);
bw = new BufferedWriter(fw);
bw.write("我是程序员");
bw.newLine();
bw.write("我今年123岁");
bw.newLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(bw!=null) {
bw.close();
}
if(fw!=null) {
fw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}