【问题标题】:Why am I getting an error when it tries to read from the file?为什么在尝试从文件中读取时出现错误?
【发布时间】:2017-01-30 04:17:53
【问题描述】:

我正在编写一个可以使用经典加密方法对消息进行编码和解码的程序。消息将从文件中读取并写入输出文件。以下程序是用 java 编写的,并且编译没有错误。当我运行程序以测试它是否在我给它输入和输出文件的名称之后到目前为止它运行到某种异常抛出错误。我认为问题在于代码中的 for 循环。这是一个将所有消息存储到字符数组中的循环。有什么建议可以修复它或其他更好的数据结构(如堆栈或队列)?

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

class CryptoProject1
{
static char eord;
static Scanner cin=new Scanner(System.in);
static char [] message=new char[10000];

public static void main (String [] args)
    throws IOException
{
    //getting the input txt file name from user
    String infilename;
    System.out.println("Please give the name of the input file.");
    infilename=cin.nextLine();
    Scanner fileread=new Scanner (new FileReader(infilename));  

    //getting the output txt file name from user
    String outfilename;
    System.out.println("Please give the name of the output file.");
    outfilename=cin.nextLine();
    PrintWriter filewrite=new PrintWriter(new FileWriter(outfilename));

    //saving the message into an array
    //construct/make it into a usable function??
    for(int i=0; i<message.length; i++)
    {
        message[i]=fileread.next().charAt(0);
    }

    //trial to make sure it reads and writes correctly
    //printing the message onto the output file
    for(int i=0; i<message.length; i++)
    {
        filewrite.print(message[i]);
    }

}   

【问题讨论】:

  • “它遇到了某种异常”。有什么例外?它应该准确地告诉您出了什么问题,以及哪里出了问题。
  • 在你的第一个循环中添加一个检查:if(fileread.hasNext()) 然后执行操作 else break
  • @resueman 这是它给出的错误消息:java.util.Scanner.throwFor(Scanner.java:907) 处的线程“main”java.util.NoSuchElementException 中的异常。 Scanner.next(Scanner.java:1416) 在 CryptoProject1.main(CryptoProject1.java:112)

标签: java cryptography filereader filewriter


【解决方案1】:
for(int i=0; i<message.length; i++)
{
  message[i]=fileread.next().charAt(0);
}

在这里,您无法知道文件长度是否与消息相同或更长,文件可能有 10 个字符。你需要这样做:

for(int i=0; i<message.length; i++)
{
  if(!fileread.hasNext())
    break;
  message[i]=fileread.next().charAt(0);
}

简单检查一下是否还有要从文件中读取的内容,如果没有则停止读取。

此外,习惯上使用 Java 对象 File 来表示文件,而不是使用字符串来保存文件路径。示例:

private File output;
public void create file(String path)
{
    output = new File(path);
}

private BufferedWriter out = new BufferedWriter(new FileWriter(output));

【讨论】:

  • 修复了错误,但现在当它写入输出文件时,只需要每个单词的第一个字母,然后是后面的一堆空值。
  • 嗯,你正在逐个字符地读取文件,而且扫描仪很难(next() 获取下一个令牌,你正在从中获取第一个字符)。
  • 我的建议:将message 更改为字符串列表ArrayList&lt;String&gt; 并将fileread.nextLine() 添加到其中。这需要很少的更改(您不能使用它的大小来读取文件,因为它的大小在创建后为0,您需要像while(fileread.hasNext()) message.add(fileread.nextLine()) 这样的while 循环。然后您可以使用列表的size() 写入文件并循环。 Check this for reference
【解决方案2】:

此外,无论何时写入文件,请确保通过调用 close() 方法将其关闭,否则将不会保存其内容

【讨论】:

  • 是的,这意味着在读取字符并将其分配给数组的循环中可能存在问题。它可能与 .charAt(0) 有关。
【解决方案3】:
//I ran this code without an error
//getting the input txt file name from user
String infilename;
System.out.println("Please give the name of the input file.");
infilename=cin.nextLine();
Scanner fileread=new Scanner (new FileReader(infilename));  

//getting the output txt file name from user
String outfilename;
System.out.println("Please give the name of the output file.");
outfilename=cin.nextLine();
PrintWriter filewrite=new PrintWriter(new FileWriter(outfilename));

//saving the message into an array
//construct/make it into a usable function??
for(int i=0; i<message.length; i++)
{
    if(fileread.hasNext())
    message[i]=fileread.next().charAt(0);
}
fileread.close();

//trial to make sure it reads and writes correctly
//printing the message onto the output file
for(int i=0; i<message.length; i++)
{
    filewrite.print(message[i]);
}
filewrite.close();
}

【讨论】:

  • 我确保我的代码完全匹配这个,它仍然只给出每个单词的第一个字母和输出中的一堆空值......我现在编译它没有问题,我'我无法让它按照我想要的方式工作。
猜你喜欢
  • 1970-01-01
  • 2023-01-31
  • 1970-01-01
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多