JAVA IO二(文件流)

JAVA IO—文件读写

一.FileInputStream

FileInputStream是从文件中获取字节数据,主要用于读取原始字节流,如图像数据,音频,视频等,也可以读取字符流数据。但是,对于读取字符流,建议使用FileReader类。

public static void main(String[] args) {
		try {
			FileInputStream inputStream = new FileInputStream("D:\\test.txt");

			String originalData = "读取原始数据:"; 
			String charData = "读取字符:";
			int ret =  inputStream.read();
			while (ret != -1) {
				originalData = originalData + ret;
				charData = charData + (char)ret;
				ret = inputStream.read();
			}
			System.out.println(originalData);
			System.out.println(charData);
			inputStream.close();

			inputStream = new FileInputStream("D:\\test.txt");
			byte[] charByte = new byte[1024];
			int len = inputStream.read(charByte);
			String str = new String(charByte, 0, len);
			System.out.println(str);
			inputStream.close();	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

执行结果:

读取原始数据:10410110810811132119111114108100
读取字符:hello world
hello world

二.FileReader

FileReader用于从文件中读取字符流,这跟FileInputStream很像,不同的是FileInputStream读取字节流,与编码无关,而FileReader读取的是字符,跟编码有关,一个字符可能对应一个或多个字节。

public static void main(String[] args) {
		try {
			Reader reader = new FileReader("D:\\test.txt");

			char[] charByte = new char[1024];
			int len = reader.read(charByte);
			String str = String.valueOf(charByte);
			System.out.println("文本长度:" + len);
			System.out.print(str);
			reader.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

运行结果

文本长度:4
我爱中国

“我爱中国”这四个字占8个字节,但这里的长度是字符,所以为4。

三.FileOutputStream

FileOutputStream与FileInputStream正好相反,用于将字节流写入到文件中,如果文件不存在,则会自动创建,如果文件存在,则会覆盖已有文件。

public static void main(String args[]){    
        try{    
          FileOutputStream outStream=new FileOutputStream("D:\\test.txt");    
          outStream.write("hello word".getBytes());       
          outStream.close();       
         }catch(Exception e){System.out.println(e);}    
   }

四.FileWriter

FileWriter用于将字符流写入到文件,与FileReader功能相反。与FileOutputStream类似。

public static void main(String args[]) {
		try {
			FileWriter writer = new FileWriter("D:\\test.txt");
			writer.write("hello world");
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

五.RandomAccessFile

RandomAccessFile用于读取或写入从文件任何位置的字节流。

	static final String FILEPATH = "d:/test.txt";

	public static void main(String[] args) {
		try {
			String str = new String(readFromFile(FILEPATH, 0, 18));
			System.out.println(str);
			writeToFile(FILEPATH, "hello world", 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static byte[] readFromFile(String filePath, int position, int size) throws IOException {
		RandomAccessFile file = new RandomAccessFile(filePath, "r");
		file.seek(position);
		byte[] bytes = new byte[size];
		file.read(bytes);
		file.close();
		return bytes;
	}

	private static void writeToFile(String filePath, String data, int position) throws IOException {
		RandomAccessFile file = new RandomAccessFile(filePath, "rw");
		file.seek(position);
		file.write(data.getBytes());
		file.close();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值