Java IO 之输入、输出流(字节流、字符流)入门实例
—————-字节流:
public abstract class OutputStream extends Object implements Closeable, Flushable; // 输出流,以字节或字节数组为单位写出数据到外部
public class FileOutputStream extends OutputStream;
public FileOutputStream(File file)
throws FileNotFoundException;
public FileOutputStream(File file,
boolean append) // append为true 表示内容自动追加
throws FileNotFoundException;
public void write(int b) // 按int型字节写出
throws IOException;
public void write(byte[] b) // 一次性写出整个字节数组的内容
throws IOException;
public void write(byte[] b,
int off,
int len) // 写出该数组指定范围的字节
throws IOException;
public void close() // 写操作完毕后释放资源
throws IOException;
public abstract class InputStream; // 输入流,以字节或字节数组为单位读入数据到某处(内存、文件)
public class FileInputStream
extends InputStream;
public FileInputStream(File file) // 构造输入流
throws FileNotFoundException;
public int read() // 按字节读入数据,返回一个int型表示的字节或者-1表示文件尾;
throws IOException;
public int read(byte[] b)// 读入数据并全部存入到b数组中,有可能b不够用
throws IOException;
public int read(byte[] b,
int off,
int len)// 读入数据到b数组指定的范围;
throws IOException;
public int available() // 返回输入流的有效字节长度;
throws IOException;
public void close()
throws IOException;
——字节流操作的步骤:
1.构建File类:打开文件操作对象;
2.构建输入,输出流:创建流对象用于输入或输出;
3.读写操作;
4.关闭流,释放资源;
完成写操作:
package com.mldn;
import java.io.File;
import java.io.OutputStream; // 输出流,抽象类,用来接收所有的子类实例
import java.io.FileOutputStream;// OutputStream的子类,可以操作File类;
import java.io.IOException;
import java.io.FileNotFoundException;
public class OutputDemo
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
String strData = “Hello, World!”; // 准备写入数据
byte[] bytes = strData.getBytes(); // 字节流按字节处理数据,要把String —> byte数组
try
{
// 2.通过字节流的子类指定输出的位置:file关联的文本文件是流输出的目的地;
OutputStream out = new FileOutputStream(file); // FileNotFoundException
// 3.通过输出流完成字节流写操作:
out.write(bytes); // IOException
for (int x = 0; x < bytes.length; x++)
{
//out.write(x); // 写入整型,将不能直接读取,需InputStream来读入内存,并输出到标准输出
out.write(bytes[x]); // 一次写一个字节
}
// 4.写操作完毕,关闭输出流,释放资源;
out.close(); // IOException
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
Hello, World!Hello, World!
*/
————-文件内容的自动追加:
package com.mldn;
import java.io.File;
import java.io.OutputStream; // 输出流,抽象类,用来接收所有的子类实例
import java.io.FileOutputStream;// OutputStream的子类,可以操作File类;
import java.io.IOException;
import java.io.FileNotFoundException;
public class OutputDemo
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
String strData = “Hello, World!”; // 准备写入数据
byte[] bytes = strData.getBytes(); // 字节流按字节处理数据,要把String —> byte数组
try
{
// 2.通过字节流的子类指定输出的位置:file关联的文本文件是流输出的目的地;
OutputStream out = new FileOutputStream(file, true); // FileNotFoundException,实现写操作的自动内容追加!
// 3.通过输出流完成字节流写操作:
out.write(bytes); // IOException
for (int x = 0; x < bytes.length; x++)
{
//out.write(x); // 写入整型,将不能直接读取,需InputStream来读入内存,并输出到标准输出
out.write(bytes[x]); // 一次写一个字节
}
// 4.写操作完毕,关闭输出流,释放资源;
out.close(); // IOException
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
public FileOutputStream(File file, boolean append) throws FileNotFoundException;
该构造可以指定是否自动在文件中实现自动内容追加; 只要append == true;
Hello, World!Hello, World!Hello, World!Hello, World!
*/
——————实现自动追加时换行
package com.mldn;
import java.io.File;
import java.io.OutputStream; // 输出流,抽象类,用来接收所有的子类实例
import java.io.FileOutputStream;// OutputStream的子类,可以操作File类;
import java.io.IOException;
import java.io.FileNotFoundException;
public class OutputDemo
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
String strData = “\r\nHello, World!”; // 准备写入数据,加入换行!
byte[] bytes = strData.getBytes(); // 字节流按字节处理数据,要把String —> byte数组
try
{
// 2.通过字节流的子类指定输出的位置:file关联的文本文件是流输出的目的地;
OutputStream out = new FileOutputStream(file, true); // FileNotFoundException,实现写操作的自动内容追加!
// 3.通过输出流完成字节流写操作:
out.write(bytes); // IOException
/*for (int x = 0; x < bytes.length; x++)
{
//out.write(x); // 写入整型,将不能直接读取,需InputStream来读入内存,并输出到标准输出
out.write(bytes[x]); // 一次写一个字节
}*/
// 4.写操作完毕,关闭输出流,释放资源;
out.close(); // IOException
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
public FileOutputStream(File file, boolean append) throws FileNotFoundException;
该构造可以指定是否自动在文件中实现自动内容追加; 只要append == true;
Hello, World!Hello, World!Hello, World!Hello, World!
Hello, World!Hello, World!Hello, World!Hello, World!
Hello, World!
*/
————-从磁盘文件读入数据到内存:
package com.mldn;
import java.io.File;
import java.io.InputStream; // 输入流,抽象类,用来接收所有的子类实例
import java.io.FileInputStream;// InputStream的子类,可以操作File类;
import java.io.IOException;
import java.io.FileNotFoundException;
public class InputDemo
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
byte[] bytes = null; // 用来接收读入的字节
try
{
// 2.通过字节流的子类指定输入的位置:file关联的文本文件是流输入的源;
InputStream in = new FileInputStream(file); // FileNotFoundException,实现读操作!
int size = in.available(); // 读取文件字节数 ,可以使用(int)file.length()获取
bytes = new byte[size];
// 3.通过输入流完成字节流读操作:
in.read(bytes); // IOException,从文件读入内存 将按byte数组的长度读取字节流
/*
for (int x = 0; x < bytes.length; x++)
{
bytes[x] = (byte)in.read(); // 每次读入一个字节并保存在bytes数组中
}
*/
// 4.读操作完毕,关闭输入流,释放资源;
in.close(); // IOException
System.out.println(“内容:\n” + new String(bytes) + “\n共” + size + “个字节读取完毕!”); // 将读入的字节转换 String
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
administrator@xu-desktop:~$ java com.mldn.InputDemo
内容:
Hello, World!Hello, World!Hello, World!Hello, World!
Hello, World!
共67个字节读取完毕!
*/
————–当不确定读取文件大小时:
package com.mldn;
import java.io.File;
import java.io.InputStream; // 输入流,抽象类,用来接收所有的子类实例
import java.io.FileInputStream;// InputStream的子类,可以操作File类;
import java.io.IOException;
import java.io.FileNotFoundException;
public class InputDemo
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
try
{
// 2.通过字节流的子类指定输入的位置:file关联的文本文件是流输入的源;
InputStream in = new FileInputStream(file); // FileNotFoundException,实现读操作!
// 3.通过输入流完成字节流读操作:
int temp = 0; // 接收临时字节流
int len = 0; // 计算字节数
byte[] bytes = new byte[1]; // 接收字节流
File save = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “saveFile.txt”);
FileOutputStream out = new FileOutputStream(save, true); // 保存内容
System.out.println(“内容:”);
while ((temp = in.read()) != -1) // 当前读取未到文件尾
{
++len;
out.write(temp); // 按int写出字节流,保存到指定磁盘文件
bytes[0] = (byte)temp;
System.out.print(new String(bytes)); // 将读入的字节转换 String并写出到标准输出文件;
}
// 4.读操作完毕,关闭输入流,释放资源;
in.close(); // IOException
out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
administrator@xu-desktop:~$ java com.mldn.InputDemo
内容:
Hello, World!Hello, World!Hello, World!Hello, World!
Hello, World!
共67个字节读取完毕!
不确定文件大小时,一定要判断是否读到了文件尾!
*/
————-字符流:
public abstract class Writer extends Object implements Appendable, Closeable, Flushable;
// 字符输出流:用于写出数据;
public class OutputStreamWriter extends Writer;// 字符流 –> 字节流的一个桥梁!
public class FileWriter extends OutputStreamWriter; // 文件输出字符流;
public FileWriter(File file)
throws IOException;
public FileWriter(File file,
boolean append) // 实现自动追加
throws IOException;
public void write(char[] cbuf) // 写字符数组;
throws IOException;
public void write(String str) // 写字符串;
throws IOException;
public void write(char[] cbuf,
int off,
int len)// 以字符形式写出
throws IOException;
public void write(int c) // 写出单字符
throws IOException;
public void write(String str,
int off,
int len)// 写出字符串数据;
throws IOException;
public void close()
throws IOException;
public void flush() // 手动强制刷新缓冲区,使内容保存到文件;
throws IOException;
public String getEncoding();// 返回流引用的字符编码;
public Writer append(char c)// 追加字符到输出流
throws IOException;
—–
public abstract class Reader extends Object implements Readable, Closeable ;// 输入流,读入数据;
public class InputStreamReader extends Reader; // 字符流与字节流的桥梁;
public class FileReader extends InputStreamReader;//文件字符输入流;
public FileReader(File file)
throws FileNotFoundException;
public int read() // 读入一个字符;
throws IOException;
public int read(char[] cbuf,
int offset,
int length)// 读入一个数组的一部分区域中;
throws IOException;
public int read(char[] cbuf)// 读入数据到字符数组
throws IOException;
public int read(CharBuffer target) //
throws IOException;
public void close()
throws IOException;
public void mark(int readAheadLimit)// 标记当前流的读取点;以便复位操作;
throws IOException;
public boolean markSupported(); // 确定当前流是否支持标记读取点;
public void reset() // 复位到当前流在前面标记过的读取点;
throws IOException;
———-字符流比字节流原理是一致的,只不过使用的时候不必在字节与字符串类型之间进行转换了!
—–用字符流实现对磁盘文件的读、写操作;
package com.mldn;
import java.io.File;
import java.io.FileReader; // 输入流,
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter; // 字符输出流
public class ReadWrite
{
public static void main(String[] args)
{
// 1.用File类打开文件:
File file = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “myFile.txt”);
try
{
// 2.通过字节流的子类指定输入的位置:file关联的文本文件是流输入的源;
FileReader read = new FileReader(file); // FileNotFoundException,实现读操作!
// 3.通过输入流完成字节流读操作:
int temp = 0; // 接收临时字符流
int len = 0;
char[] ch = new char[1];
File save = new File(File.separator + “home” + File.separator + “administrator” + File.separator + “saveFile.txt”);
FileWriter write = new FileWriter(save, true); // 保存内容
System.out.println(“内容:”);
while ((temp = read.read()) != -1) // 当前读取未到文件尾
{
++len;
write.write(temp); // 按int写出单个字符流,保存到指定磁盘文件
ch[0] = (char)temp;
System.out.print(new String(ch)); // 将读入的字节转换 String并写出到标准输出文件;
}
System.out.println(“\n共” + len + “个字符读取完毕!”);
// 4.读操作完毕,关闭输入流,释放资源;
read.close(); // IOException
//write.flush(); // 手动刷新缓冲区,强制保存内存缓冲区的内容到文件
write.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/*
administrator@xu-desktop:~$ java com.mldn.ReadWrite
内容:
毛峰属于绿茶,产地很多,主要有云南,峨眉,遵义等,但正宗原产地为安徽黄山,也是我国十大名茶之一。
黄山毛峰属于炒青绿茶,外形微卷,状似雀舌,绿中泛黄,银毫显露,且带有金黄色鱼叶(俗称黄金片)。入杯冲泡雾气结顶,汤色清碧微黄,叶底黄绿有活力,滋味醇甘,香气如兰,韵味深长。
雀舌是黄山毛峰中的极品,产于明前第一批茶。
形状: 芽头肥壮,形似雀舌
色泽: 嫩绿泛象牙色
净度: 匀齐
香气: 嫩香持久
滋味: 鲜爽回甘
汤色: 淡黄清澈明亮
叶底: 嫩匀肥壮,嫩绿明亮
同于绿茶中含有丰富的微量元素,多种维生素,所以常饮对身体有益,主要好处有:
1、对促进血液循环、降低胆固醇、增加毛细血管弹性,增强血液抗凝性都有一定好处;
2、脑力劳动者可提高脑子的敏捷程度,保持头脑清醒、精神饱满,增强思维能力,判断能力和记忆力;
3、经常接触有毒害物质的工作人员,可以选择绿茶作为劳动保护饮料;
4、糖尿病患者一般宜多饮绿茶;
5、绿茶所含的茶多酚能够消灭癌细胞。
6、绿茶含维生素C和茶多酚量较多,其抑制细菌生长繁殖、抗辐射、防血管硬化、降血脂及增加白细胞的作用较强;患有胆炎、胆囊炎、前列腺炎、关节炎、高血压、心脏病、神经衰弱等症的患者以及长期与放射线接触的人,以饮绿茶为宜。
7、中老年人饮绿茶,还有防癌、抗癌的作用。
共592个字符读取完毕!
可以发现可以正确的读取字符并以本地编码直接正常显示,这是与字节流不同的;
字节流直接操作文件;而字符流需要经过内存缓冲区作为中继站,如果流操作未关闭,必须手动flush刷新缓冲区,才能将缓冲区的文件写出到文件;
按字节更常用,特别是copy数据,比方说图片;
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《Java IO 之输入、输出流(字节流、字符流)入门实例》
本文地址:http://www.xiupu.net/archives-81.html
关注公众号:
微信赞赏
支付宝赞赏