[英]What's the difference between a Resource, URI, URL, Path and File in Java?
[英]What's the difference between File and FileLoader in Java?
所以我有以下代码,我应该在其中读取一个文本文件(这只是主类):
import gui.MenuWindow;
import java.io.IOException;
import javax.swing.JOptionPane;
public class Assessor {
public static void main(String args[]) throws IOException {
FileLoader file = new FileLoader("Example.txt");
try{
new MenuWindow(file.loader());
} catch(Exception exc) {
JOptionPane.showMessageDialog(null, "Error Reading File");
}
}
}
然后我必须使用 Swing 将文本加载到列表框中。 问题是我发现了这个新代码来读取文本文件:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadTextFileExample {
public static void main(String[] args) {
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null) {
contents.append(text)
.append(System.getProperty(
"line.separator"));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// show file contents here
System.out.println(contents.toString());
}
}
所以我想知道以下两行之间有什么区别:
FileLoader file = new FileLoader("Example.txt"); //First Code
File file = new File("test.txt"); //Second Code
还有... StringBuffer
和BufferedReader
是用来做什么的? 谢谢!
所以我想知道以下两行之间有什么区别:
FileLoader file = new FileLoader("Example.txt"); //First Code File file = new File("test.txt"); //Second Code
第一个创建了 Andreas 讨论的java.io.FileLoader
。 由于javadoc说“此 class 的构造函数假定默认字符编码和默认字节缓冲区大小是适当的”,因此永远不应使用它。
第二个创建一个java.io.File
,它只是一个文件路径,带有一些实用方法,可用于读取目录树,删除,创建和移动文件等,也可以与FileInputStream
和其他类一起使用实际访问文件内容。
还有... StringBuffer 和 BufferedReader 是用来做什么的? 谢谢!
StringBuffer
用于收集文件的内容。
BufferedReader
用于加速文件的读取。 BufferedReader
不是一次读取一个字符,而是使用内部缓冲区批量读取。
这是一个关于学习 Java SE 的示例性问题,尤其是关于java.io ZEFE90A8E604A7C60B4 I was a bit puzzled in the beginning, but now I am quite sure that you want to compare the FileReader
to the File
class, which both belong to the same package java.io .
Java SE API中的File
:
“文件和目录路径名的抽象表示。”
换句话说,它用于处理 Java 中文件系统上的文件和目录。 由于 Java 是一种面向对象的语言,他们为它制作了 class。 文件,即二进制文件和文本文件,与目录有一些共同的属性,如:绝对、规范路径和简单名称等。
Of course, File is one of the base classes in the java.io package and many classes like FileReader
make use of it for object construction.
FileReader
:
“方便 class 读取字符文件。”
它带有一个方便的构造函数,它将文件名或文件路径作为String
。 最初,它是由 File 实例构造的。 Reader
实例通常用于读取文本文件,而InputStream
用于读取二进制文件。 Reader 实例通常连接到字符集,例如"UTF-8"
以将字节转换为字符流。
另请查看 Oracle 提供的优秀Java 教程。
我希望File
和FileReader
之间的区别变得更加清晰。 特别注意,当您实例化File
实例时,没有 I/O。 要回答您的问题,这两个类的互连将是:
File file = new File("test.txt"); // 1) Instaniate the file
Reader reader = new FileReader(file); // 2) Instantiate the Reader using the File instance
当您将BufferedReader
包裹在Reader
实例周围时,您可以逐行读取文本文件,如下所示:
BufferedReader bufferedReader = new BufferedReader(reader); // 3) Get a "buffered reader" to have access line by line.
当您想要链接大量String
对象时, StringBuffer
就会出现,因为String
对象是不可变的,而字符串操作如
String s1 = "Star ";
String s2 = "Wars";
s1 = s1 + s2;
非常昂贵,尤其是在循环中,因为每次添加时都会创建一个新的 String object(左侧结果),除了保留的 Java VM 堆空间外,几乎没有大小限制。
让我指出,您最好使用StringBuilder
class,它更快,是StringBuffer
的非同步对应部分,在 Java 5 版本中引入。 StringBuffer
保证在不同线程之间同步的特性几乎从未使用过。 作为 Java 程序员,我这辈子从来没有遇到过它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.