matters needing attention
If the file cannot be found when running the code, but the file does exist again, check the working path of idea
route
Path
The Path object encapsulates a Path into an object, and then performs some Path operations through this object. All methods are as follows. Specific methods and functions can also be guessed through the name. If you don't understand, check the API documents. All the following methods can enter the corresponding class through idea and click alt + 7 to view
Paths
Get the tool class of the Path object. There are two overloaded methods in total
- Path get(String first, String... more) allows you to receive one or more strings to construct a path object
- Path get (uri) allows you to receive a uri object to construct a path object
import java.nio.file.Path; import java.nio.file.Paths; class PathDemo { public static void demo() { // Overloaded method 1, which allows one or more parameters to be accepted Path path1 = Paths.get("Main.java"); System.out.println(path1.toAbsolutePath()); Path path2 = Paths.get("E","1.base","workspace","slime"); System.out.println(path2.toAbsolutePath()); // Overload method 2, which allows you to accept a URI object Path urlPath = Paths.get(path1.toUri()); System.out.println(urlPath.toAbsolutePath()); } } public class Main { public static void main(String[] args) { PathDemo.demo(); } }
Output:
E:\1.base\workspace\slime\file_io\src\main\java\cyrus\file_io\file\Main.java E:\1.base\workspace\slime\file_io\src\main\java\cyrus\file_io\file\E\1.base\workspace\slime E:\1.base\workspace\slime\file_io\src\main\java\cyrus\file_io\file\Main.java
file
File
File is to construct a file into a file object, and then operate this object to read, add, update and delete the file. The specific API is as follows. You can guess seven or eight points according to the name. For specific API usage, you can view the API document.
Files object
The Files object can manipulate the Files under the object Path or this Path. The Path here is wrapped as a Path object.
File lookup
Use the FileSystems class to find files. Examples are as follows:
In matcher, * * / at the beginning of glob expression means "current directory and all subdirectories", which is very useful when you not only want to match the Path at the specific end of the current directory. A single * means "anything", then a dot, and then braces indicate a series of possibilities - we're looking for something that ends in. tmp or. txt. You can find more details in the getPathMatcher() document.
class Find { public static void demo() { Path test = Paths.get("test"); PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**/*.{temp,txt}"); try { Files.walk(test).filter(matcher::matches).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { Find.demo(); } }
Output:
test\123.temp test\dsaioh.txt test\test\345.temp test\test\qwe.txt
File reading and writing
read
Use the Files tool class to operate. The following example uses the readalllines (path) method to read and print the entire Main.java file to the control line.
public class Main { public static void main(String[] args) throws IOException { List<String> list = Files.readAllLines(Paths.get("Main.java")); list.stream().forEach(System.out::println); } }
Output:
package cyrus.file_io.file; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class Main { public static void main(String[] args) throws IOException { List<String> list = Files.readAllLines(Paths.get("Main.java")); list.stream().forEach(System.out::println); } }
write in
Here, first create a file 1.txt, and then write the document through the Path write() method. This method requires a Path and a byte array to be written
public class Main { public static void main(String[] args) throws IOException { File file = new File("test" + File.separator + "test" + File.separator + "1.txt"); if (!file.exists()) { file.createNewFile(); } String str = "input something"; Files.write(file.toPath(), str.getBytes()); } }
Output:
What if the file is too large
read
Read the file line by line through Files.lines(), and each line is a Stream stream.
public class Main { public static void main(String[] args) throws IOException { Stream<String> lines = Files.lines(Paths.get("Main.java")); lines.forEach(System.out::println); } }
Output:
package cyrus.file_io.file; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.stream.Stream; public class Main { public static void main(String[] args) throws IOException { Stream<String> lines = Files.lines(Paths.get("Main.java")); lines.forEach(System.out::println); } }
write in
First, read the file line by line through Files.lines(), and then write it line by line
public class Main { public static void main(String[] args) throws IOException { String name = "test" + File.separator + "test" + File.separator + "Main.txt"; PrintWriter printWriter = new PrintWriter(name); Files.lines(Paths.get("Main.java")).forEach(printWriter::println); printWriter.close(); } }
Output:
This article provides some personal opinions in my learning process. Loopholes are essential. I hope you can give me more advice and help repair loopholes!!!
It's better to enter my language bird document experience
https://www.yuque.com/docs/share/e91bfe3d-1d76-4ef2-99a5-543d79e20c38?# Document
Reference: java programming ideas
You may also want to read the following articles:
Easy to understand and speak IO