package test.io;import java.io.*;public class ReadFile{
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{
fis = new FileInputStream(args[0]);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
}
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace(); }catch(IOException ee){
System.out.println("读错误。");
}finally{
try{
fis.close();
}catch(IOException e){
System.out.println("文件错误");
System.exit(1);
}
}
}
}
编译通过了,运行时不等我输入数据就运行了,错误如下:
E:\self>java test.io.ReadFile
Exception in thread "main" java.lang.NullPointerException
at test.io.ReadFile.main(ReadFile.java:25)哪位有抽点时间帮我看看吧。
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{
fis = new FileInputStream(args[0]);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
}
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace(); }catch(IOException ee){
System.out.println("读错误。");
}finally{
try{
fis.close();
}catch(IOException e){
System.out.println("文件错误");
System.exit(1);
}
}
}
}
编译通过了,运行时不等我输入数据就运行了,错误如下:
E:\self>java test.io.ReadFile
Exception in thread "main" java.lang.NullPointerException
at test.io.ReadFile.main(ReadFile.java:25)哪位有抽点时间帮我看看吧。
package test.io;import java.io.*; public class ReadFile{
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
if(null==str)
return;
fis = new FileInputStream(str);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
//FileOutputStream fos=new FileOutputStream("d:\1.txt");
//fos.write(buff,0,n);
} //end while
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace();
}catch(IOException ee){
System.out.println("读错误。");
}catch(NullPointerException e){
System.err.println("没有输入参数");
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}finally{
try{
if(null!=fis)
fis.close();
}catch(IOException e){
System.out.println("文件错误");
System.exit(1);
}
} //end try...catch()
} //end main()
} //end class ReadFile
import java.io.*;public class FileInputStreamTest {
public FileInputStreamTest() {
}
public static void main(String[] args){
FileInputStream fls = null;
File file = null;
int n;
try{
file = new File("FileInputStreamTest.java");
fls = new FileInputStream(file);
while((n=fls.read())!=-1){
//char ch = (char)fls.read();
System.out.write (n);
}
fls.close();
}catch(FileNotFoundException fnfe){
System.out.println ("找不到文件!!");
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
我这个程序现在运行的问题是我还没有从键盘输入数据,他就运行了。我是说在DOS下输入参数!!这样的话该怎么做啊??不知道我说的意思你明白了吗?
比如说java a 参数一 参数二 参数三
你 要在运行 的时候就需要用到流了.System.in对像.
java test.io.ReadFile abc.txt
你的COMMANDLINE都没有肯定不行
在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
if(null==str)
return;
fis = new FileInputStream(str);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
//FileOutputStream fos=new FileOutputStream("d:\1.txt");
//fos.write(buff,0,n);
} //end while
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace();
}catch(IOException ee){
System.out.println("读错误。");
}catch(NullPointerException e){
System.err.println("没有输入参数");
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}finally{
try{
if(null!=fis)
fis.close();
}catch(IOException e){
System.out.println("文件错误");
System.exit(1);
}
} //end try...catch()
} //end main()
} //end class ReadFile
public FileInputStreamTest() {
}
public static void main(String[] args){
FileInputStream fls = null;
File file = null;
int n;
try{
file = new File("FileInputStreamTest.java");
fls = new FileInputStream(file);
while((n=fls.read())!=-1){
//char ch = (char)fls.read();
System.out.write (n);
}
fls.close();
}catch(FileNotFoundException fnfe){
System.out.println ("找不到文件!!");
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}