经常忘记或者混淆的一些常用

本文详细介绍了Java中的IO流概念,包括字节流和字符流的区别,以及如何使用各种输入输出流进行文件操作。通过示例代码展示了如何读取和写入文件,并提供了将字节流转换为字符流的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1k = 1024byte(字节)

1byte(字节) = 8位(bit)


UTF-8编码格式中一个汉字用的三个字节,有的用的4个字节保存
GBK编码各种中一个汉字用的是2个字节保存,
所以用不同的编码格式 就会出现汉字为乱码的情况


字符就是几个字母,比如a和A都是一个字符.aa就是两个字符.
每个字节是由8位组成的.位是最小的单位了,叫做bit.
而字节呢,叫做byte.
所以,一个英文字母,无论大写和小写都是一个字符、一个字节,8位.
一个汉字是一个字符、两个字节,16位


字节流 和 字符楼


通过查在线文档:http://tool.oschina.net/apidocs/apidoc?api=jdk-zh  在线jdk文档得知:

字节流中:FileInputStream 构造函数为:FileInputStream(File file) 

BufferedInputStream 构造函数:BufferedInputStream(InputStream in)   或者  BufferedInputStream(InputStream in, int size)

DataInputStreamg构造函数:DataInputStream(InputStream in)

StringBufferInputStream 构造函数:StringBufferInputStream(String s)

ByteArrayInputStream 构造函数:ByteArrayInputStream(byte[] buf) 或者 ByteArrayInputStream(byte[] buf, int offset, int length)创建

                                                            一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组

字符流中:BufferedReader构造函数:BufferedReader(Reader in)

FileReader构造函数:FileReader(File file)

StringReader构造函数:StringReader(String s)

ByteArrayReader构造函数: 

IO流的分类

下面内容参考文章:http://www.2cto.com/kf/201312/262036.html   如若冒犯原作者的地方可联系本人删除即可。

根据处理数据类型的不同分为:字符流和字节流


根据数据流向不同分为:输入流和输出流

InputStream 是所有的输入字节流的父类,它是一个抽象类。


ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。PipedInputStream 是从与其它线程共用的管道中读取数据,与Piped 相关的知识后续单独介绍。


ObjectInputStream 和所有FilterInputStream的子类都是装饰流(装饰器模式的主角)。

eg:

/**
 * 字节流
 * 读文件内容,节省空间
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[(int)f.length()];
       in.read(b);
       System.out.println("文件长度为:"+f.length());
       in.close();
       System.out.println(new String(b));
    }
}

逐字节读:

 /**
 * 字节流
 * 读文件内容,节省空间
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       InputStream in=new FileInputStream(f);
       byte[] b=new byte[(int)f.length()];
       for (int i = 0; i < b.length; i++) {
           b[i]=(byte)in.read();
       }
       in.close();
       System.out.println(new String(b));
    }
}

DataInputStream:注意参数是inputstream 不是file

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
  
public class DataOutputStreamDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       DataInputStream input = new DataInputStream(new FileInputStream(file));
       char[] ch = new char[10];
       int count = 0;
       char temp;
       while((temp = input.readChar()) != 'C'){
           ch[count++] = temp;
       }
       System.out.println(ch);
    }
}

OutputStream 是所有的输出字节流的父类,它是一个抽象类。


ByteArrayOutputStream、FileOutputStream是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。PipedOutputStream 是向与其它线程共用的管道中写入数据,


ObjectOutputStream 和所有FilterOutputStream的子类都是装饰流。

eg:向文件中写入字符串

/**
 * 字节流
 * 向文件中写入字符串
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       String str="Hello World";
       byte[] b=str.getBytes();
       out.write(b);
       out.close();
    }
}

逐字节写入文件

/**
 * 字节流
 * 向文件中一个字节一个字节的写入字符串
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f);
       String str="Hello World!!";
       byte[] b=str.getBytes();
       for (int i = 0; i < b.length; i++) {
           out.write(b[i]);
       }
       out.close();
    }
}

向文件中追加新内容

/**
 * 字节流
 * 向文件中追加新内容:
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       OutputStream out =new FileOutputStream(f,true);//true表示追加模式,否则为覆盖
       String str="Rollen";
       //String str="\r\nRollen"; 可以换行
       byte[] b=str.getBytes();
       for (int i = 0; i < b.length; i++) {
           out.write(b[i]);
       }
       out.close();
    }
}

使用内存操作流将一个大写字母转化为小写字母:

/**
 * 使用内存操作流将一个大写字母转化为小写字母
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String str="ROLLENHOLT";
       ByteArrayInputStream input=new ByteArrayInputStream(str.getBytes());
       ByteArrayOutputStream output=new ByteArrayOutputStream();
       int temp=0;
       while((temp=input.read())!=-1){
           char ch=(char)temp;
           output.write(Character.toLowerCase(ch));
       }
       String outStr=output.toString();
       input.close();
       output.close();
       System.out.println(outStr);
    }
}

DataOutputStream类示例:

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo{
   public static void main(String[] args) throws IOException{
       File file = new File("d:" + File.separator +"hello.txt");
       char[] ch = { 'A', 'B', 'C' };
       DataOutputStream out = null;
       out = new DataOutputStream(new FileOutputStream(file));
       for(char temp : ch){
           out.writeChar(temp);
       }
       out.close();
    }
}

Reader:

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁


Reader 是所有的输入字符流的父类,它是一个抽象类。
CharReader、StringReader是两种基本的介质流,它们分别将Char 数组、String中读取数据。PipedReader 是从与其它线程共用的管道中读取数据。
BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。

InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流。

eg:

/**
 * 字符流
 * 从文件中读出内容
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       char[] ch=new char[100];
       Reader read=new FileReader(f);
       int temp=0;
       int count=0;
       while((temp=read.read())!=(-1)){
           ch[count++]=(char)temp;
       }
       read.close();
       System.out.println("内容为"+new String(ch,0,count));
    }
}

writer:

Writer 是所有的输出字符流的父类,它是一个抽象类。
CharArrayWriter、StringWriter 是两种基本的介质流,它们分别向Char 数组、String 中写入数据。
PipedWriter 是向与其它线程共用的管道中写入数据,
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。

向文件中写入数据:

eg:

/**
 * 字符流
 * 写入数据
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName="D:"+File.separator+"hello.txt";
       File f=new File(fileName);
       Writer out =new FileWriter(f);
       String str="hello";
       out.write(str);
       out.close();
    }
}


将字节输出流转化为字符输出流:

eg:/**
 * 将字节输出流转化为字符输出流
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName= "d:"+File.separator+"hello.txt";
       File file=new File(fileName);
       Writer out=new OutputStreamWriter(new FileOutputStream(file));
       out.write("hello");
       out.close();
    }
}

将字节输入流转换为字符输入流:

/**
 * 将字节输入流变为字符输入流
 * */
import java.io.*;
class hello{
   public static void main(String[] args) throws IOException {
       String fileName= "d:"+File.separator+"hello.txt";
       File file=new File(fileName);
       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();
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值