第十周课程总结

将英文字母的奇数改为大写。
代码:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class Chake {
	public static void main(String[] args) throws Exception{                         //异常抛出,不处理
		File f=new File("d:"+File.separator+"360downloads"+File.separator+"demo.txt");             //声明File对象
		OutputStream out=null;                       //准备好一个输出对象
		out=new FileOutputStream(f);           //通过对象多态性,进行实例化
		String str="abcdef";                           //准备一个字符串
		byte b[]=str.getBytes();                       //只能输出byte数组,所以将字符串变成byte数组
		for(int i=0;i<b.length;i++) {
			if(i%2==0&&b[i]>='a'&&b[i]<='z') {             //判断是否是奇数以及是否是英文字母
				b[i]=(byte) (b[i]-32);
			}
			out.write(b[i]);	                 //将内容输出,保存文件
		}
		out.close();
	}

}

实验截图:

总结:
在Java中IO操作流程:
(1)使用File类打开一个文件。
(2)通过字节流或字符流的子类指定输出的位置。
(3)进行读/写操作。
(4)关闭输入/输出。

字节输出流:OutputStream
public abstract class OutputStream
extends Object
implements Closeable,Flushable

字节输入流:InputStream
public abstract class IntputStream
extends Object
implements Closeable

字符输出流:Writre
public abstract class Writre
extends Object
implements Appendable,Closeable,Flushable

字符输入流:Reader
public abstract class Reader
extends Object
implements Readable,Closeable

原文地址:https://www.cnblogs.com/tzmad/p/11770731.html