-3

我的家庭作业说“编写一个程序来读取一个文件并将文件的副本写入另一个插入了行号的文件”我有这个代码但是出了点问题,有人可以帮忙吗?先感谢您

显示文件:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class ShowFile {

public static void main(final String args[])

throws IOException

{
    int i;

    FileInputStream fin;

    try {

        fin = new FileInputStream(args[0]);

    } catch (final FileNotFoundException e) {

        System.out.println("File Not Found");

        return;

    } catch (final ArrayIndexOutOfBoundsException e) {

        System.out.println("Usage: ShowFile File");

        return;
    }

    do {

        i = fin.read();

        if (i != -1)
            System.out.print((char) i);

    } while (i != -1);

    fin.close();

}
}

拷贝文件:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class CopyFile {

public static void main(final String args[])

throws IOException

{
    int i;

    FileInputStream fin;

    FileOutputStream fout;

    try {

        // open input file

        try {

            fin = new FileInputStream(args[0]);

        } catch (final FileNotFoundException e) {

            System.out.println("Input File Not Found");

            return;

        }

        // open output file

        try {

            fout = new FileOutputStream(args[1]);

        } catch (final FileNotFoundException e) {

            System.out.println("Error Opening Output File");

            return;

        }
    } catch (final ArrayIndexOutOfBoundsException e) {

        System.out.println("Usage: CopyFile From To");

        return;

    }

    // Copy File

    try {

        do {

            i = fin.read();

            if (i != -1)
                fout.write(i);

        } while (i != -1);

    } catch (final IOException e) {

        System.out.println("File Error");

    }

    fin.close();

    fout.close();

}
}

这是错误消息-

 Exception in thread "main" java.lang.NoClassDefFoundError: C 
   Caused by: java.lang.ClassNotFoundException: C 
   at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
   at java.security.AccessController.doPrivileged(Native Method) 
   at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
   at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
   at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
   at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
4

3 回答 3

0

这个怎么样 ...

BufferedReader reader = new BufferedReader(new FileReader("infile"));
BufferedWriter writer = new BufferedWriter(new FileWriter("outfile"));
String line;
int lineNumber = 0;
while((line = reader.readLine()) != null) {
    writer.write(++lineNumber + " " + line);
    writer.newLine();
}
writer.close();
reader.close();
于 2013-03-18T03:04:15.490 回答
0

我认为问题一定出在您运行程序的方式上。异常似乎是说它找不到名为“C”的类。

我的猜测是您提供了要作为路径名而不是类名执行的类的名称。请仔细阅读该java命令的手册页。

于 2013-03-18T03:22:14.073 回答
0

你的代码没有问题。我认为你只是传递了错误的论点。

假设您的驱动器中有这个 readme.txt,您必须运行它,如下所示:

java ShowFile "C:\readme.txt"
于 2013-03-18T04:01:59.093 回答