In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you about the basic knowledge of Java and the relevant knowledge points about the operation of File files. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
The principle and Classification of ♒ I-peg O-flow
Input O is an acronym for Input and Output, which is a very practical technology for dealing with data transfer (such as reading / writing files, network communication).
In the Java program, the input / output of data is carried out in the form of stream
Various stream classes and interfaces are provided under the java.io package to obtain different kinds of data, and to input and output data through methods.
File stream: files operate in the form of streams in the program
Input stream: the path of data from the data source (file) to the program (memory)
Output stream: the path of data from the program (memory) to the data source (file)
The classification of I-O-stream
According to the operating data units, it is divided into: byte stream (binary file), character stream (text file).
According to the flow direction of the data, it is divided into input stream and output stream.
According to the role of flow, it is divided into node flow and processing flow.
Abstract base class byte stream character stream input stream InputStreamReader output stream OutputStreamWriter
The architecture of ♨️ I PUBO
File (File) concept
What is a file?
Files are not new to us. Files are places where data are stored, such as word documents, txt text, excel files, pictures, videos. And so on are all files, and the operating system manages the data on the disk in units of files. From the point of view of data storage, all files are essentially the same, consisting of bytes and, in the final analysis, 0-1 bit strings.
Folder (directory)
If multiple files are not classified together, it will be very inconvenient for users to use. Therefore, the mechanism of tree directory (also known as folder) is introduced, in which files can be placed in different folders, and folders can also be nested. This makes it easy for users to manage and use files.
✍️ common operations (File class)
Create file object-related constructors and methods
New File (String pathname); / / build a File object based on the path
New File (File parent,String child); / / build based on parent directory file + subpath
New File (String parent,String child); / / build based on parent directory path + subpath
CreateNewFile (); / / create a new file
Under disk E, create the file test01.txt\ test02.txt\ test03.txt as above
Import java.io.File;import java.io.IOException;public class FileCreate {public static void main (String [] args) throws IOException {/ / Mode 1 String pathname = "e:\\ test01.txt"; File file1 = new File (pathname); file1.createNewFile (); / / Mode 2 File parentfile = new File ("e:\\"); String child2 = "test02.txt" File file2 = new File (parentfile, child2); file2.createNewFile (); / / Mode 3 String parent = "e:\\"; String child3 = "test03.txt"; File file3 = new File (parent, child3); file3.createNewFile ();}}
Get information about the file
Get.getName (); / / get the file name
CanRead (); / / whether the file is readable
CanWrite (); / / whether the file is writable
GetAbsoultePath (); / / get the absolute path of the file
GetPath (); / / relative path
GetParent (); / / get the parent directory of the file
Lenth (); / / File size (in bytes)
Exists (); / / determine whether the file exists
IsFile (); / / determine whether it is a file or not
IsDirectory (); / / determine whether it is a directory
Import java.io.File;public class FileInfomation {public static void main (String [] args) {/ / create file object File file = new File ("e:\\ test01.txt"); System.out.println ("file name:" + file.getName ()); System.out.println ("whether the file is readable:" + file.canRead ()) System.out.println ("whether the file is writable:" + file.canWrite ()); System.out.println ("File absolute path:" + file.getAbsolutePath ()); System.out.println ("File size (bytes):" + file.length ()); System.out.println ("whether the file exists:" + file.exists ()) System.out.println ("is it a file:" + file.isFile ()); System.out.println ("is a directory:" + file.isDirectory ());}}
File comparison
File f1=new File ("D:\\ test1.txt"); File f2=new File ("D:\\ test2.txt"); F1 compare the address of the two objects f1.equals (f2); / / compare the paths of the files corresponding to the two objects
Directory operations and file deletion
Mkdir (); / / create a single-layer directory
Mkdirs (); / / create a multi-tier directory
Delete (); / / Delete the directory (this layer directory must be empty and have no content)
View file directory
List (); / / returns an array of strings naming the files and directories in the directory represented by this abstract pathname.
ListFiles (); / / returns an array of abstract pathnames that represent the files in the directory represented by the abstract pathname.
Case column: traverses all files in a directory for printout
Public class PrintFile {public static void main (String [] args) {/ / create a file object File file = new File ("e:\\ Test"); String [] list = file.list (); / / an array of names corresponding to the directory / file under the folder for (String s: list) {System.out.println (s);} File [] files = file.listFiles () For (File f: files) {System.out.println (f.getName () + "," + f.getAbsolutePath ());}
Traversing the directory
1. Given a file object file
2.listFiles () gets an array of all the file objects under the file
3. Traverse the array of File objects, if it is a directory, call this method recursively to get all the file objects in that directory; if it is a file, print the output path + name
Import java.io.File;public class PrintFile {public static void main (String [] args) {File file = new File ("E:\\ Code"); getAllFile (file);} public static void getAllFile (File file) {/ / get an array of File objects in a given directory File [] files = file.listFiles () / / start traversing if (files! = null) {for (File f: files) {if (f.isDirectory ()) {/ / determine whether it is a directory, and if so, call recursive getAllFile (f) } else {/ / No, just print the path + file name System.out.println (f.getAbsoluteFile () + "under:" + f.getName ());} above is all the content of this article entitled "Java basic knowledge of Java O flow and File file operation method". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.