新手求教 Java 8 关于读文件的一个简单有关问题
新手求教 Java 8 关于读文件的一个简单问题
------解决思路----------------------
抛异常
------解决思路----------------------
package test;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
// 演示文件IO方法
public class Test {
public static void main(String[] args) throws IOException {
// 第一步:构建一个Scanner对象
// Paths.get这个方法提示编译出错,但我是照着书上的例子写的,请问哪里出问题了?
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
// 第二步:调用input函数族
while (in.hasNextLine()) {
String lineStr = in.nextLine();
// 读取行 System.out.print(lineStr);
}
// 关闭输入流
in.close();
}
}
就能通过了,少了 throws IOException
------解决思路----------------------
IDE有没有提示解决办法?
------解决思路----------------------
把你的代码包围在try。。。catch块中
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
这句有个IOException要处理
------解决思路----------------------
lz已经抛出异常了额...
感觉应该是没有这个文件的缘故吧,有后缀名的文件要加后缀名的
package test;
import java.nio.file.Paths;
import java.util.*;
// 演示文件IO方法
public class Test {
public static void main(String[] args) {
// 第一步:构建一个Scanner对象
// Paths.get这个方法提示编译出错,但我是照着书上的例子写的,请问哪里出问题了?
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
// 第二步:调用input函数族
while (in.hasNextLine()) {
String lineStr = in.nextLine(); // 读取行
System.out.print(lineStr);
}
// 关闭输入流
in.close();
}
}
------解决思路----------------------
抛异常
public static void main(String[] args) throws IOException {
// 第一步:构建一个Scanner对象
// Paths.get这个方法提示编译出错,但我是照着书上的例子写的,请问哪里出问题了?
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
// 第二步:调用input函数族
while (in.hasNextLine()) {
String lineStr = in.nextLine(); // 读取行
System.out.print(lineStr);
}
// 关闭输入流
in.close();
}
------解决思路----------------------
package test;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;
// 演示文件IO方法
public class Test {
public static void main(String[] args) throws IOException {
// 第一步:构建一个Scanner对象
// Paths.get这个方法提示编译出错,但我是照着书上的例子写的,请问哪里出问题了?
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
// 第二步:调用input函数族
while (in.hasNextLine()) {
String lineStr = in.nextLine();
// 读取行 System.out.print(lineStr);
}
// 关闭输入流
in.close();
}
}
就能通过了,少了 throws IOException
------解决思路----------------------
IDE有没有提示解决办法?
------解决思路----------------------
把你的代码包围在try。。。catch块中
Scanner in = new Scanner(Paths.get("/home/fangmeng/testFile"));
这句有个IOException要处理
------解决思路----------------------
lz已经抛出异常了额...
感觉应该是没有这个文件的缘故吧,有后缀名的文件要加后缀名的