第七次作业

**1:**Java中流从流动方向看分为输出流和输入流。以读取的类型来看分为字符流和字节流,这两者之间可以相互转换。
2:Inputstream的子类
(1)FileInputstream在文件读取时使用
(2)PipedInputsream在进行管道接收时使用
(3)ObjectInputstream在对象串行化时使用
Outputstream的子类
(1)FileOutputstream在文件写时使用
(2)PipedOutputsream在进行管道输出时使用
(3)ObjecOutputtstream在对象串行化时使用

**3:**字节流与字符流转换时的方法
(1)输入时需要使用InputstreamReader的构造方法如InputstreamReader(Inputstream in)
(2)输出时需要使用OutputstreamWriterr的构造方法如OutputstreamWriter(Outputstream out)
4:过滤流是在一定的条件下筛选出符合条件的文件提高查找效率。
常见的过滤流有FilterInputsream(Inputstream in)和FilterOutputstream(Outputstream out)两种方法
5:对象的序列化和反序列化
序列化就是将类对象通过串行化转换成流写入外存中相反反序列化就是将外存中的流转换成类对象。
java通过Serializable接口实行对象的串行化。
6File类表示对文件进行相关操作的类。File类提供了计算机对文件的读写操作功能以及遍历目录创建文件等等cmd命令行的功能。
**7:**java对文件的读写提供的操作
(1)FileOutputstream和FileWriter
(2)FileInputStream和FileReader
3``要求建立一个缓冲区,将字节输入流中的内容转为字符串

import java.io.*;
public class test {
	 static String loadStream(InputStream in) throws IOException {
	        StringBuffer buffer = new StringBuffer();  
	        int n = 1,i=5;
	        byte[] buf = new byte[n];
	        while(i-->0) {
	        	in.read(buf,0,n);
	        	System.out.println(new String(buf));
	            buffer.append(new String(buf)); 
	        }
	        return new String(buffer);  
	  }
	public static void main(String[] args) throws IOException {
		InputStream in = new BufferedInputStream(System.in);
		String test=loadStream(in);
		System.out.println("result=: "+test);
	}
}

4将一个字符串转为字节数组输入流,将这个流中所有小写字母换成大写字母并写入字节数组输出流中,再将数组输出流转为字符串

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class test {
public static void main(String[] args) {
	String str = "Hello World!"; //源字符串
	//字节数组输入流
	ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
	//字节数组输出流
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	//循 环读取
	int b = -1;
	while((b = bais.read()) != -1){
		//把小写字母转换成大写字母
		System.out.print(" "+(char)b);
		if(b >= 97 && b <= 122){ //大写字母从 65~90, 小写字母从 97~122
			b -= 32;
		}
		//写回到字节数组输出流中
		baos.write(b);
	}
	System.out.println();
	//输出,并把字节数组输出流转换成字符串
	String out = baos.toString();
	System.out.println(out);
	}
}

5将文件中的内容转为字符串

import java.io.*;
public class test {
	static public String loadFile(String filename) {
	    File file = new File( filename );//建立文件对象
	    try {
	        Reader rdr = new FileReader( file );//建立读文件对象
	        long sz = file.length();
	        //一次全部读入
	        char[] ch = new char[(int)sz]; 
	        rdr.read(ch);
	        rdr.close();
	        return new String(ch);//变为字符串
	    } catch(IOException ioe) {
	        return null;
	    }
	}
	public static void main(String[] args) {
		String fname = "test.txt";
		String test=loadFile(fname);
		System.out.println(test);
	}
}

6将字符串contents中的内容写入文件filename中


mport java.io.*;
public class test {
	static public boolean saveFile(String filename, String contents) {
	    try {
	        File file = new File( filename );
	        if(!file.exists()) {
	              file.createNewFile();
	        }
	        Writer wtr = new FileWriter( file );
	        char[] ch = contents.toCharArray();
	        wtr.write(ch);
	        wtr.close();
	        return true;
	    } catch(IOException ioe) {
	        return false;
	    }
	} 
	public static void main(String[] args) throws IOException {
		String fname = "test.txt";
		String contents = "hello world!";
		if(saveFile(fname,contents))System.out.println("contents is save!");
	}
}

8实现文件查找功能。提供两个参数,第一个参数为查找的起始路径,第二个参数为要查找的文件名称。如果找到,则给出文件的完整路径,否则提示文件不存在

import java.io.File;
public class test
{
	String path = null;
	public String searchFile(File dir, String fileName) 
	{
		if(dir.isFile())
		{
			if(dir.getName().equals(fileName))
				path = dir.getAbsolutePath();
		}
		if (dir.isDirectory())
		{
			File[] files = dir.listFiles();
			File f ;
			for (int i=0;i<files.length;i++ ){
				f = files[i];
				path = searchFile(f, fileName);
			}				
		}
		return path;
	}
 
	public static void main(String[] args) {
		File dir = new File("D:/360Downloads/");
		String fileName = "filelist.dat";	
		if (!dir.exists())
		{
			System.out.println("目录不存在:" + dir);
			System.exit(0);
		}
		String path = new test().searchFile(dir,fileName);
		if(path!=null)
			System.out.println("文件完整路径:" + path);
		else
			System.out.println("不存在此文件");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值