import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public static void readText throws FileNotFoundException {
Scanner scan = new Scanner(new File("samplefilename.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
//Here you can manipulate the string the way you want
}
}
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
br.close();
15
在Java 7中:
String folderPath = "C:/folderOfMyFile";
Path path = Paths.get(folderPath, "myFileName.csv"); //or any text file eg.: txt, bat, etc
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(path , charset)) {
while ((line = reader.readLine()) != null ) {
//separate all csv fields into string array
String[] lineVariables = line.split(",");
}
} catch (IOException e) {
System.err.println(e);
}
0
FileReader不允许您指定编码,如果需要指定,请使用 InputStreamReader :
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"));
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
// process the line.
}
// line is not visible here.
}
20 回答
您可以做的是使用扫描仪扫描整个文本并逐行浏览文本 . 当然你应该导入以下内容:
扫描仪基本上扫描所有文本 . while循环用于遍历整个文本 .
.hasNextLine()
函数是一个布尔值,如果文本中还有更多行,则返回true ..nextLine()
函数为您提供整行作为字符串,然后您可以按照自己的方式使用它 . 尝试System.out.println(line)
打印文本 .附注:.txt是文件类型文本 .
看看这个博客:
在Java 7中:
FileReader不允许您指定编码,如果需要指定,请使用
InputStreamReader
:如果从Windows导入此文件,则它可能具有ANSI编码(Cp1252),因此您必须指定编码 .
您可以使用Scanner类
您可以使用流来更精确地执行此操作:
一种常见的模式是使用
如果假设没有字符编码,则可以更快地读取数据 . 例如ASCII-7但它没有太大的区别 . 您对数据的处理很可能需要更长的时间 .
编辑:一种不太常见的模式,可以避免
line
泄漏的范围 .更新:在Java 8中你可以做到
注意:您必须将Stream放在try-with-resource块中以确保在其上调用#close方法,否则基础文件句柄永远不会关闭,直到GC稍后执行它 .
在Java 8中,您可以:
一些注意事项:
Files.lines
返回的流(与大多数流不同)需要关闭 . 原因mentioned here我避免使用forEach()
. 奇怪的代码(Iterable<String>) lines::iterator
将流转换为Iterable .实现这一目标的明确方法,
例如:
如果您当前目录中有
dataFile.txt
输出如下,
这个对我有用 . 希望它也会对你有所帮助 .
对于带有java 8的 Reading file
您可以使用此代码:
你也可以使用
apache commons io
:在Java 8中,还有一种使用Files.lines()的替代方法 . 如果您的输入源不是文件,而是更抽象的内容,如
Reader
或InputStream
,则可以通过BufferedReader
slines()
方法对行进行流式处理 .例如:
将为
BufferedReader
读取的每个输入行调用processLine()
.我通常直接阅读例行程序:
Java-9:
下面是一个完整的错误处理示例和支持Java 7之前的charset规范 . 使用Java 7,您可以使用try-with-resources语法,这样可以使代码更清晰 .
如果您只想要默认的字符集,可以跳过InputStream并使用FileReader .
这是Groovy版本,具有完整的错误处理:
java-8出来后(2014年3月),您将能够使用流:
打印文件中的所有行:
您需要在
class BufferedReader
中使用readLine()
方法 . 从该类创建一个新对象并在其上运行此方法并将其保存为字符串 .BufferReader Javadoc
我记录并测试了10 different ways to read a file in Java然后通过将它们读入1KB到1GB的测试文件来相互运行它们 . 以下是读取1GB测试文件的最快3种文件读取方法 .
请注意,在运行性能测试时,我没有向控制台输出任何内容,因为这会降低测试速度 . 我只想测试原始阅读速度 .
1)java.nio.file.Files.readAllBytes()
在Java 7,8,9中测试过 . 这是总体上最快的方法 . 读取1GB文件始终不到1秒 .
2)java.nio.file.Files.lines()
这在Java 8和9中已成功测试,但由于缺乏对lambda表达式的支持,因此无法在Java 7中运行 . 在一个1GB的文件中读取大约需要3.5秒才能将其放在第二位,直到读取更大的文件 .
3)BufferedReader
测试在Java 7,8,9中工作 . 在1GB测试文件中读取大约需要4.5秒 .
您可以找到所有10种文件读取方法here的完整排名 .