简体   繁体   English

尝试从文件中读取文件时为什么会出现错误?

[英]Why am I getting an error when it tries to read from the file?

I am writing a program that can encode and decode messages using classical encryption methods. 我正在编写一个程序,可以使用经典的加密方法对消息进行编码和解码。 The message is to be read in from a file and written to an output file. 该消息将从文件中读取并写入输出文件。 The following program is written in java and compiles with no errors. 以下程序是用Java编写的,并且没有错误进行编译。 When I run the program to test if it works so far after I give it the names of the input and output file it runs into some kind of exception throwing error. 当我运行该程序以测试其输入和输出文件的名称后是否能正常运行时,它会遇到某种引发异常的错误。 I assume the issue lies in the for loop that follows in the code. 我认为问题出在代码后面的for循环中。 It is a loop to store all of the message into an array of characters. 将所有消息存储到一个字符数组中是一个循环。 Any suggestions to fix it or another data structure that would work better (like a stack or queue)? 有什么建议修复它或其他更好的数据结构(如堆栈或队列)?

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]);
    }

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

Here, you cannot know if file length is same or higher than message, file may as well have 10 characters. 在这里,您不知道文件长度是否等于或大于消息长度,文件也可能有10个字符。 You need to do that: 您需要这样做:

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

Just a simple check if there is still something to be read from file, if not then stop reading. 只需简单检查一下是否还有文件需要读取,否则请停止读取。

Also it is customary to use the Java object File to represent files rather than using string to hold the file path. 同样习惯上使用Java对象File来表示文件,而不是使用字符串来保存文件路径。 Example: 例:

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

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

另外,每当写入文件时,请确保通过调用close()方法将其关闭,否则将不保存其内容

//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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在尝试读取 excel 文件时收到数据提供程序不匹配错误? - Why I am getting Data Provider MisMatch error when I try to read from excel file? 使用Scanner读取文件:为什么在Java中使用Scanner读取文件时出现错误? - read a file using Scanner: Why am I getting an error when using Scanner for read files in java? 当程序尝试从实体流中反序列化对象时出现错误 - I am getting an error when the program tries to deserialise an object from the entity stream 为什么我在尝试从文本文件中读取时收到错误消息“在java代码中未报告的异常Java.io”? - Why am I getting an error message “unreported exception Java.io in a java code when trying to read from a text file”? 尝试从文件中读取某些字节时,为什么会超出索引范围? - Why am I getting an index out of bounds when I try to read certain bytes from a file? 为什么我在尝试读取文件时收到 NullPointerException? - Why am I getting a NullPointerException when trying to read a file? 为什么在处理图像文件时出现此错误? - Why am I getting this error when handling an image file? 从cmd重定向输出时,为什么输出文件为空? - Why am I getting the output file as empty when I am redirecting the output from cmd? 为什么在尝试读取.DOCX文件时出现异常? - Why am I getting exception as I try to read .DOCX file? 为什么使用Swagger和OIDC会收到“无法从服务器读取错误信息”? - Why am I getting 'Can't read from server error' with Swagger and OIDC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM