java基础复习总结7

一个File类的对象,表示了磁盘上的文件或目录 当无法操作,要操作要用到后面学到的类

file 的构造方法

File myFile = new File(" myfile. txt");
File myDir = new File(" MyDocs");
File  myFile = new File( myDir, "myfile. 
txt");
public class FileTest6
{
	public static void main(String[] args)
	{
		File file = new File("C:\\abc\\xyz\\hello");
		
		String[] names = file.list();
		
		for(String name : names)
		{
			if(name.endsWith(".java"))
			{
				System.out.println(name);
			}
		}

 一个用来显示目录结构的。java文件

public class ListAllTest
{
	//用于判断目录或文件所处的层次
	private static int time;
	
	// 递归的方法
	public static void deepList(File file)
	{
		if (file.isFile() || 0 == file.listFiles().length)
		{
			return;
		}
		else
		{
			File[] files = file.listFiles();
			
			files = sort(files);
			
			for(File f : files)
			{
				StringBuffer output = new StringBuffer();
				
				if(f.isFile())
				{
					output.append(getTabs(time));
					output.append(f.getName());
				}
				else
				{
					output.append(getTabs(time));
					output.append(f.getName());
					output.append("\\");
				}
				
				System.out.println(output);
				
				if(f.isDirectory())
				{
					time++;
					
					deepList(f);
					
					time--;
				}
			}
		}
	}

	// 整理文件数组,使得目录排在文件之前
	private static File[] sort(File[] files)
	{
		ArrayList<File> sorted = new ArrayList<File>();

		// 寻找到所有的目录
		for (File f : files)
		{
			if (f.isDirectory())
			{
				sorted.add(f);
			}
		}
		// 寻找到所有的文件
		for (File f : files)
		{
			if (f.isFile())
			{
				sorted.add(f);
			}
		}

		return sorted.toArray(new File[files.length]);
	}
	
	//判断需要加多少 tab的方法
	private static String getTabs(int time)
	{
		StringBuffer buffer = new StringBuffer();
		
		for(int i = 0; i < time; i++)
		{
			buffer.append("\t");
		}
		
		return buffer.toString();
	}
	
	
	public static void main(String[] args)
	{
		File file = new File("C:\\Projects\\wsclient");
		
		deepList(file);
	}
	
}

 

 FilenameFilter 

仅定义了一个方法,accept( )。该方法被列表中的每个文件调用一次。它的通常形式如下:boolean accept(File directory, String filename)

 

public class FileTest7
{
	public static void main(String[] args)
	{
		File file = new File("C:\\abc\\xyz\\hello");

		String[] names = file.list(new FilenameFilter()
		{
			public boolean accept(File dir, String name)
			{
				if(name.endsWith(".txt"))
				{
					return true;
				}
				
				return false;
			};
		});
		
		for(String name : names)
		{
			System.out.println(name);
		}
		
	}
}

    数据读取逻辑 :

open a stream
while more information
read information
close the stream

 

public class InputStreamTest1
{
	public static void main(String[] args) throws Exception
	{
		InputStream is = new FileInputStream("c:/hello.txt");
		
		byte[] buffer = new byte[200];
		
		int length = 0;
		
		while(-1 != (length = is.read(buffer, 0, 200)))
		{
			String str = new String(buffer,0, length);
			
			System.out.println(str);
		}
			
		is.close();	
			
	}
}

 

    写数据的逻辑为:
open a stream
while more information
write information
close the stream

public class OutputStreamTest1
{
	public static void main(String[] args) throws Exception
	{
		OutputStream os = new FileOutputStream("c:\\out.txt", true);
		
		String str = "aaaaa";
		
		byte[] buffer = str.getBytes();
		
		os.write(buffer);
		
		os.close();
	}
}

 

• FileInputStream和FileOutputStream
节点流,用于从文件中读取或往文件中写入字节流。如果在构造
FileOutputStream时,文件已经存在,则覆盖这个文件。
• BufferedInputStream和 BufferedOutputStream
过滤流,需要使用已经存在的节点流来构造,提供带缓冲的读写,提高了读
写的效率。
• DataInputStream和DataOutputStream 过滤流,需要使用已经存在的节点流来构造,提供了读写Java中的基本数 据类型的功能。
• PipedInputStream和PipedOutputStream
管道流,用于线程间的通信。一个线程的PipedInputStream对象
从另一个线程的PipedOutputStream对象读取输入。要使管道流有 用,必须同时构造管道输入流和管道输出流。

• 过滤流的主要特点是在输入输出数据的同时能对所传输的
数据做指定类型或格式的转换,即可实现对二进制字节数
据的理解和编码转换。
• 数据输入流DataInputStream中定义了多个针对不同类型
数 据 的 读 方 法 , 如 readByte() 、 readBoolean() 、
readShort()、readChar()、readInt()、readLong()、
readFloat()、readDouble()、readLine()等。
• 数据输出流DataOutputStream中定义了多个针对不同类型
数据的 写 方法 , 如 writeByte() 、writeBoolean() 、
writeShort()、writeChar()、writeInt()、writeLong()、
writeFloat()、writeDouble()、writeChars()等。

public class DataStream1
{
	public static void main(String[] args) throws Exception
	{
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(
				new FileOutputStream("data.txt")));

		byte b = 3;
		int i = 12;
		char ch = 'a';
		float f = 3.3f;

		dos.writeByte(b);
		dos.writeInt(i);
		dos.writeChar(ch);
		dos.writeFloat(f);

		dos.close();

		DataInputStream dis = new DataInputStream(new BufferedInputStream(
				new FileInputStream("data.txt")));
		
		//读和写的顺序要保持一致
		System.out.println(dis.readByte());
		System.out.println(dis.readInt());
		System.out.println(dis.readChar());
		System.out.println(dis.readFloat());
		
		dis.close();
	}
}

 

缓冲输入/输出是一个非常普通的性能优化。Java
的BufferedInputStream 类允许把任何InputStream
类“包装”成缓冲流并使它的性能提高
BufferedInputStream 有两个构造方法
– BufferedInputStream(InputStream inputStream)
– BufferedInputStream(InputStream inputStream, int bufSize)
– 第一种形式创建BufferedInputStream流对象并为以后
的使用保存InputStream参数in,并创建一个内部缓冲
区数组来保存输入数据。
– 第二种形式用指定的缓冲区大小size创建
BufferedInputStream流对象,并为以后的使用保存
InputStream参数in。

 

public class BufferedOutputStreamTest1
{
	public static void main(String[] args) throws Exception
	{
		OutputStream os = new FileOutputStream("1.txt");
		
		BufferedOutputStream bos = new BufferedOutputStream(os);
		
		bos.write("http://www.google.com".getBytes());
		
		bos.close();
		os.close();
	}
}

 

• ByteArrayOutputStream是一个把字节数组当作输
出流的实现。ByteArrayOutputStream 有两个构造
方法
– ByteArrayOutputStream( )
– ByteArrayOutputStream(int numBytes)
– 在第一种形式里,一个32位字节的缓冲区被生成。第
二个构造方法生成一个numBytes大小的缓冲区。缓冲
区保存在ByteArrayOutputStream的受保护的buf成员里。
缓冲区的大小在需要的情况下会自动增加。缓冲区保
存的字节数是由ByteArrayOutputStream的受保护的
count域保存的

public class ByteArrayInputStreamTest1
{
	public static void main(String[] args)
	{
		String temp = "abc";
		
		byte[] b = temp.getBytes();
		
		ByteArrayInputStream in = new ByteArrayInputStream(b);
		
		for(int i = 0; i < 2; i++)
		{
			int c;
			
			while(-1 != (c = in.read()))
			{
				if(0 == i)
				{
					System.out.println((char)c);
				}
				else
				{
					System.out.println(Character.toUpperCase((char)c));
				}
			}
					
			System.out.println();
			
			in.reset();
			
			
		}
		
	}
}

 字符流的使用 先使用 inputstreamreader 类 和 outputstreamwriter 包装一个 file 输入输出流 通过这两个类搭建字符 到 字节 转换的桥梁 包装成一个可以用来 做字符 输入输出流

public class StreamTest
{
	public static void main(String[] args) throws Exception
	{
		FileOutputStream fos = new FileOutputStream("file.txt");
		
		OutputStreamWriter osw = new OutputStreamWriter(fos);
		
		BufferedWriter bw = new BufferedWriter(osw);
		
		bw.write("http://www.google.com");
		bw.write("\n");
		bw.write("http://www.baidu.com");
		
		bw.close();
		
		FileInputStream fis = new FileInputStream("file.txt");
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		
		
		String str = br.readLine();
		
		while(null != str)
		{
			System.out.println(str);
			
			str = br.readLine();
		}
		
		br.close();
		
		
	}

 

将标准的输入设备包装成输入流

public class StreamTest2
{	
	public static void main(String[] args) throws IOException
	{
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		
		String str;
		
		while(null != (str = br.readLine()))
		{
			System.out.println(str);
		}
		
		br.close();
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值