java - 为什么提示时无法打开输入文件?

标签 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);

}   

}

最佳答案

Scanner.nextLine() 在行末尾包含 ENTER。您必须删除该字符,然后它才可以工作。

关于java - 为什么提示时无法打开输入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946569/

相关文章:

restructuredtext - 重构文本 - 逃避标题?

mysql - 如何在 MySQL 数据库文本字段中创建粗体数据?

java - 构建 jar 后,弹出错误 NoSuchMethodError for int but long ok

java - 附加到 HTML 内容时如何保持 SWT 浏览器小部件的底部可见?

java - 使用异常的流程成本

unix中的清除提示

angularjs - 在 Angularjs 中显示文本之前设置文本格式

java - 获取小盒子而不是实际字符

java - 迁移 Feign Load Balancer 实现以兼容 Spring cloud 2020.0.0

url - 如何在 Latex 中编写 URL?