第十周课程总结

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

实验源码

package test1;

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

public class Dacument {
public static void main(String[] args) throws Exception {
	File h=new File("d:"+File.separator+"Java源代码"+File.pathSeparator+"test.txt");//声明对象
	OutputStream out=new FileOutputStream(h);//准备一个输出对象
	String s="HELLO WORLD!!";
	byte c[]=s.getBytes();//将字符串转为字节型
	out.write(c);//将内容输出,保持文件
	out.close();//关闭输出流
	File f=new File("d:\\Java源代码\\test.txt");//声明对象
	byte[] b=new byte[(int)f.length()];//定义数组存储对象f中的内容
	InputStream in=new FileInputStream(f);//准备好一个输入对象
		for(int i=0;i<b.length;i++)
		{
			b[i]=(byte)in.read();//将对象中的内容转为字节存储到数组b中
		}
		for(int i=0;i<b.length;i+=2)
			if(b[i]>=65&&b[i]<=90)//判断奇数位是否为大写
				b[i]+=32;//大写转为小写
	in.close();//关闭输入流
	System.out.println(new String(b));
}
}

实验结果

二、File类

唯一表示与文件本身有关的类

public File(String pathname)//传递的内容为文件路径

1.File类中的主要方法和常量

序号 方法或常量 类型 描述
1 public static final String pathSeparator 常量 表示路径分隔符(windows是:“;”)
2 public static final String separator 常量 表示路径分隔符(windows是“\”)
3 public File(String pathname) 构造 创建File类对象,传入完整路径
4 public File(File parent,String child) 构造 根据指定父路径创建子文件
5 public bolean creatNewFile()throws IOException 普通 创建新文件
6 public boolean delete() 普通 删除文件
7 public boolean exists() 普通 判断文件是否存在
8 public boolean isDirectory() 普通 判断给定路径是否是一个目录
9 public long length() 普通 返回文件大小
10 public File[] list() 普通 列出指定目录的全部内容,只是列出了名称
11 public File[] listFile() 普通 列出指定目录的全部内容,只会列出路径
12 public boolean mkdir() 普通 创建一个目录
13 public boolean mkdirs() 普通 创建一个多级目录
14 public boolean renameTo(FIie dest) 普通 为已有的文件重新命名
15 public long lastModifide() 普通 取得文件最后一次修改日期时间
16 public File getParentFile() 普通 取得当前路径的父路径

2.字节流

字节输出流:OutputStream
OutputStream类的常用方法

序号 方法或常量 类型 描述
1 public void close()throws IOException 普通 关闭输出流
2 public void flush()throws IOException 普通 刷新缓冲区
3 public void write(byte[] b)throws IOException 普通 将一个byte数组写入数据流
4 public void write(byte[] b,int off,int len)throws IOException 普通 将一个指定范围的byte数组写入数据流
5 public abstract void write(int b)throws IOException 普通 将一个字节数据写入数据流

追加新内容

public FileOutputStream(File file,boolean append)throws FileNotFonundException

如果append的值设置为true,则表示在文件的末尾追加内容。
字节输入流:IntputStream
IntputStream类的常用方法

序号 方法或常量 类型 描述
1 public int available() throws IOException 普通 可以取得输入文件的大小
2 public void close()throws IOException 普通 关闭输入流
3 public abstract int read()throws IOException 普通 读取内容,以数字的方式读取
4 public int read(byte[] b)throws IOException 普通 将内容读到byte数组之中,同时返回读入的个数

3.字符流

字节输出流:Write
Write类的常用方法

序号 方法或常量 类型 描述
1 public abstract void close()throws IOException 普通 关闭输出流
2 public void write(String str)throws IOException 普通 将字符串输出
3 public void write(char[] cbuf)throws IOException 将字符数组输出
4 public abstract void flush()throws IOException 强制性清除缓存

追加新内容

public FileWrite(File file,boolean append)throws FileNotFonundException

字符输入流:Reader
Reader类的常用方法

序号 方法或常量 类型 描述
1 public abstract void close()throws IOException 普通 关闭输出流
2 public void read()throws IOException 普通 读取单个字符
3 public void read(char[] cbuf)throws IOException 将内容读到字符数组中,返回读入的长度
posted @ 2019-11-01 12:03  L磊  阅读(245)  评论(0编辑  收藏  举报