[英]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.