建 立 三 个 文 件 分 别 为 INT DOUBLE STRING
输 入 数 据 
输 出 数 据 .看 似 简 单 ,但 出 不 来 结 果 .
import java.io.FileNotFoundException;
 import java.lang.SecurityException;
 import java.util.Formatter;
 import java.util.FormatterClosedException;
 import java.util.NoSuchElementException;*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;public class CreateFile {
String fn1;
String fn2;
String fn3; Formatter outputInt;
Formatter outputDou;
Formatter outputStr;
Scanner input;
Scanner input1;
Scanner input2;
Scanner input3;

public CreateFile(){

} public void validateFileName() {
while (outputInt == null) {
input = new Scanner(System.in); System.out.println("Please enter a file name for Intergers");
if (input.hasNext())
try {
fn1 = input.next()+".txt";
outputInt = new Formatter("fn1");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("please enter a valid filename");
}
} while (outputDou == null) {
input = new Scanner(System.in); System.out.println("Please enter a file name for Doubles");
if (input.hasNext())
try {
fn2 = input.nextLine()+".txt";
outputDou = new Formatter("fn2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("please enter a valid filename");
}
} while (outputStr == null) {
input = new Scanner(System.in); System.out.println("Please enter a file name for Strings");
if (input.hasNext())
try {
fn3 = input.nextLine()+".txt";
outputStr = new Formatter("fn3");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print("please enter a valid filename");
}
} } // add records to file
public void addRecords() {
input = new Scanner(System.in);
System.out.println("please enter data for the three filename");
System.out.printf("To terninate input, type the end-of-file indicator,/n On UNIX/LINUX/Mac OX X,type <ctrl>d then press enter,/n"
+ "On Windows type <ctrl>z then press enter"); while (input.hasNext())// loop until end-of-file indicator
{
if (input.hasNextInt()) try // output values to file
{
outputInt.format(input.next()); }
// end try
catch (FormatterClosedException formatterClosedException) {
System.err.println("Error writing to file.");
return;
} // end catch
catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine(); // discard input so user can try again
} // end catch
else if (input.hasNextDouble())
try // output values to file
{
outputDou.format(input.next()); }
// end try
catch (FormatterClosedException formatterClosedException) {
System.err.println("Error writing to file.");
return;
} // end catch
catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine(); // discard input so user can try again
} // end catch else
try // output values to file
{
outputStr.format(input.next()); }
// end try
catch (FormatterClosedException formatterClosedException) {
System.err.println("Error writing to file.");
return;
} // end catch
catch (NoSuchElementException elementException) {
System.err.println("Invalid input. Please try again.");
input.nextLine(); // discard input so user can try again
} // end catch } // end while } // end method add public void closeFile() {
if (outputInt != null && outputDou != null && outputStr != null)
outputInt.close();
    outputDou.close();
outputStr.close();
} // end method closeFile
/*public void openfile() {
try {
input = new Scanner(new File(fn1));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch try {
input = new Scanner(new File(fn2));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch
try {
input = new Scanner(new File(fn3));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch
}*/ public void read() {
//open three files
try {
input1 = new Scanner(new File("fn1"));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch try {
input2 = new Scanner(new File("fn2"));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch
try {
input3 = new Scanner(new File("fn3"));
} catch (SecurityException securityException) {
System.err.println("You do not have write access to this file.");
System.exit(1); // terminate the program
} // end catch
catch (FileNotFoundException fileNotFoundException) {
System.err.println("Error opening or creating file.");
System.exit(1); // terminate the program
} // end catch
         
//create three formatter's objects. 
//read records from files using Scanner objects. 
//print the token of each file using Formatter objects.
 try {
outputInt = new Formatter("fn1");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 try {
outputDou = new Formatter("fn2");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 try {
outputStr = new Formatter("fn3");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} outputInt.format("%25.24s%n,%25.24s%n,%25.24s%n", "Intergers",
"Double", "String"); while (input1.hasNext() || input2.hasNext() || input3.hasNext()) {
outputInt.format("%25.24s%n", input1.next());
outputDou.format("%25.24s%n", input2.next());
outputStr.format("%25.24s%n", input3.next());
System.out.println();
}
}}
public class Lab5Test {

  
  public static void main (String[] args) 
  {
  CreateFile application = new CreateFile();
 
  application.validateFileName();
  application.addRecords();
 application.closeFile();
 application.read();
 application.closeFile();
 
 } // end main
} // end class ReadTextFileTest
 

解决方案 »

  1.   

    input = new Scanner(System.in);
    while (input.hasNext())// loop until end-of-file indicator
    {
    if (input.hasNextInt()) try // output values to file
    {
    outputInt.format(input.next());
    这里会有死循环,input这里要等待系统输入,所以要阻塞。但是一旦输入的话while就死循环了。
      

  2.   

    看看楼主的 程序:
    outputInt = new Formatter("fn1"); (包括后面的相似的2条语句,)看楼主的意思应该去掉双引号.
    能够录入数据,不同类型放到不同的文件里. 但后边 //create three formatter's objects.  
    //read records from files using Scanner objects.  
    //print the token of each file using Formatter objects.
    不明白要做什么了.
      

  3.   

    我 正 在  国 外 学  计 算 机 ,学 二 年 ,以 前 零 基 础 .这 是 JAVA课 的 一 个 作 业 .多 谢 以 上 两 位 意 见 ,我 还 是 搞 不 出 , 问 老 师 去 了 .