对于java中io流的一些操作和类进行总结
io流的分类:
字节流:用来处理二进制文件
字符流:用来处理文本数据
文件io流:
字节流:
InputStream的子类:
FileInputStream类:
读的方式:read
- 一个字节的读取:
public class test_FileInputStream {
public static void main(String[] args) {
}
@Test
public void test_FileInputStream() throws IOException {
String filepath="D:\\网盘文件\\hello.txt";
FileInputStream fileInputStream=null;
int read_data=0;//读取文件字节数
try {
fileInputStream=new FileInputStream(filepath);
//read读取的字节数:如果读取失败的话就直接返回-1
while ((read_data=fileInputStream.read())!=-1){
System.out.print((char) read_data);
}
}catch (Exception e){
e.printStackTrace();
}finally {
fileInputStream.close();
}
}
}
- 使用byte数组来读取:
public class test_FileInputStream02 {
public static void main(String[] args) {
}
@Test
public void test_FileInputStream02() throws IOException {
String filepath="d:\\网盘文件\\hello.txt";
byte[] buf=new byte[10];
FileInputStream fileInputStream=null;
int read_len=0;//读取文件字节数
try {
fileInputStream=new FileInputStream(filepath);
//按位数组读取元素:
while ((read_len=fileInputStream.read(buf))!=-1) {
System.out.print(new String(buf,0,read_len));
}
}catch (Exception e){
e.printStackTrace();
}finally {
fileInputStream.close();
}
}
}
OutputStream子类:
FileOuputStream类:
写入的方式:
- 写入文件内容:
public class test_FileOutputStream {
public static void main(String[] args) {
}
@Test
public void fileOutputStream() throws IOException {
String filename="D:\\网盘文件\\hello.txt";
FileOutputStream fileOutputStream=null;
//写入文件的内容:
String str="hello world";
try {
//写入文件使用追加的方式:
fileOutputStream=new FileOutputStream(filename,true);
fileOutputStream.write(str.getBytes());
System.out.println("写入文件成功!");
}catch (Exception e){
e.printStackTrace();
}finally {
fileOutputStream.close();
}
}
}
文件拷贝:
public class test_拷贝文件 {
public static void main(String[] args) {
}
@Test
public void copyfile(){
String srcfile="d:\\file.txt";
String desfile="d:\\网盘文件\\file.txt";
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
int read_len=0;
byte [] buf=new byte[1024];
try {
fileInputStream=new FileInputStream(srcfile);
fileOutputStream=new FileOutputStream(desfile);
while ((read_len=fileInputStream.read(buf))!=-1){
//使用位数组的方式拷贝文件:
fileOutputStream.write(buf,0,read_len);
System.out.println("拷贝文件完成!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
}
}
字符流:Reader 和Writer
Reader:实现的子类
FileReader类:
public class test_FileReader {
//Reader的实现子类;
/***
*
* 使用读取单个字符的方式
* ***/
@Test
public void file_reader01() throws IOException {
String filepath="d:\\file.txt";
FileReader fileReader =null;
int read_len=0;
try {
fileReader=new FileReader(filepath);
System.out.print("读取字符为:");
while ((read_len=fileReader.read())!=-1){
System.out.print((char)read_len);
}
}catch(Exception e){
e.printStackTrace();
}finally {
fileReader.close();
}
}
/***
*
* 使用字符数组的方式进行读取文件
* **/
@Test
public void file_reader02() throws IOException {
String filepath="d:\\file.txt";
FileReader fileReader =null;
//使用字符数组的方式进行读
char [] chars=new char[20];
int read_data=0;
try{
fileReader=new FileReader(filepath);
while ((read_data=fileReader.read(chars))!=-1){
System.out.print(new String(chars,0,read_data));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fileReader.close();
}
}
}
Writer:实现的子类
Filewriter类:
public class test_FileWriter {
//Writer的实现子类
@Test
public void file_writer01() throws IOException {
String filepath="d:\\file.txt";
FileWriter fileWriter=null;
// char [] str={'a','b','c'};
try{
fileWriter=new FileWriter(filepath,true); //以追加的方式写入;
// fileWriter.write(str); 以字符数组的方式写入
fileWriter.write("你好java"); //以字符串的方式写入
System.out.println("写入文件成功");
}catch (Exception e){
e.printStackTrace();
}finally {
fileWriter.close();
}
}
}
节点流和处理流:
节点流:
从一个特定的数据源(比如是FileReader和FilerWriter)进行读写文件,比如说文件。直接跟数据源相连。
处理流:(BufferedReader、BufferedWriter、BufferedInputStream、BufferedOutputStream)继承了Reader和Writer类
对某一个节点流进行包装,可以封装任意一个节点流,使处理读写更加方便。
字符处理流:
BufferReader:(用来处理字符)
public class test_BufferReader {
public static void main(String[] args) throws Exception{
String filepath="d:\\file.txt";
String line=null; //按行读取
BufferedReader bufferedReader = new BufferedReader(new FileReader(filepath));
//readLine() 是按一行来读取的;
// 当返回null 表示读取完毕;
while ((line=bufferedReader.readLine()) !=null){
System.out.println(line);
}
bufferedReader.close();
}
}
BufferWriter:(用来处理字符)
public class test_BufferWriter {
public static void main(String[] args) throws IOException {
String filepath="d:\\file.txt";
//new FileWriter(filepath,true) //以追加的方式:
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath,true));
bufferedWriter.write("要努力变强啊!");
bufferedWriter.newLine(); //进行换行操作;
System.out.println("写入文件成功!");
bufferedWriter.write("那是肯定的");
//关闭流的操作:
bufferedWriter.close();
}
}
字节处理流(用来处理二进制数据的处理流):
BufferedInuputStream:(用来处理字节,只要给它传入一个InputStream的节点流)
BufferedOutputStream:(用来处理字节,只要给它传入一个OutputStream节点流)
public class test_BinputStream_BoutputStream {
public static void main(String[] args) throws IOException {
String srcpath="d:\\test.jpg";
String despath="d:\\test01.jpg";
int read_len=0;//读取数据的长度:
//字节处理流,就是将其他的节点字节处理进行包装,然后操作的
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream=null;
byte[] buf=new byte[1024];//以字节数组的方式读取:
try {
//新建处理流的对象:
bufferedInputStream=new BufferedInputStream(new FileInputStream(srcpath));
bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(despath));
//读取文件:
while ((read_len=bufferedInputStream.read(buf))!=-1){
//写入文件
bufferedOutputStream.write(buf,0,read_len);
}
System.out.println("读取和写入二进制图片成功!");
}catch (Exception e){
e.printStackTrace();
}finally {
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
}
对象处理流:ObjectInputStream(提供反序列化,读文件)、ObjectOutputStream(提供序列化,写文件)
需求:需要将保存的文件的数据,需要进行序列化和反序列化
序列化:保存数据的值的类型
反序列化:恢复数据的数据类型和值
序列化和反序列的注意事项:
ObjectInputStream
ObjectOutputStream:
public class test_ObjectOutputStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filepath="d:\\data.txt";
//进行序列化:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filepath));
oos.writeInt(10);
oos.writeBoolean(true);
oos.writeUTF("你好");
oos.writeObject(new Dog("小黄",10));
System.out.println("序列化成功!");
oos.close();
//进行反序列化:进行读取数据:
//读取数据和序列化的类型要是一样的,否则会报异常;
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filepath));
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readUTF());
System.out.println(ois.readObject());
System.out.println("反序列化成功");
//关闭流文件
ois.close();
}
}
//Dog实现序列:
class Dog implements Serializable {
private String name;
private int age ;
Dog(){
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
}
标准的输入和输出流:
转换流:处理文件编码方式错乱的情况。
InputStreamReader(将字节流转为字符流) 和OutputStreamWriter
InputStreamReader:将字节流转为字符流
OutputStreamWriter:将字节流转为字符流
//使用转换流的方式:
@Test
public void test_InputStreanReader() throws IOException {
String filepath="d:\\网盘文件\\a.txt";
//转换流: 指定转换的字符编码方式= gbk的方式;
InputStreamReader isr = new InputStreamReader(new FileInputStream(filepath),"gbk");
//将转换流使用处理流来处理 ;因为InputStreamReader也是Reader的子类,可以使用处理流来包装;
BufferedReader bufferedReader = new BufferedReader(isr);
String s = bufferedReader.readLine();
System.out.println("转换流输出的结果是:"+s);
bufferedReader.close();
}
@Test
public void test_OutputStreamWriter() throws IOException {
String filepath="d:\\网盘文件\\a.txt";
//将字节流FileOutputStream 转为字符流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filepath,true),"gbk");
//将转换流用字符处理流进行包装:
BufferedWriter bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write("ok,好的");
bufferedWriter.close();
System.out.println("转换流写入文件完毕!");
}
打印流(只有输出流,没有输入流):PrintReader、PrinterWriter
PrintStream:(字节流)
public class test_printStream {
public static void main(String[] args) throws IOException {
//字节打印流
PrintStream out = System.out;
out.write("你好".getBytes());
out.close();
}
}
PrintWriter:(字符流)
@Test
public void test_PrintWriter(){
System.out.println("使用PrintWriter");
//打印到显示台:
PrintWriter printWriter = new PrintWriter(System.out);
printWriter.write("你好北京");
printWriter.close();
}
java中的Properties文件:
常用属性方法:load、store、getProperties()、setProperties()
public class test_properties {
@Test
public void test_loadproperties() throws IOException {
Properties properties = new Properties();
properties.load(new FileReader("src\\mysql.properties"));
//将获取到的属性输出到控制台:
// properties.list(System.out);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
@Test
public void test_setproperties() throws IOException {
Properties properties = new Properties();
properties.setProperty("charset","utf-8");
properties.setProperty("小黄","100");
properties.setProperty("风越大","鱼越贵");
//存储到文件中
properties.store(new FileOutputStream("src\\mysql.properties",true),null);
}
}