你的fileinputstream在作用域外访问了,改成这样
import java.io.*;
class output
{
public static void main(String[] args) throws Exception
{
int size;
byte bytearray[]=new byte[50];
FileInputStream fileinputstream = null;
try
{
fileinputstream=new FileInputStream("d:\\java\\abc.java");
}
catch(IOException nofound)
{
System.out.println("can not found file");
}
size=fileinputstream.available();
System.out.println("Available bytes="+size);
System.out.println("reading 50 bytes");
if(fileinputstream.read(bytearray)!=50);
{
System.out.println("lost");
}
System.out.println(new String(bytearray,0,50)); fileinputstream.close();
}
}
import java.io.*;
class output
{
public static void main(String[] args) throws Exception
{
int size;
byte bytearray[]=new byte[50];
FileInputStream fileinputstream = null;
try
{
fileinputstream=new FileInputStream("d:\\java\\abc.java");
}
catch(IOException nofound)
{
System.out.println("can not found file");
}
size=fileinputstream.available();
System.out.println("Available bytes="+size);
System.out.println("reading 50 bytes");
if(fileinputstream.read(bytearray)!=50);
{
System.out.println("lost");
}
System.out.println(new String(bytearray,0,50)); fileinputstream.close();
}
}
是在try块中定义的,块外不能访问,改成这样
FileInputStream fileinputstream = null;
try
{
fileinputstream=new FileInputStream("d:\\java\\abc.java");
}
就可以了
这句话在try{}中,所以它的适用范围不能出去,try{}外面的就不认了。
你可以把 FileInputStream fileinputstream = null;先放在方法一开始的地方。然后try{}中直接用fileinputstream=new FileInputStream("d:\\java\\abc.java");