The File Class
public static void main(String[] args) {
//创建File对象
File file = new File("D:\\Android");
//获取该目录下的所有文件
String[] files = file.list();
String[] files = file.list(new DirFilter(".*"));
//listFiles是获取该目录下所有文件和目录的绝对路径
File[] fs = file.listFiles();
}
class DirFilter implements FilenameFilter {
private Pattern pattern;
public DirFilter(String regex) {
pattern = Pattern.compile(regex);
}
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
}
You can use a File object to create a new directory or an entire directory path if it doesn't exist. You can also look at the characteristics of files (size, last modification date, read/write), see whether a File object represents a file or a directory, and delete a file.
FileUtils from Apache Common
方法 | 描述 |
---|---|
write(File file, CharSequence data) | ... |
... | ... |
https://blog.csdn.net/wangmx1993328/article/details/80136921
Collection<File> listFiles(final File directory, final IOFileFilter fileFilter, final IOFileFilter dirFilter)
Collection<File> listFiles(final File directory, final String[] extensions, final boolean recursive)
1)directory:被遍历的目录,必须是已经存在的目录,否则报错
2)fileFilter:文件过滤器,IOFileFilter 是一个接口,常用的实现类有:SuffixFileFilter(文件后缀过滤器)、PrefixFileFilter(文件前缀过滤器)、TrueFileFilter(总是返回true的文件过滤器)、FalseFileFilter(总是返回false的文件过滤器)
3)dirFilter:与上面同理
4)extensions:如果为 null ,则底层用的是 TrueFileFilter.INSTANCE,否则用的是 new SuffixFileFilter(suffixes)
5)recursive:如果为 true,则底层用的是 TrueFileFilter.INSTANCE,否则 false 时 用 FalseFileFilter.INSTANCE
6)如下所示为 listFiles( final File directory, final String[] extensions, final boolean recursive) 的源码:
Input and Output
Types of InputStream
Class | Function | Constructor arguments | How to use it |
---|---|---|---|
ByteArrayInputStream | Allows a buffer in memory to be used as an InputStream. | The buffer from which to extract the bytes. | As a source of data: Connect it to a FilterlnputStream object to provide a useful interface. |
StringBufferInputStream | Converts a String into an InputStream. | A String. The underlying implementation actually uses a StringBuffer. | As a source of data: Connect it to a FilterlnputStream object to provide a useful interface. |
FileInputStream | For reading information from a file. | A String representing the file name, or a File or FileDescriptor object. | As a source of data: Connect it to a FilterlnputStream object to provide a useful interface. |
PipedInputStream | Produces the data that’s being written to the associated PipedOutput-Stream. Implements the "piping" concept. | PipedOutputStream | As a source of data in multithreading: Connect it to a FilterlnputStream object to provide a useful interface. |
SequenceInputStream | Converts two or more InputStream objects into a single InputStream. | Two InputStream objects or an Enumeration for a container of InputStream objects. | As a source of data: Connect it to a FilterlnputStream object to provide a useful interface. |
FilterInputStream | Abstract class that is an interface for decorators that provide useful functionality to the other InputStream classes. |
Types of FilterInputStream
Class | Function | Constructor arguments | How to use it |
---|---|---|---|
Data-InputStream | Used in concert with DataOutputStream, so you can read primitives (int, char, long, etc.) from a stream in a portable fashion. | InputStream | Contains a full interface to allow you to read primitive types. |
BufferedInputStream | Use this to prevent a physical read every time you want more data. You’re saying, "Use a buffer." | InputStream, with optional buffer size. | This doesn’t provide an interface per se. It just adds buffering to the process. Attach an interface object. |
LineNumber-InputStream | Keeps track of line numbers in the input stream; you can call getLineNumber( ) and setLineNumber (int). | InputStream | This just adds line numbering, so you’ll probably attach an interface object. |
Pushback-InputStream | Has a one-byte pushback buffer so that you can push back the last character read. | InputStream | Generally used in thescanner for a compiler. You probably won’t use this. |
Types of FilterOutputStream
Class | Function | Constructor arguments | How to use it |
---|---|---|---|
DataOutputStream | Used in concert with DataInputStream so you can write primitives (int, char, long, etc.) to a stream in a portable fashion. | OutputStream | Contains a full interface to allow you to write primitive types. |
PrintStream | For producing formatted output. While DataOutputStream handles the storage of data, PrintStream handles display. | OutputStream, with optional boolean indicating that the buffer is flushed with every newline. | Should be the "final" wrapping for your OutputStream object. You’ll probably use this a lot. |
BufferedOutputStream | Use this to prevent a physical write every time you send a piece of data. You’re saying, "Use a buffer." You can call flush( ) to flush the buffer. | OutputStream, with optional buffer size. | This doesn’t provide an interface per se. It just adds buffering to the process. Attach an interface object. |
RandomAccessFile
public class UsingRandomAccessFile {
static String file = "rtest.dat";
static void display() throws IOException {
RandomAccessFile rf = new RandomAccessFile(file, "r");
for(int i = 0; i < 7; i++)
System.out.println(
"Value " + i + ": " + rf.readDouble());
System.out.println(rf.readUTF());
rf.close();
}
public static void main(String[] args)
throws IOException {
RandomAccessFile rf = new RandomAccessFile(file, "rw");
for(int i = 0; i < 7; i++)
rf.writeDouble(i*1.414);
rf.writeUTF("The end of the file");
rf.close();
display();
rf = new RandomAccessFile(file, "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
display();
}
}
Piped streams
The PipedInputStream, PipedOutputStream, PipedReader and PipedWriter have been mentioned only briefly in this chapter. This is not to suggest that they aren’t useful, but their value is not apparent until you begin to understand concurrency, since the piped streams are used to communicate between tasks. This is covered along with an example in the Concurrency chapter.