在Java中,似乎有不同的方式来读取和写入文件数据。
我想从一个文件读取ASCII数据。可能的方式和它们的区别是什么?
在Java中,似乎有不同的方式来读取和写入文件数据。
我想从一个文件读取ASCII数据。可能的方式和它们的区别是什么?
当前回答
这可能不是问题的确切答案。这只是读取文件的另一种方式,其中不需要在Java代码中显式指定文件的路径,而是将其作为命令行参数读取。
使用以下代码,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class InputReader{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s="";
while((s=br.readLine())!=null){
System.out.println(s);
}
}
}
只需继续运行它,使用:
java InputReader < input.txt
这将读取input.txt的内容并将其打印到控制台。
你也可以让System.out.println()通过命令行写入一个特定的文件,如下所示:
java InputReader < input.txt > output.txt
这将从input.txt读取并写入output.txt。
其他回答
使用BufferedReader:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("/fileToRead.txt"));
try {
String x;
while ( (x = br.readLine()) != null ) {
// Printing out each line in the file
System.out.println(x);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
ASCII是一个文本文件,因此您可以使用reader进行读取。Java还支持使用InputStreams从二进制文件中读取。如果要读取的文件很大,那么您可能希望在FileReader之上使用BufferedReader来提高读取性能。
阅读这篇关于如何使用Reader的文章
我还推荐你下载并阅读这本很棒(但免费)的书,叫做《用Java思考》
在Java 7中:
new String(Files.readAllBytes(...))
(文档) 或
Files.readAllLines(...)
(文档)
在Java 8中:
Files.lines(..).forEach(...)
(文档)
在实践中,缓冲流类的性能要高得多,以至于NIO.2 API包含了专门返回这些流类的方法,部分原因是为了鼓励您始终在应用程序中使用缓冲流。
这里有一个例子:
Path path = Paths.get("/myfolder/myfile.ext");
try (BufferedReader reader = Files.newBufferedReader(path)) {
// Read from the stream
String currentLine = null;
while ((currentLine = reader.readLine()) != null)
//do your code here
} catch (IOException e) {
// Handle file I/O exception...
}
您可以替换此代码
BufferedReader reader = Files.newBufferedReader(path);
与
BufferedReader br = new BufferedReader(new FileReader("/myfolder/myfile.ext"));
我推荐这篇文章来学习Java NIO和IO的主要用途。
最简单的方法是使用Java中的Scanner类和FileReader对象。简单的例子:
Scanner in = new Scanner(new FileReader("filename.txt"));
扫描器有几个方法读取字符串,数字,等…您可以在Java文档页面上查找有关这方面的更多信息。
例如,将整个内容读入String:
StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
sb.append(in.next());
}
in.close();
outString = sb.toString();
另外,如果你需要一个特定的编码,你可以使用这个来代替FileReader:
new InputStreamReader(new FileInputStream(fileUtf8), StandardCharsets.UTF_8)
对于基于jsf的Maven web应用程序,只需使用ClassLoader和Resources文件夹读取任何你想要的文件:
Put any file you want to read in the Resources folder. Put the Apache Commons IO dependency into your POM: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> Use the code below to read it (e.g. below is reading in a .json file): String metadata = null; FileInputStream inputStream; try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); inputStream = (FileInputStream) loader .getResourceAsStream("/metadata.json"); metadata = IOUtils.toString(inputStream); inputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return metadata;
您可以对文本文件、.properties文件、XSD模式等执行相同的操作。