import java.io.*;
import java.util.*;
public class test2
{
public static void main(String[] args)
{
Scanner input=null;
try
{
InputStream in=new FileInputStream(getClass().getResource("test.java").getFile());
input=new Scanner(in);
}
catch (FileNotFoundException e)
{}
while(input.hasNextLine())
{
System.out.println(input.nextLine());
}
}
}
test.java文件的路径是否正确
InputStream in=getClass().getResourceAsStream("test.java");
public class Test {
public static void main(String[] args) {
Scanner input = null;
File file = new File("D:\\wangzhidong\\workspace\\test\\src\\com\\test\\demo\\Test.java");
try {
InputStream in = new FileInputStream(file);
input = new Scanner(in);
} catch (FileNotFoundException e) {
}
while (input.hasNextLine()) {
System.out.println(input.nextLine());
}
}
}
最好是绝对路径
文件路径要给对