Java IO流笔记
关于流:
是一组有顺序的有起点和终点的字节集合,是对数据传输的总称或抽象. 用于数据传输
IO流分类:
根据处理数据类型的不同可分为:字符流和字节流.
根据数据流向不同分为:输入流和输出流.
字符流和字节流:
使用普通字节流复制a文件的内容到b文件中
public static void main(String[] args) throws IOException {
test();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
test11();
}
public static void test() throws IOException {
FileInputStream fis = new FileInputStream("/Users/dllo/Desktop/HelloMail/a.txt");
FileOutputStream fos = new FileOutputStream("/Users/dllo/Desktop/HelloMail/b.txt");
// 方式一:一次读取写入单个字节
int i = 0;
while ((i=fis.read())!=-1){
fos.write(i);
}
fos.close();
fis.close();
// 方式二:一次读取写入一个字节数组
byte[] bytes = new byte[1024];
int len = 0;
while((len=fis.read(bytes)) !=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
使用高效字符流复制文件
// 使用高效字符流复制文件
public static void test2() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/dllo/Desktop/HelloMail/a.txt"));
// 如果d文件中有数据,true表示继续往文件中追加数据
BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/dllo/Desktop/HelloMail/d.txt",true));
String line = null;
// 高效字符输入流的特有方法readline(),每次读取一行数据
while((line = br.readLine()) !=null){
bw.write(line);
// 高效字符输出流的特有方法newline()
bw.newLine();
// 将缓冲区中的数据刷到目的地文件中
bw.flush();
}
// 关闭流,在关闭前,会先刷新该流,从里到外依次关闭
bw.close();
br.close();
}
键盘输入字节流InputStream
public static void test3() throws IOException {
// 创建键盘录入
InputStream inputStream = System.in;
// 将字节流转换为字符流
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
// 高效字符流读取数据
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 打印流写入数据,第一个true表示如果文件中有内容就追加数据,第二个true表示启用自动刷新
PrintWriter printWriter = new PrintWriter(new FileWriter("/Users/dllo/Desktop/HelloMail/a.txt",true),true);
String line = null;
while ((line=bufferedReader.readLine())!=null){
// 键盘输入”over”结束输入
if (“over”.equals(line)){
break;
}
// PrintWrite启用自动刷新
printWriter.println(line);
}
printWriter.close();
bufferedReader.close();
}
使用OutputStream向屏幕上输出内容
public static void test4(){
OutputStream out = System.out;
try {
out.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
为System.out.println()重定向输出
// 输入输出重定向
public static void test5(){
// 此刻直接输出到屏幕
System.out.println(“hello”);
File file = new File("/Users/dllo/Desktop/HelloMail/"+File.separator+"a.txt");
try {
System.out.println(new PrintStream(new FileOutputStream(file)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("这些内容在文件中才能看到哦!");
}
使用缓冲区从键盘上读入内容
public static void test6(){
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String str = null;
System.out.println("请输入内容");
try {
str = buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你输入的内容是: "+str);
}
Scanner类从文件中读出内容
public static void test6(){
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
String str = null;
System.out.println("请输入内容");
try {
str = buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你输入的内容是: "+str);
}
字符输出流Wirter
/*
字符输出流Writer
定义和说明:
Winter是所有的输出字符流的父类,它是一个抽象类
CharArrayWriter,StringWriter 是两种基本介质流,它们分别向Char数组,String 中写入数据.
PipedWriter 是向与其他线程共用的管道中写入数据.
BufferWriter 是一个装饰器,为Writer提供缓冲功能.
PrintWriter和PrintStream极其类似,功能和使用也非常相似.
OutputStreamWriter是OutputStream到Writer转换的桥梁,它的子类FileWriter其实就是一个
实现此功能的具体类.功能和使用和OutputStream极其类似.
*/
public static void test8() throws IOException {
// String fileName = “/Users/dllo/Desktop/HelloMail/a.txt”;
//
// File file = new File(fileName);
File file = new File("/Users/dllo/Desktop/HelloMail/a.txt");
Writer out = new FileWriter(file);
String str = "早上没吃饭,好饿,饿的胃疼!!!";
out.write(str);
out.close();
}
字符流与字节流转换
/*
字符流与字节流转换
转换流的特点:
(1) 是字符流和字节流之间的桥梁
(2) 可对读取到的字节数据经过指定编码转换成字符
(3) 可对读取到的字符数据经过指定编码转换成字节
何时使用转换流?
当字节和字符之间有转换动作时;
流操作的数据需要编码或解码时;
具体的对象体现:
InputStreamReader:字节到字符的桥梁
OutputStreamWriter:字符到字节的桥梁
这两个流对象是字符体系中的成员,他们有转换作用,本身又是字符流.所以在构造的时候需要传入字节流对象进来
*/
// 字节流和字符流转换实例:
/*
将字节流输出转化为字符流输出流
*/
public static void test9() throws IOException {
File file = new File("/Users/dllo/Desktop/HelloMail/a.txt");
Writer out = new OutputStreamWriter(new FileOutputStream(file));
out.write("突然不饿了!因为已经饿过劲了!");
out.close();
}
字符流从文件中读取内容
public static void test10() throws IOException {
File file = new File("/Users/dllo/Desktop/HelloMail/a.txt");
Reader read = new InputStreamReader(new FileInputStream(file));
char[] b = new char[100];
int len = read.read(b);
System.out.println(new String(b,0,len));
read.close();
}
File类
/*
File类
*/
// 创建一个文件夹
public static void test11() throws IOException {
File file = new File("/Users/dllo/Desktop/HelloMail/z.txt");
file.createNewFile();
}
转自