IO-FileInputStream、FileOutputStream类

向文件中写入字符串

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

public class IOOperation {
	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 = "你好!";
		byte[] byteArr = str.getBytes();
		out.write(byteArr);
		out.close();
	}
}

运行结果:hello.txt写入“你好”

 

当然也可以一个字节一个字节的写。

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

public class IOOperation {
	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 = "你好!";
		byte[] byteArr = str.getBytes();
		for (byte b : byteArr) {
			out.write(b);
		}
		out.close();
	}
}

运行结果:hello.txt写入“你好”

 

向文件中追加新内容

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

public class IOOperation {
	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);
		String str = "\r\nRollen";
		byte[] byteArr = str.getBytes();
		for (byte b : byteArr) {
			out.write(b);
		}
		out.close();
	}
}

运行结果:hello.txt文件写入

你好!

Rollen

读取文件内容

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

public class IOOperation {
	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[] byteArr = new byte[1024];
		in.read(byteArr);
		in.close();
		System.out.println(new String(byteArr));
	}
}

运行结果:

你好!

Rollen


但是这个例子读取出来会有大量的空格,我们可以利用in.read(b);的返回值来设计程序。如下:

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

public class IOOperation {
	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[] byteArr = new byte[1024];
		int len = in.read(byteArr);
		in.close();
		System.out.println("读入长度为:" + len);
		System.out.println(new String(byteArr, 0, len));
	}
}

运行结果:

读入长度为:20

你好!

Rollen


读者观察上面的例子可以看出,我们预先申请了一个指定大小的空间,但是有时候这个空间可能太小,有时候可能太大,我们需要准确的大小,这样节省空间,那么我们可以

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

public class IOOperation {
	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);
		long len = f.length();
		byte[] byteArr = new byte[(int) len];
		in.read(byteArr);
		in.close();
		System.out.println("读入长度为:" + len);
		System.out.println(new String(byteArr));
	}
}

运行结果:

读入长度为:20

你好!

Rollen


将上面的例子改为一个一个读:

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

public class IOOperation {
	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);
		long len = f.length();
		byte[] byteArr = new byte[(int) len];
		for (int i = 0; i < len; i++) {
			byteArr[i] = (byte) in.read();
		}
		in.close();
		System.out.println("读入长度为:" + len);
		System.out.println(new String(byteArr));
	}
}

运行结果:和上面一样

 

上面的几个例子都是在知道文件的内容多大,然后才展开的,有时候我们不知道文件有多大,这种情况下,我们需要判断是否读到文件的末尾。

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

public class IOOperation {
	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[] byteArr = new byte[1024];
		int count = 0;
		int temp = 0;
		while ((temp = in.read()) != -1) {
			byteArr[count++] = (byte) temp;
		}
		in.close();
		System.out.println(new String(byteArr));
	}
}

运行结果:

你好!

Rollen

read方法,当独到文件末尾的时候会返回-1,正常情况下返回读到的值。

 

复制文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOOperation {
	public static void main(String[] args) throws IOException {
		String fileName1 = "D:" + File.separator + "hello.txt";
		String fileName2 = "E:" + File.separator + "hello.txt";
		File f1 = new File(fileName1);
		File f2 = new File(fileName2);
		if (!f1.exists()) {
			System.out.println("被复制的文件不存在!");
			System.exit(1);
		}
		InputStream in = new FileInputStream(f1);
		OutputStream out = new FileOutputStream(f2);
		if (in != null && out != null) {
			int temp = 0;
			while ((temp = in.read()) != -1) {
				out.write(temp);
			}
		}
		in.close();
		out.close();
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值