实现accept()方法就是 指定自己的规则,指定哪些文件应该由list()方法列出。
stream 流 就是键盘、文件、网络连接等不同的输入输出源。
import java.io.*;
public class FilenameFilterTest
{
public static void main(String[] args)
{
File file = new File(".");
String[] nameList = file.list( (dir, name) -> name.endsWith(".java") || new File(name).isDirectory() ); //条件就是要么是个.JAVA结尾 要么是个路径。
/*String[] nameList=file.list(
new FilenameFilter()
{
@Override
public boolean accept(File dir,String name)
{
return (name.endsWith(".java"))||(new File(name).isDirectory());
//是否已.java结尾 || 是否是路径
}
}
);*/
for(String name : nameList)
{
System.out.println(name);
}
}
}
stream 流 就是键盘、文件、网络连接等不同的输入输出源。
import java.io.*;
public class FileInputStreamTest
{
public static void main(String[] args) throws IOException
{
// 创建字节输入流
FileInputStream fis = new FileInputStream(
"FileInputStreamTest.java");
// 创建一个长度为1024的“竹筒”
byte[] bbuf = new byte[1024];
// 用于保存实际读取的字节数
int hasRead = 0;
// 使用循环来重复“取水”过程
while ((hasRead = fis.read(bbuf)) > 0 )
{
// 取出“竹筒”中水滴(字节),将字节数组转换成字符串输入!
System.out.print(new String(bbuf , 0 , hasRead ));
}
// 关闭文件输入流,放在finally块里更安全
fis.close();
}
}
import java.io.*;
public class FileReaderTest
{
public static void main(String[] args)
{
try(
// 创建字符输入流
FileReader fr = new FileReader("FileReaderTest.java"))
{
// 创建一个长度为32的“竹筒”
char[] cbuf = new char[32];
// 用于保存实际读取的字符数
int hasRead = 0;
// 使用循环来重复“取水”过程
while ((hasRead = fr.read(cbuf)) > 0 )
{
// 取出“竹筒”中水滴(字符),将字符数组转换成字符串输入!
System.out.print(new String(cbuf , 0 , hasRead));
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}