I have the following task that have to be implemented in Java.
I have a String that represent the path of a txt file, something like this:
String fileFatturePa = "C:\\Users\\Andrea\\Desktop\\D150316.T1642\\myFile.txt";
This textual file contains some text (that represent XML but this is not important because I do not have to operate on it with something like XPath but I have to do simple substring operation).
So I need to read this textual file (starting from its path) and then I have to do some textual operation.
This file could be very big.
What is the best way to read it stargint from its path?
一个好的起点是 Oracle 的官方 Java 文档: Basic I/O 。
BufferedReader reader = new BufferedReader(new FileReader(fileFatturePa));
try
{
String line = null;
while ((line = reader.readLine()) != null)
{
***operations(specific to each line of the text file) ***
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
reader.close();
}
Simple, you create a new File: File f = new File(path)
. For raed all the lines, you can use the class Fileutils from Apache Commons IO. And you will have something like: List<String> allLines=Fileutils.readlines(f)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.