2014-07-25 141 views
1

对于此代码,这是一个java文本格式化程序。我运行它,然后它会提示列大小,但是当它提示输入文件时,我输入它并说它不存在?为什么在提示时无法打开输入文件?

我做错了什么。

我试着做文件位置,然后文件名本身。 我应该尝试重新保存到同一工作位置吗? 请帮忙。

这是我的代码,如果有更多的内部问题,我忽略了。我认为它一直在努力。

import java.io.*; 
import java.util.Scanner; 

/** 
* Formatter - simple text formatting 
*/ 
public class Formatter 
{ 
public static void exit(Scanner sc) 
{ 
    // Keep console window alive until 'enter' pressed (if needed). 
    System.out.println(); 
    System.out.println("Done - press enter key to end program"); 
    String junk = sc.nextLine(); 
    System.exit(0); 
} 

/** 
* main - text formatting 
*/ 
public static void main (String[] args) 
{ 
    Scanner sc = new Scanner(System.in); 

    // Get maximum output column width for formatting 
    int maxWidth; 
    System.out.println("Enter the output column width"); 
    do { 
     maxWidth = sc.nextInt(); 
     sc.nextLine(); 
     if (maxWidth < 30 || maxWidth > 100) { 
      System.out.println("Column size must be between 30 and 100...re-enter"); 
      maxWidth = 0; 
     } 
    } while (0 == maxWidth); 


    // Get name of input text file to read and format, check it exists, and readable. 



    File textinFile = null; 
    Scanner scan = null; 
    String read = null; 
    String read1 = ""; 
    String textinName; 

    do { 
     System.out.println("Enter the name of the input text file"); 

     textinName = sc.nextLine(); 

     textinFile = new File(textinName); 
     if (!textinFile.exists()) { 
      System.out.println("File does not exist: " + textinName + " - re-enter"); 
      textinName = null; 
      continue; 
     } 
     if (!textinFile.canRead()) { 
      System.out.println("Unable to read from file: " + textinName + " - re-enter"); 
      textinName = null; 
      continue; 
     } 


     try { 
      scan = new Scanner(textinFile); 
      scan = new Scanner(new File(textinName)); 
      while(scan.hasNextLine()){ 
      read = scan.nextLine() + "\n"; 
      read1 += read; 

      } 
      scan.close(); 
     } catch (FileNotFoundException e) { 
      // Unexpected, as already checked for file existing 
      System.out.println("Unexpected error: " + e.toString()); 
      //textinScanner = new Scanner(textinFile); 


      continue; 
     } 

    } while (!textinFile.exists()); 


    // Get name of the output file to write formatted text to, and open a file. 

    String textoutName = null; 
    do { 
     System.out.println("Enter the name of the output file"); 
     textoutName = sc.nextLine(); 

     File textoutFile = new File(textoutName); 
     if (textoutFile.exists()) { 
      System.out.println("File already exists: " + textoutName); 
      System.out.println("Do you want to overwrite this file (Y/N)"); 
      String yesno = sc.nextLine(); 
      if (yesno.toLowerCase().startsWith("n")) { 
       System.out.println("Re-enter output file name"); 
       textoutName = null; 
       continue; 
      } 
     } 
    } while (null == textoutName); 

    // Open the output file for writing (or the console). 
    PrintWriter textoutWriter = null; 
    Scanner outputReader = null; 
    String newReader = null; 
    String newReader1 = ""; 
    try { 
      //outputReader = new Scanner(new File(textinName)); 
      //textoutWriter = new PrintWriter(new BufferedWriter(new FileWriter(textoutName))); 

     scan = new Scanner(textinFile); 
     scan = new Scanner(new File(textinName)); 
     while(scan.hasNext()){ 
      newReader = scan.next() + "\n"; 
      newReader1 += newReader; 

      } 
      scan.close(); 
    } catch (IOException e) { 
     System.out.println(e.toString()); 
     exit(sc); 
    } 


    System.out.println("\nGiven the following input file:\n"); 

    System.out.println(read1); 

    System.out.println("\nSpecifying a formatted output width of " + maxWidth + " should produce the following output:\n"); 
    System.out.println("Formatted output text follows...\n"); 

    for(int n = 0; n <= maxWidth; n++) 
    { 
     System.out.print("*"); 
    } 

    // Read words from the input file, appending to the current line being built until 
    // the line plus the newest word would exceed the output column width. 
    // When the line is full, write it to the output file, and reset it to empty. 
    // Continue until end of file encountered. 
    String word = ""; 
    String line = ""; 

    System.out.println(""); 

System.out.println(newReader1); 

} 

}

+0

你在什么操作系统上?你输入的文件名是什么?文件是否真的存在,如果你在资源管理器/查找器中打开它/什么?信息来自哪里?从'textinFile.exists()'检查或从'catch(FileNotFoundException e)'块? –

回答

0

Scanner.nextLine()包括ENTER在该行的末尾。你必须删除那个角色,那么它应该工作。

相关问题