求大神帮忙!这个程序数组为什么会溢出?
程序如下
import java.io.BufferedInputStream;
import java.io.File;
import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import
java.io.IOException;
public class TestLineNumbering {
/**
* @param args
*/
public static void main(String[] args) {
//args=new String[100];
new TestLineNumbering(args[0]);
}
String fileName;
File out;
int row = 1;
BufferedInputStream bufferedInputStream = null;
FileOutputStream fileOutputStream = null;
public TestLineNumbering(String fileName)
{
if(!fileName.endsWith(".txt"))
{
fileName += ".txt";
}
fileName = "test/9_1/" + fileName;//测试文件夹
out = new File(fileName);
bufferedInputStream = new BufferedInputStream(System.in);
try {
fileOutputStream = new FileOutputStream(out);
} catch (
FileNotFoundException e1) {
e1.printStackTrace();
}
System.out.println("请输入文件内容(以Ctrl+Z结束):");
try {
int temp;
fileOutputStream.write((Integer.toString(1) + " ").getBytes());
while((temp =bufferedInputStream.read()) != -1)
{
fileOutputStream.write(temp);
if(temp == '\n')
{
fileOutputStream.write((Integer.toString(++row) + " ").getBytes());
}
}
} catch (
IOException e) {
e.printStackTrace();
}
finally{
try {
if(bufferedInputStream != null)
{
bufferedInputStream.close();
}
if(fileOutputStream != null)
{
fileOutputStream.close();
}
System.out.println("文件保存成功!");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
为什么会给我报Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 0
at exercise.TestLineNumbering.main(TestLineNumbering.java:16)
这个错呢?应该怎么修改才好?请各位大神多多帮忙啊!!谢谢啦!!!
------解决方案--------------------具体是哪一行呢
------解决方案--------------------args[0]没有给参数,命令行输入例如:
javac TestLineNumbering.java
java TestLineNumbering 7
------解决方案--------------------new TestLineNumbering(args[0]);这一行是执行你的构造器的:
public TestLineNumbering(String fileName),里面定义什么写什么就好了,起个文件名,例如aa,随你。
变成new TestLineNumbering(aa);就对了。。。
------解决方案--------------------不用改代码,在输入命令时后面加上文件名:
java TestLineNumbering
out.txt