2013-03-18 93 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"