--------
   问题:Scanner sc = new Scanner("info.txt")与Scanner sc = new Scanner(new FileReader("info.txt"))有什么区别?-------
请看代码:import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;public class TextFileTest {
public static void main(String[] args) {
Employee[] staff = new Employee[3];
staff[0] = new Employee("zxw",36000.2,1991,11,23);
staff[1] = new Employee("zxw",72000.4,1991,11,23);
staff[2] = new Employee("zxw",144000.8,1991,11,23);
try{
PrintWriter out = new PrintWriter("zxwInfo.txt");
writeData(staff,out);
out.close();
System.out.println("----------------out over-------------");
Scanner in = new Scanner(new FileReader("zxwInfo.txt"));
Employee[] newStaff = readData(in);
in.close();
System.out.println("----------------in over-------------");
for (Employee employee : newStaff) {
System.out.println(employee);
}
}
catch(IOException err){err.printStackTrace();}
} private static Employee[] readData(Scanner in) {
int n = in.nextInt() ;
in.nextLine() ;
Employee[] employees = new Employee[n];
for (int i = 0; i < n ; i++) {
employees[i] = new Employee();
employees[i].readData(in);
}
return employees;
} private static void writeData(Employee[] staff, PrintWriter out) {
out.println(staff.length);
for (Employee e : staff) {
e.writeData(out);
}
}
}
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;class Employee{
private String name;
private double salary;
private Date hireDay;
public Employee(){}
public Employee(String n , double s , int year , int month , int day){
name = n ; 
salary = s ;
GregorianCalendar calendar = new GregorianCalendar(year , month-1 ,day);
hireDay = calendar.getTime() ;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public Date getHireDay() {
return hireDay;
}
public void raiseSalary(double byPercent){
double raise = salary * byPercent / 100 ;
salary += raise ;
}
public String toString(){
return getClass().getName()+
"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
}
public void writeData(PrintWriter out){
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(hireDay);
out.println(name + "|" + salary + "|" + calendar.get(Calendar.YEAR) + "|" 
+calendar.get(Calendar.MONTH+1) + "|" + calendar.get(Calendar.DAY_OF_MONTH));
}
public void readData(Scanner in){
String line = in.nextLine() ;
String[] tokens = line.split("\\|");
name = tokens[0];
salary = Double.parseDouble(tokens[1]);
int y = Integer.parseInt(tokens[2]);
int m = Integer.parseInt(tokens[3]);
int d = Integer.parseInt(tokens[4]);
GregorianCalendar calendar = new GregorianCalendar(y, m-1, d);
hireDay = calendar.getTime() ;
}
}
这是个正确的程序,但是,
将TextFileTest.java中main函数中的
Scanner in = new Scanner(new FileReader("zxwInfo.txt"));
改成
Scanner in = new Scanner("zxwInfo.txt");
,就报错了,请问,这是为什么?

解决方案 »

  1.   

    因为Scanner没有这个构造函数
    Scanner(String s);
      

  2.   

    看看API吧。
     Constructor Summary
    Scanner(File source)
              Constructs a new Scanner that produces values scanned from the specified file.
    Scanner(File source, String charsetName)
              Constructs a new Scanner that produces values scanned from the specified file.
    Scanner(InputStream source)
              Constructs a new Scanner that produces values scanned from the specified input stream.
    Scanner(InputStream source, String charsetName)
              Constructs a new Scanner that produces values scanned from the specified input stream.
    Scanner(Readable source)
              Constructs a new Scanner that produces values scanned from the specified source.
    Scanner(ReadableByteChannel source)
              Constructs a new Scanner that produces values scanned from the specified channel.
    Scanner(ReadableByteChannel source, String charsetName)
              Constructs a new Scanner that produces values scanned from the specified channel.
    Scanner(String source)
              Constructs a new Scanner that produces values scanned from the specified string.
      
      

  3.   

    1楼兄弟,明明就有那个API,你看:Scanner
    public Scanner(String source)构造一个新的 Scanner,它生成的值是从指定字符串扫描的。 参数:
    source - 要扫描的字符串
    --------------------------------------------------------
    另外,重复下我的问题:
    --------
    问题:
    Scanner sc = new Scanner("info.txt")

    Scanner sc = new Scanner(new FileReader("info.txt"))
    有什么区别?
    为什么我的程序中将TextFileTest.java中main函数中的
    Scanner in = new Scanner(new FileReader("zxwInfo.txt"));
    改成
    Scanner in = new Scanner("zxwInfo.txt");
    ,就报错了,请问,这是为什么?-------
      

  4.   

    Scanner(InputStream source) 
              构造一个新的 Scanner,它生成的值是从指定的输入流扫描的。
    Scanner(File source) 
              构造一个新的 Scanner,它生成的值是从指定文件扫描的。Scanner(String source) 
              构造一个新的 Scanner,它生成的值是从指定字符串扫描的。
    Scanner(String source)这里没有提到文件也没有说source是指文件路径
      

  5.   

    Sorry,搞错了。这个是从字符串扫描,不是读取文件。
      

  6.   

    工欲善其事 必先利其器!
    多看看api!有利无害。
    不是有异常吗!打印输出看看