Java 访问外部文件
我正在使用nio2来读取使用eclipse的桌面中的外部文件。我得到了以下代码的异常 “Java 访问外部文件,java,eclipse,Java,Eclipse,我正在使用nio2来读取使用eclipse的桌面中的外部文件。我得到了以下代码的异常 “java.nio.file.NoSuchFileException:C:\Users\User\Desktop\JEE\FirstFolder\first.txt” 请告知如何解决?还尝试使用命令提示符。得到相同的异常 public class ReadingExternalFile { public static void main(String[] args) { Path p
java.nio.file.NoSuchFileException
:C:\Users\User\Desktop\JEE\FirstFolder\first.txt
”
请告知如何解决?还尝试使用命令提示符。得到相同的异常
public class ReadingExternalFile {
public static void main(String[] args) {
Path p1= Paths.get("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt");
System.out.println(p1.toString());
System.out.println(p1.getRoot());
try(InputStream in = Files.newInputStream(p1);
BufferedReader reader = new BufferedReader(new InputStreamReader(in)))
{
System.out.println("Inside try");
String line=null;
while((line=reader.readLine())!=null){
if (!line.equals("")) {
System.out.println(line);
}
//System.out.println(line);
}
} catch (IOException e) {
System.out.println( e);
}
}
}
我不明白为什么要使用Path对象,你可以简单地使用file对象创建文件,并使用字符串作为路径,然后将其包装在文件读取器对象中,然后将其包装在缓冲读取器中,结尾应该如下所示:
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt");
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
System.out.println(bfr.readLine());
bfr.close();
} catch (IOException e){
e.printStackTrace();
}
}
不要忘记在读写之后关闭流,也要使用可读的名称(不要像我做的那样,使用有意义的名称!)我不明白为什么要使用路径对象,您可以使用file对象创建文件,只需使用字符串作为路径,然后将其包装在文件读取器对象中,然后将其包装在缓冲读取器中,结尾应如下所示:
public static void main(String[] args) {
try {
File file = new File("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt");
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
System.out.println(bfr.readLine());
bfr.close();
} catch (IOException e){
e.printStackTrace();
}
}
不要忘记在读写之后关闭流,也要使用可读的名称(不要像我做的那样,使用有意义的名称!)尝试下面的代码,希望这对您有所帮助
Path p1= Paths.get("C:\\Users\\user\\Desktop\\FirstFolder\\first.txt");
try(
BufferedReader reader = Files.newBufferedReader(p1, Charset.defaultCharset()))
{
System.out.println("Inside try");
String line=null;
while((line=reader.readLine())!=null){
if (!line.equals("")) {
System.out.println(line);
}
//System.out.println(line);
}
} catch (IOException e) {
System.out.println( e);
}
尝试下面的代码,希望这将帮助你
Path p1= Paths.get("C:\\Users\\user\\Desktop\\FirstFolder\\first.txt");
try(
BufferedReader reader = Files.newBufferedReader(p1, Charset.defaultCharset()))
{
System.out.println("Inside try");
String line=null;
while((line=reader.readLine())!=null){
if (!line.equals("")) {
System.out.println(line);
}
//System.out.println(line);
}
} catch (IOException e) {
System.out.println( e);
}
试试这个
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
试试这个
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\FirstFolder\\first.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
}谢谢。我很熟悉这种方法。你能告诉我如何使用Paths类阅读吗???我正在尝试学习nio2,但在尝试时遇到了这个错误。你在这里使用了哪个版本的java??java 8??不,我正在使用java 7。您是否放置了正确的路径“C:\\Users\\User\\Desktop\\FirstFolder\\first.txt”??我看到您的代码很好,运行良好。在运行之前,请清理并生成项目。已尝试清理项目。但仍然收到相同的错误。使用命令提示符运行程序也会出现同样的错误。您是否能够运行代码并获得输出??。谢谢。谢谢。我对这种方式很熟悉。你能告诉我如何使用Paths类阅读吗???我正在尝试学习nio2,但在尝试时遇到了这个错误。你在这里使用了哪个版本的java??java 8??不,我正在使用java 7。您是否放置了正确的路径“C:\\Users\\User\\Desktop\\FirstFolder\\first.txt”??我看到您的代码很好,运行良好。在运行之前,请清理并生成项目。已尝试清理项目。但仍然收到相同的错误。使用命令提示符运行程序也会出现同样的错误。您是否能够运行代码并获得输出??。谢谢。嗨,谢谢你提供的信息。我知道这样,我试图学习nio2,但在使用“路径”类时出现了错误。你能建议在这种情况下如何使用nio2读取文件吗?如果你能添加控制台输出,那就太好了,包括问题底部的错误,并在问题中明确表示希望使用nio2软件包。您好,感谢您提供的信息。我知道这种方式,我试图学习nio2,但在使用“路径”时出现了错误在这种情况下,你能建议如何使用nio2来读取文件吗?如果你能添加控制台输出,包括问题底部的错误,并在问题中明确表示你想使用nio2包,那就太好了。嗨,谢谢你提供的信息。我知道这种方法,我试图学习nio2,但在使用“路径”类时出错。您能建议在这种情况下如何使用nio2读取文件吗?您好,谢谢您提供的信息。我知道这种方式,我试图学习nio2,但在使用“路径”类时出错。您能建议在这种情况下如何使用nio2读取文件吗?