簡體   English   中英

使用FileOutputStream復制文件,編譯時找不到符號

[英]copy files using FileOutputStream, Cannot find symbol when complie

我嘗試使用FileInputStream和FileOutputStream通過以下代碼復制文件的內容:

public class Example1App {
   public static void main(String[] args) {
    Example1 program= new Example1();
    program.start();
   }
}

import java.io.*;
public class Example1 {
    public static void main(String[] args) throws Exception {
       FileInputStream fin = new FileInputStream(args[0]);
       FileOutputStream fout = new FileOutputStream(args[1]);
       int c;
       while ((c = fin.read()) != -1)
       fout.write(c);
       fin.close();
       fout.close();
     }
   }

編譯時,錯誤消息為:找不到符號program.start();。 ^符號:方法start()位置:Example1類型的變量程序有人可以幫助我解釋為什么發生這種情況嗎? 非常感謝您的提前幫助。

發生這種情況是因為您的Example1類中沒有名為start

您可能想做的是:

  1. 在您的Example1類中創建一個start()方法
  2. 更好的是,代替start() ,將方法命名為copy並為其指定參數: copy(String arg0, String arg1)

因此,您將獲得:

import java.io.*;
public class Example1 {
    public void copy(String inName, String outName) throws Exception {
       FileInputStream fin = new FileInputStream(inName);
       FileOutputStream fout = new FileOutputStream(outName);
       int c;
       while ((c = fin.read()) != -1)
       fout.write(c);
       fin.close();
       fout.close();
     }
}

和:

public class Example1App {
   public static void main(String[] args) {
       Example1 program = new Example1();
       try {
         program.copy(args[0], args[1]);
       } catch (Exception e) {
         // Generally, you want to handle exceptions rather 
         // than print them, and you should handle some 
         // exceptions in copy() so you can close any open files.
         e.printStackTrace(); 
       }
   }
}

(實際上,您可以將它們組合成一個程序-只需將main方法從Example1App移至Example1,然后刪除Example1App)

您正在調用一個不存在的方法,如消息所示。 只需完全擺脫第一類並使用'java Example1'直接執行第二類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM