Java-IO

Java IO流

相关的类或者接口

类                                           解释说明

File 文件类

RandomAccessFile  随机存取文件类

InputStream 字节输入流

OutputStream 字节输出流

Reader 字符输入流

Writer 字符输出流


流:一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或者抽象。

InputStream&&OutputStream是抽象类。


字节流:一次读入或者读出是8位二进制,直接与数据源相连。

字符流:一次读入或者读出是16位二进制


一个汉字 = 2个字节

1个字节 = 8位 = 8个二进制数

1位 = 1个二进制数


一个汉字 = 2个字符

一个字母或者标点 = 1个字符


字节流读文件:InputStream

package charAndByte;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class inputStream {

	public static void main(String args[]) throws IOException {
		
		File file = new File("C:\\Users\\summ\\Desktop\\2.txt");
		
		InputStream input = new FileInputStream(file);
		
		byte[] b = new byte[(int) file.length()];
		
		//读取文件中的内容到b中
		input.read(b);
		
		input.close();
		/**
		 * String str = new String(byte[] bytes);
		 * 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
		 * 新 String 的长度是字符集的函数,因此可能不等于 byte 数组的长度。 
		 */
		System.out.println(new String(b));
		
	}
	
}



字节流取文件到目标文件:OutPutStream

package charAndByte;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutPutStream {
	public static void main(String args[]) throws IOException {
		//设置文件路径
		File file = new File("C:\\Users\\summ\\Desktop\\1.txt");
		try {
			//创建一个字节输出流对象
			OutputStream outPutStream = new FileOutputStream(file);
			
			String str = "hello world, what is your name?";
			/**
			 *getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,
			 *并将结果存储到一个新的 byte 数组中。
			 */
			byte[] b = str.getBytes();
			
			/**
			 * write(byte[] b)将b.length个字节从byte数组写入此文件输出流中。
			 */
			outPutStream.write(b);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}


字符流读取文件:Reader

package charAndByte;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class reader {

	public static void main(String args[]) throws IOException {
		
		File file = new File("C:\\Users\\summ\\Desktop\\2.txt");
		
		Reader reader = new FileReader(file);
		
		BufferedReader bReader = new BufferedReader(reader);
		
		String str = null;
		
		while((str = bReader.readLine()) != null) {
			System.out.println(str);
		}
		
	}
}

字符流取文件:Writer

package charAndByte;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class writer {
	
	public static void main(String args[]) throws IOException {
		
		File file = new File("C:\\Users\\summ\\Desktop\\2.txt");
		
		//字符输出流
		Writer writer = new FileWriter(file);
		
		String str = "ni hao a , bei jing tian an men !";
		
		writer.write(str);
		
		writer.close();
		
	}
}

其中一些资料参考网络和API文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值