JAVA之IO

IO

在这里插入图片描述

InputStream

public static void main(String[] args) throws IOException {
		//创建对象
		File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
		InputStream is = new FileInputStream(file);
		//读一个byte
		int bit = is.read();
		System.out.println((char)bit);
		bit = is.read();
		System.out.println((char)bit);
	}
public static void main(String[] args) throws IOException {
		File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
		InputStream is = new FileInputStream(file);
		int num = 0;
		//循环读取所有字节
		while((num=is.read())!=-1) {
			System.out.println((char)num);
		}
		is.close();
	}
public static void main(String[] args) throws IOException {
		File file = new File("C:\\Users\\Gu\\Desktop\\abc.txt");
		InputStream is = new FileInputStream(file);
		
		byte[] b = new byte[1024];
		int len = is.read(b);
		String msg = new String(b,0,len);
		System.out.println(msg.toString());
		is.close();
	}
public static void main(String[] args) throws IOException {
		InputStream is = new FileInputStream("a.txt");
		
		ObjectInputStream ois = new ObjectInputStream(is);
		int num = ois.readInt();
		System.out.println(num);
		double d = ois.readDouble();
		System.out.println(d);
		String s =ois.readUTF();
		System.out.println(s);
	}

OutputStream

public static void main(String[] args) throws IOException {
		//创建对象
		OutputStream os = new FileOutputStream("a.txt");
		//声明写出数据
		int num = 97;
		//写出数据
		os.write(num);
		//关闭
		os.close();
	}
/*
 * 创建FileOutputStream对象时,第二个参数为boolean类型,
 * 说明是否在当前文件后面追加内容,默认是false,不追加
 */
public class Test5 {
	public static void main(String[] args) throws IOException {
		OutputStream os = new FileOutputStream("a.txt",true);
		String msg = "Happeniness";
		os.write(msg.getBytes(), 0, msg.length());
		os.close();
	}
/*
 * ObjectOutputStream写出的是对象
 */
public class Test {
	public static void main(String[] args) throws IOException {
		//创建对象
		OutputStream f = new FileOutputStream("a.txt");
		ObjectOutputStream os = new ObjectOutputStream(f);
		os.writeInt(123);
		os.writeDouble(10.2);
		os.writeUTF("Unhappiness");
		os.close();
	}

Reader

在这里插入图片描述

/*
 * 	Reader:所有字符输入流的父类
 */
public class Test1 {
	public static void main(String[] args) throws IOException {
		InputStream f = new FileInputStream("a.txt");
		Reader reader = new InputStreamReader(f);
		int num = reader.read();
		System.out.println(num);
		reader.close();
	}
public static void main(String[] args) throws IOException {
		InputStream f = new FileInputStream("a.txt");
		Reader reader = new InputStreamReader(f);
		int num =0;
		while((num=reader.read())!=-1) {
			System.out.println((char)num);
		}
		reader.close();
	}
public static void main(String[] args) throws IOException {
		InputStream f = new FileInputStream("a.txt");
		Reader reader = new InputStreamReader(f);
		//将读取到的信息存放在chs字符数组中。
		char[] chs = new char[1024];
		int count = reader.read(chs);
		System.out.println(new String(chs,0,count));
		reader.close();
	}
/*
 * Writer:所有字符输出流的父类
 * OutputStreamWriter:字符输出流
 */
public class Test {
	public static void main(String[] args) throws IOException {
		//创建流对象
		OutputStream os = new FileOutputStream("a.txt");
		Writer out = new OutputStreamWriter(os);
		//声明数据
		char ch = '帅';
		//输出数据
		out.write(ch);
		//刷新
		out.flush();
		//关闭
		out.close();
	}
}
public static void main(String[] args) throws IOException {
		//创建流对象
		OutputStream os = new FileOutputStream("a.txt");
		Writer out = new OutputStreamWriter(os);
		//声明数据
		String str = "你好";
		//输出数据
		out.write(str);
		//刷新
		out.flush();
		//关闭
		out.close();
	}
public static void main(String[] args) throws IOException {
		//创建流对象
		OutputStream os = new FileOutputStream("a.txt");
		Writer out = new OutputStreamWriter(os);
		
		String str = "whata Fuck";
		out.write(str.toCharArray());
		//刷新
		
		//关闭
		out.close();
	}

BufferedReader

/*
 * bufferedreader 读取效率更高
 */
public class Test06 {
	public static void main(String[] args) throws IOException {
		//创建对象
		BufferedReader reader = new BufferedReader(
					new InputStreamReader(
					new FileInputStream("a.txt")));
		//读取
		String str = null;
		StringBuilder sb = new StringBuilder();
		while((str=reader.readLine())!=null) {
			sb.append(str);
			sb.append("\r\n");//保留格式
		}
		//读数据
		System.out.println(sb.toString());
		//关闭
		reader.close();	
	}
}

BufferedWriter

public static void main(String[] args) throws IOException {
		//创建对象
		OutputStream os = new FileOutputStream("a.txt");
		Writer out = new OutputStreamWriter(os);
		BufferedWriter bw = new BufferedWriter(out);

		//写出数据
		bw.write("我自横刀向天笑");
		bw.newLine();
		bw.write("去留肝胆两昆仑");
		bw.flush();
		bw.close();
		out.close();
		os.close();
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值