我正在尝试完成这项任务:
编写一个程序,读取一个文件并将该文件的副本写入另一个插入行号的文件。
到目前为止,我已经写了这么多代码:
import java.io.*;
import java.util.Scanner;
public class Question43
{
public static void main(String[] args) throws IOException
{
Scanner fileIn = new Scanner(new File("Assign4.txt") );
FileWriter fileOut = new FileWriter("FileOut.txt");
PrintWriter output = new PrintWriter(fileOut);
String []array = new String[10];
int indx = 0;
while(fileIn.hasNext( ) )
{
array[indx] = fileIn.nextLine( );
}
fileIn.close();
int num = 1;
for(int i =0; i < array.length; i++)
{
output.println(num+"\t"+array[i]);
}
}
}
当我单击运行时,我收到此错误:
Exception in thread "main" java.io.FileNotFoundException: Assign4.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Question43.main(Question43.java:8)
有人可以告诉我该怎么做吗?我应该将名为 Assign4 的文本文件放在哪里,以便 Eclipse IDE 可以读取它?另外,新文件将在哪里创建?
我感谢您的帮助!