各位前辈,我这个程序的这个错如何改啊??在线等

Hearkener 2008-01-17 10:24:45
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)

哪位有抽点时间帮我看看吧。
...全文
92 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahe7788 2008-01-18
  • 打赏
  • 举报
回复
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();
}
}
}
hahe7788 2008-01-18
  • 打赏
  • 举报
回复
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
hahe7788 2008-01-18
  • 打赏
  • 举报
回复
按有几个群,你不妨加进去,可以和大家一起讨论啊.........46986340,28039577,4804620
在那里看看有无能回答你的,谢谢,LZ,甭忘了给俺分哦,谢谢LZ
dracularking 2008-01-18
  • 打赏
  • 举报
回复
Dos下输入参数 跟在类名后面 按顺序是 args[0] args[1]...

java [-options] class [args...]

具体如:
java test.io.ReadFile abc.txt
casablancaliu 2008-01-18
  • 打赏
  • 举报
回复
E:\self> java test.io.ReadFile filepath 试下这样。
你的COMMANDLINE都没有肯定不行
awusoft 2008-01-18
  • 打赏
  • 举报
回复
main里的参数是运行的时候就指定的
比如说java a 参数一 参数二 参数三
你 要在运行 的时候就需要用到流了.System.in对像.
Hearkener 2008-01-18
  • 打赏
  • 举报
回复
感谢你对我的问题的回答,我想通过DOS用键盘传参数,我记得好像是用main(String[] args)里的参数 args[i]就行来着 但现在不知怎么用了,当时好像没用到流。
我这个程序现在运行的问题是我还没有从键盘输入数据,他就运行了。我是说在DOS下输入参数!!这样的话该怎么做啊??不知道我说的意思你明白了吗?
michaelye18 2008-01-18
  • 打赏
  • 举报
回复

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();
}
}
}
michaelye18 2008-01-17
  • 打赏
  • 举报
回复

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
michaelye18 2008-01-17
  • 打赏
  • 举报
回复
由于你没有输入参数,因此构造的对象fis不存在,因此在调用fis.close()会抛出空指针异常

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧