2013-03-18 96 views
-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(); 

} 
} 

的CopyFile:

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

} 
} 

这是错误MESSAGE-

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) 
+0

什么是错误讯息? – Patashu 2013-03-18 03:03:27

+0

而问题会是? – MadProgrammer 2013-03-18 03:07:26

+0

@Slink保持简单,您可以尝试下面给出的代码。 – 2013-03-18 03:15:22

回答

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(); 
+0

谢谢,我只是试过这个,我得到一个错误,说BufferedReader不能解析为一个类型。 – Slink 2013-03-18 03:17:19

+0

您是否导入了所需的课程? – 2013-03-18 03:18:49

+0

@Sudhanshu - 给学生编码以便他们不必做自己的作业是一个坏主意。特别是因为真正的问题似乎与学生的代码无关。 – 2013-03-18 03:24:56

0

我认为问题必须以您运行程序的方式进行。这个例外似乎是说它找不到一个名为“C”的类。

我的猜测是你已经提供了作为路径名而不是类名执行的类的名字。请仔细阅读java命令的手册页。

0

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

让你在你的驱动器有这样的readme.txt, 你必须运行它,就像这样:

java ShowFile "C:\readme.txt"