题目:使用Java IO 将奇数位小写改为大写
实验代码:
package Java;
import java.io.File;
import java.io.FileOutputStream;
public class 大写 {
public static void main(String[] args) throws Exception{
File f=new File("d:"+File.separator+"test.txt");
FileOutputStream out=null;
out=new FileOutputStream(f);
String str="helloTomorrow";
byte b[]=str.getBytes();
for(int i=0;i<b.length;i++) {
if(i%2==0) {
out.write((byte) (b[i]-32));
}
else {
out.write(b[i]);
}
}
out.close();
}
}
运行结果截图:
学到了什么:
字节流与字符流的基本操作
字节输出流:OutputStream
public abstruct class OutputStream
extends Object
implments Closeable,Flushable
向文件中写入字符串
package Java;
import java.io.File;
import java.io.FileOutputStream;
public class OutputStream {
public static void main(String[] args) throws Exception{
File f = new File("d:"+File.separator+"test.txt");
FileOutputStream out = null;
out = new FileOutputStream(f);
String str = "Hello World";
byte b[] = str.getBytes();
out.write(b);
out.close();
}
}
字节输入流:InputStream
public abstract class InputStream
extends Object
implements Clossable
从文件中读取内容:
package Java;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class JavaIO {
public static void main(String[] args)throws Exception{
File f = new File("d:"+File.separator+"test.txt");
InputStream input=null;
input = new FileInputStream(f);
byte b[]=new byte[1024];
input.read(b);
input.close();
System.out.println("内容为:"+new String(b));
}
}
字符输出流:Writer
public abstract class Write
extends Object
implements Appendable,Closeable,Flushable
FileWrite 类的构造方法定义:
public FileWrite(File file) throws IOException
字符输入流:Reader
定义:
public abstract class Reader
extends Object
implements Readable,Closeable
学习不足及改进
1.不能写出程序流程图,
2.无法保证程序的完备性(每次写完一个程序都需要修改)
3.还需照着书本写出相应代码,有些类的常用方法不太清楚