为啥提示时无法打开输入文件?

Posted

技术标签:

【中文标题】为啥提示时无法打开输入文件?【英文标题】:Why can't I open the input file when prompted?为什么提示时无法打开输入文件? 【发布时间】:2014-07-25 01:20:15 【问题描述】:

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

   

【问题讨论】:

您使用的是什么操作系统?您键入的文件名是什么?如果您在 explorer/finder/whatever 中打开该文件,它是否真的存在?消息来自哪里 - 来自textinFile.exists() 检查或来自catch (FileNotFoundException e) 块? 【参考方案1】:

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

【讨论】:

以上是关于为啥提示时无法打开输入文件?的主要内容,如果未能解决你的问题,请参考以下文章

C语言文件为啥老提示无法打开啊!谁能告诉下谢谢!

为啥使用mklink 做目录关联时,提示“当文件已存在时,无法创建该文件”

Access数据表无法导入excel表,显示向导无法访问文件中的信息,这是为啥?

为啥打开word是提示说由于宏安全设置,无法找到宏或宏被禁用

EXCEL查询无法运行或数据库无法打开是为啥

Win10为啥测试模式关闭不了,提示无法打开启动配置数据存储