虽然试图做一些文件操作的代码是这样的,文件处理Java
File file=new File("aaa.txt");
我在程序BufferedReader
看到和InputStreamReader
也包括在内,你可以用一个简单的例子来解释?我读过很多关于文件处理的网站,但仍然令人困惑!
虽然试图做一些文件操作的代码是这样的,文件处理Java
File file=new File("aaa.txt");
我在程序BufferedReader
看到和InputStreamReader
也包括在内,你可以用一个简单的例子来解释?我读过很多关于文件处理的网站,但仍然令人困惑!
File
类本质上是一个文件描述符,它允许您获取文件的句柄,但本身并不具有从文件读取信息的方法。
这就是在InputStreamReader
进来。一个InputStreamReader
(和更容易的子类FileReader
)会为你做阅读(还有其他的方法来做到这一点,但是这是最简单的一个)。
BufferedReader
将包装InputStreamReader
并将读取文件到缓冲区(而不是简单地转换并在每次读取调用后返回字节),让您更容易读取数据。
看看这个基本tutorial
基本上BufferedReader
是读取文件信息。有多种方法可以做到这一点,这取决于您的需求。
public void printFileContentsToConsole(final String aFileName) {
//Make a new file.
File file = new File(aFileName);
//Declare the reader outside the scope so we can use it
//in the finally block.
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String line;
//Read one line at a time, printing it.
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//Try to close it, this might throw an exception anyway.
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
显然,有看BufferedReader和FileReader的API会回答你很多关于这些特殊类的问题。
澄清为什么你想要使用BufferedReader,重点是在一个文件中逐行读取。
这里是我从磁盘读取一个文件:
请注意,所有的代码必须放在一个try/catch语句中,在FileNotFoundException异常的情况下,例如。
try
{
//You first have to create a reader that will be used to access the file.
//You create the BufferedReader from a FileReader, and you only need to specify
//the address of the file on your disk.
BufferedReader br = new BufferedReader(new FileReader("fileURL"));
String s;
//The BufferedReader has a method "readLine" that reads the file line by line
//When the reader reaches the end of the file, the method readLine returns null,
//so you can control if there is some data remaining or not.
while((s = br.readLine()) != null)
{
System.out.println(s);
}
//Don't forget to close the reader when the process is over.
br.close();
}
catch(Exception e)
{
// Do some stuff
}
正如我所记得的,类BufferedReader和FileReader可以在java.io中找到;
在我看来,这是用Java读取文件的最简单方法。如果你需要知道如何写文件,这个过程几乎是一样的,所以我也可以给你一些建议。
希望这会有所帮助。
基本上java.io.File类代表在文件系统中的文件,但不包含方法来操纵它们(除创建或删除它们)
当你与他们合作,你使用java.io包中的其他类,BufferefReader和InputStreamReader就在其中,但其他类似FileInputStream。
根据操作要执行,你可以使用阅读器或流(类与“读者”或结束“作家”是对文本内容,以“流”结尾的类为二进制内容,但当然,你可以随时读/写文本为二进制)。
大多数时候你必须“链接”几个这些类来执行工作。同样需要注意的是,大多数情况下,您可以使用非常类似的代码写入套接字。
一个简单的例子可能是这样的:
package demo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
public class ReadLines {
public static void main(String [] args) throws IOException {
File toRead = new File(args[0]);
if(toRead.exists()) {
List<String> lines = readLines(toRead);
} else {
System.out.println(toRead.getName() + " doesn't exists");
}
}
private static List<String> readLines(File fromFile) throws IOException {
BufferedReader reader = null;
List<String> lines = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader(fromFile)); // chaining
String line = null;
while((line = reader.readLine()) != null) {
lines.add(line);
}
} finally {
// always close the file
if(reader != null) try {
reader.close();
} catch(IOException ioe) {
// it's ok to ignore an exception while closing a file
}
}
return lines;
}
}
我希望这个代码,使其更清晰,为您和编译(
:PI不手头
有一个编译器)
参见:
http://download.oracle.com/javase/tutorial/essential/io/ http://download.oracle.com/javase/6/docs/api/java/io/package-summary.html http://download.oracle.com/javase/6/docs/api/java/io/Reader.html http://download.oracle.com/javase/6/docs/api/java/io/Writer.html http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html http://download.oracle.com/javase/6/docs/api/java/io/OutputStream.html
为了清楚起见,从InputStreamReader.java
的InputStreamReader的javadoc的截取是从 字节的桥接流以字符流:这 读取字节并将其解码为使用 字符指定的字符集。 每次调用InputStreamReader的read()方法中的一个 可能会导致从底层字节输入流中读取一个或多个字节 。 要启用的 字节字符有效转化,更字节可以 从底层流 超前读比是必需的,以满足当前 读取操作。
对于顶部效率,考虑包装 的InputStreamReader一个 的BufferedReader内。例如:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
很好的例子,但在finally块中有一个潜在的NPE(如果在新的BufferedReader ...行中发生错误,读者可能为空)。 – 2010-10-29 13:10:06