第十周课程总结

一、判断奇位数,若为小写则转为大写

1.实验源码:

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

public class text {

    public static void main(String[] args) throws IOException {



        try {
            String str = "hello Java";
            String s = "d:"+File.separator+"text.txt";

            File file = new File(s);
            FileOutputStream ops = new FileOutputStream(file);

            byte b[] = str.getBytes();
            for(int i=0;i<b.length;i++) {
                if (i%2==0 && b[i]>='a' && b[i]<='z') {
                     b[i]=(byte)(b[i]-32);
                }
            }
            ops.write(b);
            ops.close();
            System.out.println("内容为:"+new String(b));
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

  }
}

截图:

字节流与字符流

Java io操作中以文件形式,有四个步骤:

1,使用File类打开一个文件。

2,通过字节流或字符流的子类指定输出的位置。

3,进行读写操作。

4,关闭输入输出。

字节流中outputStream类inputStream类的常用方法在书的380,384

字符流中writer类与reader类的常用方法在388,390

12-27 09:16
查看更多