我的程序出错了

时间:2014-04-28 07:07:02

标签: java mysql arrays intellij-idea

我收到此错误:

Information:Using javac 1.8.0 to compile java sources
Information:java: Errors occurred while compiling module 'HelloWorld'
Information:Compilation completed with 2 errors and 0 warnings in 6 sec
Information:2 errors
Information:0 warnings
C:\Users\Raj\IdeaProjects\HelloWorld\src\CopyCharacters.java
Error:(18, 29) java: cannot find symbol
  symbol:   variable c
  location: class CopyCharacters
Error:(21, 30) java: cannot find symbol
  symbol:   variable c
  location: class CopyCharacters

我从这个doc做练习,现在当我使用intellij idea IDE运行我的程序时,它给了我这个错误..

这是我的源代码:

import java.io.*;

public class CopyCharacters {
    public static void main(String[] args) throws IOException{

        FileReader inputstream=null;
        FileWriter outputstream=null;

        try{
            inputstream=new FileReader("D://raj.txt");
            outputstream=new FileWriter("D://raj1.txt");

            int c;
            while((c=inputstream.read())!=-1){
                outputstream.write(c);
            }
        }finally{
            if(inputstream!=c){
                inputstream.close();
            }
            if(outputstream!=c){
                outputstream.close();
            }
        }
    }
}

EDITED

根据@I希望我能想到一个好的答案它可以正常工作,但它显示出新的错误:

Exception in thread "main" java.lang.ClassNotFoundException: ArrayDemo
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)

4 个答案:

答案 0 :(得分:1)

改变你最后阻止

    }finally{
        if(inputstream!=null){
            inputstream.close();
        }
        if(outputstream!=null){
            outputstream.close();
        }
    }

为什么要将流与int进行比较?

执行此操作后,您会发现您可以保留c的声明。

答案 1 :(得分:0)

move int c;到外面尝试阻止

import java.io.*;

public class CopyCharacters {
    public static void main(String[] args) throws IOException{

        FileReader inputstream=null;
        FileWriter outputstream=null;
int c=0;

        try{
            inputstream=new FileReader("D://raj.txt");
            outputstream=new FileWriter("D://raj1.txt");

            //int c;
            while((c=inputstream.read())!=-1){
                outputstream.write(c);
            }
        }finally{
            if(inputstream!=c){
                inputstream.close();
            }
            if(outputstream!=c){
                outputstream.close();
            }
        }
    }
}

答案 2 :(得分:0)

我不明白为什么要尝试将intReader进行比较

if(inputstream!=c){

它们是不兼容的,无法进行比较,它没有意义。

由于您使用的是Java 1.8,因此您可以利用try-with-resource功能,该功能会自动关闭您的流,例如......

try (FileReader inputstream = new FileReader("D://raj.txt")) {
    try (FileWriter outputstream = new FileWriter("D://raj1.txt")) {
        int c;
        while ((c = inputstream.read()) != -1) {
            outputstream.write(c);
        }
    } finally {
    }
} finally {
}

答案 3 :(得分:-1)

在这里,您在TRY块中声明变量C.在TRY之外无法访问。在这里你可以在Finally块中访问它,它会给你错误。全局声明int c。

相关问题