为什么我在提示时无法打开输入文件?

时间:2014-07-25 01:20:16

标签: java text-formatting

对于此代码,它是一个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);

}   

}

1 个答案:

答案 0 :(得分:0)

Scanner.nextLine()包含该行末尾的ENTER。你必须删除该字符,然后它应该工作。

相关问题